tags:

views:

68

answers:

6

I got this code from w3schools, and tweaked it based on this one: http://stackoverflow.com/questions/2562851/rename-an-uploaded-file-with-php-but-keep-the-extension

Please help, what do I do so that the file extension will not be .tmp When I upload it to a folder on the server.

   <?php
    $filename=$_FILES["file"]["tmp_name"];
    $extension=end(explode(".", $filename));
    $newfilename=$prod .".".$extension;

    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < 100000))
      {
      if ($_FILES["file"]["error"] > 0)
        {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
      else
        {
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

        if (file_exists("../img/user_uploaded/" . $_FILES["file"]["name"]))
          {
          echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($filename,
          "../img/user_uploaded/" . $newfilename);
          echo "Stored in: " . "../img/user_uploaded/" .$newfilename;
          }
        }
      }
    else
      {
      echo "Invalid file, File must be less than 100Kb in size with .jpg, .jpeg, or .gif file extension";
      }
    ?>
A: 
rename('path/to/file/file.ext', 'path/to/file/newname.ext');

This function is the concatenating of copy and unlink.

Alexander.Plutov
+1  A: 

This part does rename the file.

move_uploaded_file($filename,"../img/user_uploaded/" . $newfilename);

So the issue is how to set the value of $newfilename. If you have a method to produce this then the issue is solved.

Bandpay
+1  A: 

I disagree with the answer of @Alexander.Plutov. You don't have to save a file, then copy it, then unlink it.

Since you specify the destination file name in your code:

move_uploaded_file($filename, "../img/user_uploaded/" . $newfilename);
echo "Stored in: " . "../img/user_uploaded/" .$newfilename;

all you have is to specify another name instead of $newfilename. If your goal is to replace .tmp extension by something else, you may remove the last four characters (which are always .tmp, set by PHP), then add your own extension.

If you just want to keep the extension given by the user, use:

$friendlyName = $_FILES["file"]["name"];
$extension = substr($friendlyName, strrpos($friendlyName, '.') + 1);

Of course, you have to be aware of the risk of using something posted by the user. What if there is no extension at all, or if it is too long, or if it does not match the real file type (deadly-virus.exe renamed to pretty-image.jpg)?

MainMa
+1  A: 

Try to use this library for image manipulation... maybe is offtopic, but is realy usefull for dealing with images.

WideImage: http://wideimage.sourceforge.net/

For example:

include('path to WideImage.php');

$filename = $_FILES["file"]["name"];
$ext = strtolower(substr(strrchr($filename, '.'), 1)); //Get extension
$image_name = $name . '.' . $ext; //New image name

$image = WideImage::load($_FILES['file']['tmp_name']); //Get image
$image->saveToFile('path/to/image/' . $image_name); //Save image
Wolfy
thanks for that, I also need to manipulate my image
A: 

Small example you can use rand() function to create random file-name.

$min_rand=rand(0,1000);
$max_rand=rand(100000000000,10000000000000000);
$name_file=rand($min_rand,$max_rand);//this part is for creating random name for image

$ext=end(explode(".", $_FILES["file"]["name"]));//gets extension

move_uploaded_file($_FILES["file"]["tmp_name"],"/new folder/" . $name_file.".".$ext);//actually reponsible for copying file in new folder

$filenameimage="../upload_image/" . $name_file.".".$ext;
$actualimageurl="../upload_image/" . $_FILES["file"]["name"];
Wazzy
`rand() ... rand() ... rand()` -- Deeply confused :-S
jensgram
A: 
$filename=$_FILES["file"]["tmp_name"];
$extension=end(explode(".", $filename));
$newfilename=$prod .".".$extension;

In the above code u r trying to get the temporary file's extension. Instead of u can take the extension of the original filename uploaded by the user.

$filename=$_FILES["file"]["name"];
$extension=end(explode(".", $filename));
$newfilename=$prod .".".$extension;

This will upload your file with the actual file extension.

Mohamed
in my case, this will not upload the file at all.