how to rename uploaded image in php

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

In the above code you are trying to get the temporary file's extension. Instead, you 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.


rename('path/to/file/file.ext', 'path/to/file/newname.ext');

This function is the concatenating of copy and unlink.


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

Tags:

Php

Upload