change jpg to JPG

Discussion in 'PHP' started by dougvcd, Oct 5, 2011.

  1. #1
    have some code which uploads a pic to folder on server
    is it poss if its jpg it can change it to JPG
    here is the upload code i use
    cheers
    Doug

    <?php
    //set where you want to store files
    //in this example we keep file in folder upload
    //$HTTP_POST_FILES['ufile']['name']; = upload file name
    //for example upload file name cartoon.gif . $path will be upload/cartoon.gif
    $path= "pics/".$HTTP_POST_FILES['ufile']['name'];
    if($ufile !=none)
    {
    if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
    {
    echo "Successful<BR/>";

    //$HTTP_POST_FILES['ufile']['name'] = file name
    //$HTTP_POST_FILES['ufile']['size'] = file size
    //$HTTP_POST_FILES['ufile']['type'] = type of file
    echo "File Name :".$HTTP_POST_FILES['ufile']['name']."<BR/>";
    echo "File Size :".$HTTP_POST_FILES['ufile']['size']."<BR/>";
    echo "File Type :".$HTTP_POST_FILES['ufile']['type']."<BR/>";
    echo "<img src=\"$path\" width=\"150\" height=\"150\">";
    }
    else
    {
    echo "Error";
    }
    }
    ?>
     
    dougvcd, Oct 5, 2011 IP
  2. sparek

    sparek Peon

    Messages:
    68
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Before the line

    $path= "pics/".$HTTP_POST_FILES['ufile']['name'];
    PHP:
    Try adding the code:

    if (preg_match(/"\.JPG$/", $HTTP_POST_FILES['ufile']['name'])) {
    	$HTTP_POST_FILES['ufile']['name'] = preg_replace("/\.JPG$/", ".jpg", $HTTP_POST_FILES['ufile']['name']);
    }
    PHP:
    This basically says that if $HTTP_POST_FILES['ufile']['name'] ends with .JPG then change that .JPG to .jpg
     
    sparek, Oct 5, 2011 IP
  3. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks just put code in and tells me syntex error in this line
    if (preg_match(/"\.JPG$/", $HTTP_POST_FILES['ufile']['name'])) {
     
    dougvcd, Oct 5, 2011 IP
  4. sparek

    sparek Peon

    Messages:
    68
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Sorry, the correct segment would be

    if (preg_match("/\.JPG$/", $HTTP_POST_FILES['ufile']['name'])) {
        $HTTP_POST_FILES['ufile']['name'] = preg_replace("/\.JPG$/", ".jpg", $HTTP_POST_FILES['ufile']['name']);
    }
    PHP:
    I had the / outside of the quotation marks
     
    sparek, Oct 5, 2011 IP
  5. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    big thanks just spotted it as you replied
    once again cheers
    Doug
     
    dougvcd, Oct 5, 2011 IP
  6. webshore88

    webshore88 Well-Known Member

    Messages:
    131
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #6
    Here are some code that I use often you can try and or modify it..

    // checking which type of image is
    $imgType = exif_imagetype($_FILES['upload_image']['tmp_name']);
    // catching extention from file name
    // here you can change ext as you want
    if($imgType === 1){
    $ext = ".gif";
    }
    if($imgType === 2){
    $ext = ".jpg";
    }
    if($imgType === 3){
    $ext = ".png";
    }
    $uplDir = "picture/"; // where you want to your image
    $picName = "picture_name";
    $tarPath = $uplDir.$picName.$ext;
    // lets save pic into picture folder
    move_uploaded_file($_FILES['uploadIn']['tmp_name'], $tarPath);

    image type list http://php.net/manual/en/function.exif-imagetype.php

    tell me if you have any other problem
     
    webshore88, Oct 5, 2011 IP
  7. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    ok all works fine big thanks
    can i add a line some where if the file exsits asks if overwrite at moment just gives error
    cheers
    Doug
     
    dougvcd, Oct 8, 2011 IP
  8. HuggyEssex

    HuggyEssex Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    45
    #8
    To check a file is there all you need to do is use this function is_file('path to file here') that will return boolean (true/false) if it's true rename the file.

    Glen
     
    HuggyEssex, Oct 8, 2011 IP
  9. webshore88

    webshore88 Well-Known Member

    Messages:
    131
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #9
    hey dougvcd,
    you can use file_exists($tarPath); before move_uploaded_file($_FILES['uploadIn']['tmp_name'], $tarPath);
    cheers
    webshore88
     
    webshore88, Oct 8, 2011 IP
  10. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    cheers to all for info
     
    dougvcd, Oct 10, 2011 IP