this is an example I picked from W3School. Im just trying to modify this to suit my requirement.
What I need is when an image is uploaded, the image to be renamed in a sequence to so duplication is avoided. With this code if two users has two different images but if by chance if the name of the two different images are the same, one user will get a prompt saying 'already exists' preventing uploading.
I've tried replacing
echo $_FILES["file"]["name"] . " already exists. ";
with
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["tmp_name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["tmp_name"];
but doesn't seems to be giving me a solution.
Can some one suggest me a way out to rename the images uploading in a sequence? Thanks.
The code:
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($extension, $allowedExts))
{
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("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>