php question

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!

Moderator: General Moderators

Post Reply
calmchess
Forum Commoner
Posts: 33
Joined: Fri Aug 18, 2006 8:14 pm

php question

Post 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]
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
calmchess
Forum Commoner
Posts: 33
Joined: Fri Aug 18, 2006 8:14 pm

Post by calmchess »

thanks guys
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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:
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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.
Post Reply