简体   繁体   中英

Create a unique name for the uploaded image

I have a certain code for image upload .I found this on the internet and there was no explanation of the code either.What i can understand from the code is that php upload a certain file makes it a temporary file and then moves the temporary file to the original location

Code Looks something like this

 $filename = $_FILES["img"]["tmp_name"];
          list($width, $height) = getimagesize( $filename );


          move_uploaded_file($filename,  $imagePath . $_FILES["img"]["name"]);

What happens now is that when i try to provide an unique name to the image when it is being moved using the move_uploaded_file then a file does come up inside the folder but it says an invalid file and with the extension type of file . My code for trying to achieve the same but with an unique name/id for the uploaded image.

$uniquesavename=time().uniqid(rand());
$filename = $_FILES["img"]["tmp_name"];
      list($width, $height) = getimagesize( $filename );
      move_uploaded_file($filename,  $imagePath . $uniquesavename);

How to achieve the same as before and could you please explain me the previous code as well?

Sample code:

// Get file path from post data by using $_FILES
$filename = $_FILES["img"]["tmp_name"];
// Make sure that it's a valid image which can get width and height
list($width, $height) = getimagesize( $filename );
// Call php function move_uploaded_file to move uploaded file
move_uploaded_file($filename,  $imagePath . $_FILES["img"]["name"]);

Please try this one:

// Make sure this imagePath is end with slash
$imagePath = '/root/path/to/image/folder/';
$uniquesavename=time().uniqid(rand());
$destFile = $imagePath . $uniquesavename . '.jpg';
$filename = $_FILES["img"]["tmp_name"];
list($width, $height) = getimagesize( $filename );       
move_uploaded_file($filename,  $destFile);

Edit 1: To get image type in two ways:

  • Get the file type from upload file name.
  • Use php function as below

CODE

// Get details of image
list($width, $height, $typeCode) = getimagesize($filename);
$imageType = ($typeCode == 1 ? "gif" : ($typeCode == 2 ? "jpeg" : ($typeCode == 3 ? "png" : FALSE)));
$name = $_FILES['file']['name'];
$tmp_name =  $_FILES['file']['tmp_name'];
$location = "uploads/";
$new_name = $location.time()."-".rand(1000, 9999)."-".$name;
if (move_uploaded_file($tmp_name, $new_name)){
            echo "uploaded";
}
else{
    sleep(rand(1,5));
    $new_name = $location.time()."-".rand(1000, 9999)."-".$name;
    if (move_uploaded_file($tmp_name, $new_name)){
            echo "uploaded";
    }
    else{
            echo"failed, better luck next time";
    }
}

here, location is folder inside directory, i mainly create folder "uploads" time() adds timestamp , which is always unique, until two person upload at same time, which is rare.

moreover, adding 4 digit random number to it , making combination rarest

after that adding actual file name , to making combination unique.

why i use it :

  1. u can extract timestamp later if u need to know when image was uploaded.
  2. u can extract actual filename too.

Lets, say our so unique combination somehow fails, then, php instance will wait for 1 to 5 second whatever random number is generated. and rename with latest timestamp and regenerated random number.

It's the best u can think of without being resource hog.

you can use

$strtotime = strtotime("now");
$filename = $strtotime.'_'.$_FILES['file']['name'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM