Page 1 of 1
renaming while uploading??
Posted: Sun Jan 08, 2006 11:24 pm
by PHPycho
i had a photo gallery for users where users can upload many photos..
I am writing an script that renames the files while uploading as
Code: Select all
$new_filename=$user_id."_"."uniqueNO";
$new_add=$new_filename.".jpg";
rename($old_add,$new_add);
problem??
>how to generate "uniqueNO" that never repeats?
>is there any function??
>this script changes any file extension to .jpg...is that good or else?
Plz help in solving above questions....
Thank U
Posted: Sun Jan 08, 2006 11:39 pm
by josh
the function uniqid() never returns the same value twice, of course you should sha1() or md5() this value to get a web-safe string for the filename. another option is keep a database of images (still store your images on the filesys) and use the value for the primary auto increment id for the filename itself.
U r welcome
Posted: Mon Jan 09, 2006 7:27 am
by s.dot
#1 ... adding .jpg to a picture just because it's a picture is bad. Even if it WAS a jpg, what if they used the extension .jpeg?
Also, there are some very other common and valid picture extensions. gif png bmp tiff
here's how to get a file's extension if it's being uploaded
Code: Select all
$extension = strstr($_FILES['formfilename']['name'],".");
Then, tack this on to the end of what you named the file. Which brings me to..
#2 .. Unless you're adding pictures inside of a loop, then naming them to time() would guarantee a unique filename for each picture (unless you have concurrent users uploading). Then you should use JshPro's advice and go with sha1(uniqid(1)).
So in the end you should end up with something like this.
Code: Select all
$filename = $_FILES['formfilename']['name'];
$file_extension = strstr($filename,".");
$my_filename = substr(sha1(uniqid(1),0,10);
$complete_filename = $my_filename.$file_extension;
and #3.. don't rename the files after you've already moved them to the server. you should'n't have to use rename() .. just name them what you want before you store it.
Posted: Mon Jan 09, 2006 9:02 am
by raghavan20
you could use something like
Code: Select all
image_id = session_id(). "_".microtime()
I think this should work even if you are trying to upload 10 or more photos at a time unless the server is extremely fast and capable of saving two or more images in a microsecond.
Posted: Mon Jan 09, 2006 9:24 am
by feyd
when dealing with uploads, I don't rely on any information a user submits. If I'm expecting a certain type of file, I analyze the file they send to check for validity, in the process I set the extension to the correct one if it's acceptable. How do you do that with images? getimagesize() will tell you what type of image it is, if any. You can set the extension based on that. As for uniquely naming the file, for security reasons, I wouldn't use the session_id() directly, if at all. In fact, I feel it shouldn't even directly relate to the user. The name I would use would likely be based off the current time in some fashion.