PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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?
<?
// 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!";
}
?>
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]
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.
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
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
Re-reading my own post... I could have been much clearer!
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.