php uniqid help


Hi everyone,
I am working on a php script for which I can upload user info and a profile picture. Currently, with the script, the image is being renamed, but the image type (.jpg .jpeg .png, etc) isnt following the renaming. So, after I test-upload an image, it appears in my database with its uniqid name, but no file-type.

Here is my script:
$file = $_FILES[‘file’];
$allowed = array(‘jpg’, ‘jpeg’, ‘png’);

	$fileName = $_FILES['file']['name'];
	$fileSize = $_FILES['file']['size'];
	$fileTmpName = $_FILES['file']['tmp_name'];
	$fileExt = explode('.', $fileTmpName);
	$fileActualExt = strtolower(end($fileExt));
	$uniqueName = substr(md5(time()), 0, 10).'.'.$fileActualExt;
	$uploaded_image = "images/".$uniqueName.$fileActualExt;
	move_uploaded_file($uniqueName, $uploaded_image);

	$username = $connection->real_escape_string($_POST['name']);
	$email = $connection->real_escape_string($_POST['email']);
	$text = $connection->real_escape_string($_POST['text']);

Any and all help is greatly appreciated!!

Thank you


So, it is possible $fileActualExt is empty.

You have $uniqueName appending the file extension, then to it again for $uploaded_image?


Maybe try echoing all of your variables followed by an exit just to see what the values of each of them are? It may give you a clue as to what is going on.

Good Luck,


Hi Tyler,

I have done something similar some time ago but my approach was a bit different.

File type:
after some googling i decided on ,exif_imagetype(), as the way of actually making sure that it is a picture what is actually uploaded and not some dodgy file renamed as picture.

Once the file was uploaded (into temporary folder since later on it will have to be ,move_uploaded_file, to proper folder) the actual file name was fed into ,pathinfo(), -
Then you get an array of useful stuff and i think that ,PATHINFO_EXTENSION, is something you may be interested in extracting and the appending to the file name.

Obviously, i am not saying that your way of doing is incorrect. And as the other mentioned - i greatly used ,echo, to actually see what was going on all along the way…


The OP is trying to get the extension from the $fileTmpName = $_FILES[‘file’][‘tmp_name’]; value, not the $fileName = $_FILES[‘file’][‘name’]; value.

Also, the source file in the move_uploaded_file() statement should be the $fileTmpName = $_FILES[‘file’][‘tmp_name’]; value…

These are more good reasons to not create unnecessary variables, by copying variables to other variables. It makes more that you must keep track of and more things that you can get wrong. Just use the original $_FILES[‘file’][…] elements to reference the uploaded file information.