开发者

PHP image upload - rename without losing extension?

开发者 https://www.devze.com 2023-02-14 08:32 出处:网络
I currently have: $file_name = $HTTP_POST_FILES[\'uid\'][\'name\']; $random_digit=rand(0000,9999); $new_file_name=$random_digit.$file_name;

I currently have:

$file_name = $HTTP_POST_FILES['uid']['name'];
$random_digit=rand(0000,9999);
$new_file_name=$random_digit.$file_name;
$path= "uploads/images/users/".$new_file_name;
if($ufile !=none)
{
    if(copy($HTTP_POST_FILES['uid']['tmp_name'], $path))
    {
        echo "Successful<BR/>"; 
        echo "File Name :".$new_file_name."<BR/>"; 
        echo "File Size :".$HTTP_POST_FILES['uid']['size']."<BR/>"; 
        echo "File Type :".$HTTP_POST_FILES['uid']['type']."<BR/>"; 
    }
    else
    {
        echo "Error";
    }
}

this generates a random number before the current file name ie 4593example.jpg but i would just like it to rewrite the whole filename to 4593.jpg removing the ucrrent name (example). When i开发者_开发知识库 have tried doing this i lose the extension (.jpg) any help is appreciated.


If you're dealing with images only, I would consider detecting the image's type using getimagesize() and giving the new file an extension according to that.

That way, you don't have to rely on what extension you get from the user (which could be wrong).

Like so:

$extensions = array(
  IMAGETYPE_JPG => "jpg",
  IMAGETYPE_GIF => "gif",
  IMAGETYPE_PNG => "png",
  IMAGETYPE_JPEG2000 => "jpg",
  /* ...... several more at http://php.net/manual/en/image.constants.php 
            I'm too lazy to type them up */

 );

$info = getimagesize($_FILES['uid']['tmp_name']); 
$type = $info[2];

$extension = $extensions[$type];

if (!$extension) die ("Unknown file type");

move_uploaded_file($_FILES['uid']['tmp_name'], $path.".".$extension);

` Also:

  • As @alex says, your method has a considerable risk of collisions of random names. You should add a file_exists() check to prevent those (e.g. a loop that creates a new random number until one is found that doesn't exist yet)

  • HTTP_POST_FILES is deprecated, use $_FILES instead

  • I strongly advise you to use move_uploaded_file() instead of copy() which is vulnerable to attacks.


$ext = pathinfo($filename, PATHINFO_EXTENSION);

$newFilename = $random_digit . '.' . $ext;

BTW, what will happen if the random number clashes (which may happen next or in 10 years)?

There are many better ways to name them - for example, hash of the filename and time() should theoretically never clash (though in practice hash collisions do occur).


You can get the original extension by using the following code:

$extension = strrchr($HTTP_POST_FILES['uid']['tmp_name'], '.');

Then you can add the extension to the variable $new_file_name like this:

$new_file_name = $random_digit . $file_name . '.' . $extension;


You can use this code:

$file_name = $HTTP_POST_FILES['uid']['name']; $random_digit=rand(0000,9999); 
$extension= end(explode(".", $HTTP_POST_FILES['uid']['name']));
$new_file_name=$random_digit.'.'.$extension; $path= "uploads/images/users/".$new_file_name; if($ufile !=none) { if(copy($HTTP_POST_FILES['uid']['tmp_name'], $path)) { echo "Successful
"; echo "File Name :".$new_file_name."
"; echo "File Size :".$HTTP_POST_FILES['uid']['size']."
"; echo "File Type :".$HTTP_POST_FILES['uid']['type']."
"; } else { echo "Error"; } }

enjoy!!!!!!!!!

0

精彩评论

暂无评论...
验证码 换一张
取 消