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
https://forums.phpfreaks.com/topic/184049-upload-image-with-duifferent-name/
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.

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");

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.