John English
John English

Reputation: 43

Rename the image while uploading?

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";
  }
?>

Upvotes: 1

Views: 911

Answers (3)

Larry.Z
Larry.Z

Reputation: 3724

Using the uniqid() PHP function will probably be your best method.

Note that, if you generate identifiers simultaneously on several hosts that might happen to generate the identifier at the same microsecond

See the manual

You can also generate uniqueid by mysql table.

create table ids(id int auto_increment primary key);

When users uploading their pics, write sql like this

insert into ids () values();
select last_insert_id();

Hope this will help you.

Upvotes: 0

Amith
Amith

Reputation: 1434

try this

while moving uploaded file change file name $yourFileNAME to some unique value sucha as username or user id, so that no two files have same name.

move_uploaded_file( $_FILES['file']['tmp_name'], "upload/".$yourFileNAME."".$_FILES["file"]["type"] );

Upvotes: 1

sheplu
sheplu

Reputation: 2975

just change this line : move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $your_file_name) and for your image name, you can use md5 of the file and timestamps to create an unique sequence

Upvotes: 0

Related Questions