Reputation: 1185
Hi I have got the following code. I am trying to upload an image with a unique name by using the tempnam
function.
$upfile = tempnam('client_images', '');
unlink($upfile);
//move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile);
if($_FILES["userfile"]["type"] != "image/gif" && $_FILES["userfile"]["type"] != "image/pjpeg" && $_FILES["userfile"]["type"] != "image/png" && $_FILES["userfile"]["type"] != "image/jpeg")
{
echo "ERROR: You may only upload .jpg or .gif or .png files";
}
else
{
if(!move_uploaded_file($_FILES["userfile"]["tmp_name"],$upfile))
{
echo "ERROR: Could Not Move File into Directory";
}
else
{
echo "Temporary File Name: " .$_FILES["userfile"]["tmp_name"]."<br />";
echo "File Name: " .$_FILES["userfile"]["name"]."<br />";
echo "File Size: " .$_FILES["userfile"]["size"]."<br />";
echo "File Type: " .$_FILES["userfile"]["type"]."<br />";
}
}
?>
However for some reason I cannot see these images in my client_images
folder.
The output looks like this:
Temporary File Name: C:\Windows\Temp\phpA9F3.tmp
File Name: client.png
Why is the Temp saving it as a .tmp
and not a .png?
What could I be doing wrong with this?
Upvotes: 0
Views: 46
Reputation: 850
Replace
if(!move_uploaded_file($_FILES["userfile"]["tmp_name"], $upfile)) {
//take care of error
}
With
$nameyouwant = "sample"; //we'll use the extension from the source filename
$temp = explode(".", $_FILES["userfile"]["name"]);
$newfilename = $nameyouwant. '.' . end($temp); //add file extension
if(!move_uploaded_file($_FILES["userfile"]["tmp_name"],$upfile . $newfilename)) {
//take care of error
}
else{
//save success
}
Upvotes: 1