Page 1 of 1

php question

Posted: Mon Jan 08, 2007 1:46 pm
by calmchess
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


how do I tell the following php script to change the uploaded jpegs name so that it will always be unique?

Code: Select all

<?

// Where the file is going to be placed 
$target_path = "uploads/";

/* Add the original filename to our target path.  
Result is "uploads/filename.extension" */

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
$_FILES['uploadedfile']['tmp_name'];
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo basename( $_FILES['uploadedfile']['name']);

} else{
    echo "There was an error uploading the file, please try again!";
}

?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Jan 08, 2007 2:01 pm
by Burrito
change the second argument in move_uploaded_file().

also, please use a better title for your thread next time:


[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.

Posted: Mon Jan 08, 2007 2:03 pm
by volka
try

Code: Select all

$target_path = $target_path . uniqid() . basename( $_FILES['uploadedfile']['name']);
see http://de3.php.net/manual/en/function.uniqid.php

Posted: Mon Jan 08, 2007 2:14 pm
by calmchess
thanks guys

Posted: Mon Jan 08, 2007 2:14 pm
by Kieran Huggins
Why not rename the file to it's sha1 or md5? That way it's not random, collisions wouldn't matter if someone uploaded the same file multiple times, and it would save space.

Posted: Mon Jan 08, 2007 2:18 pm
by Burrito
Kieran Huggins wrote:Why not rename the file to it's sha1 or md5? That way it's not random, collisions wouldn't matter if someone uploaded the same file multiple times, and it would save space.
ahhh, but what if someone uploaded a different file with the same name 8O

If you're going to save the filenames in a db, just grab the row id and prepend it to the filename.

Edit: It just occurred to me that you meant an md5() of the entire file, not just its name...that'd work :wink:

Posted: Mon Jan 08, 2007 2:26 pm
by Kieran Huggins
Re-reading my own post... I could have been much clearer! :oops:

To make sure calmchess gets it: The md5() of the file (not the filename, but the contents of the file) will always* be unique. And it makes a great filename to store the data with. You would need to store the metadata in a db or something similar.

* always means so often you would live a thousand years before you saw a collision.