PDA

View Full Version : renaming while uploading??



PHPycho
01-09-2006, 05:27 AM
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


$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

ralph l mayo
01-09-2006, 05:33 AM
uploaded files are given a unique name in the tmp directory reachable by $_FILES['nameofthefileinputformelement']['tmp_name']. I don't remember if it keeps the extension or not.

Velox Letum
01-09-2006, 07:13 AM
I believe it does keep the extension, but I could be wrong. Also, for a unique id, you could write a functon, or use uniqid() (http://www.php.net/function.uniqid).

marek_mar
01-09-2006, 09:37 AM
My win version of php assigna all files the *.tmp extension.

Element
01-09-2006, 09:30 PM
I threw this into my friends script, which is what you are doing (I think) you can adjust the code obviously, though.



// Grab extension from filename and create random id.
$ext = substr($file_name, strpos($file_name, ".")+1, strlen($file_name));
$new_file_name = substr(md5(uniqid(microtime())), 0, 8).".".$ext;


The random ID part is bigger then it really needs to be, but thats what he originally had, didn't want to mess with it.