Jump to content

upload image with duifferent name


vikaspa

Recommended Posts

Dear All

 

In my portal I am allowing user to upload images on is own

 

In case user tries to upload same image (which exists on server)

I want upload the image with different name

 

for example

user 1 has uploaded an image 123.jpg

and user2 also want to upload the image with same name i.e 123.jpg

 

I donot want to overwrite the image instead want to rename it internally (without notifying the user)

say rename 123.jpg to 123-1.jpg (in this case)

so that earlier file will not be overwritten

 

 

 

 

Link to comment
Share on other sites

I usually have a table of uploaded files:

 

uploaded_files

id,                orig_name,              user_id

1, 123.jpg, 4

2, 123.jpg, 5

 

The files themselves are stored on disk at some path, i.e:

/home/webapp/uploaded

 

And the file names are that of the primary key in the database:

/home/webapp/uploaded/1

/home/webapp/uploaded/2

 

This accomplishes a few goals:

 

1) Since primary keys are guaranteed to be unique, so are the file names.

2) Since I've kept the original file name in the database, I can always present the original name to the user and they will be none the wiser.

Link to comment
Share on other sites

Just change the name in the second parameter of move_uploaded_file, here is an example:

 

$tmp_name = $_FILES["pictures"]["tmp_name"];
$tmp_new_name = $_FILES["pictures"]["name"];
$path_parts = pathinfo($tmp_new_name);
$new_name = $path_parts['filename'] . time() . $path_parts['extension'];
$uploads_dir = '/uploads';
move_uploaded_file($tmp_name, "$uploads_dir/$new_name");

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.