alemapo Posted August 18, 2009 Share Posted August 18, 2009 Hi, I have code which works for image upload and am trying to rename with random name during the move. I am using md5 to create the new name. Without md5 everything works fine but with md5 I get my new name and it moves correctly but I lose the file extension on the name. Can you see what my problem is as to why I am losing the file extension using md5? I am open to other suggestions to rename the image other than using md5 if necessary. Thanks in advance, Pamela <?php //function to upload photo function upload_photo() { //set the file location $tempname = 'C:/Users/Pamela/Desktop/put_here/'.md5($_FILES['thefile']['name']); //try to move the uploaded file if(move_uploaded_file($_FILES['thefile']['tmp_name'], $tempname)) { print '<p> Your file has been uploaded. </p>'; } else { print '<p> Your file was NOT uploaded. </p>'; print ($_FILES['thefile']['error']); } } ?> Link to comment https://forums.phpfreaks.com/topic/170780-solved-rename-image-on-upload/ Share on other sites More sharing options...
DrTrans Posted August 18, 2009 Share Posted August 18, 2009 (( Deleted because I the poster was an idiot )) read below. Link to comment https://forums.phpfreaks.com/topic/170780-solved-rename-image-on-upload/#findComment-900675 Share on other sites More sharing options...
DrTrans Posted August 18, 2009 Share Posted August 18, 2009 For your answer based on md5(); md5 is encryption that randomizes a key based on supplied information ... md5 does not see the .extension as an extension. MD5 see's it as a string. for example $variable = md5($password); if you have variable = md5("picture.jpg"); its going to put picture.jpg out as just a constant string. That is the reason md5 does not work for this type of scenario. http://us2.php.net/md5 <-- more information on MD5 Link to comment https://forums.phpfreaks.com/topic/170780-solved-rename-image-on-upload/#findComment-900681 Share on other sites More sharing options...
play_ Posted August 18, 2009 Share Posted August 18, 2009 You need to take the extension and then put it back at the md5'd string. <?php //function to upload photo function upload_photo() { // find ext $ext = end( explode('.', $_FILES['thefile']['name']) ); //set the file location $tempname = 'C:/Users/Pamela/Desktop/put_here/'.md5($_FILES['thefile']['name']) . $ext; //try to move the uploaded file if(move_uploaded_file($_FILES['thefile']['tmp_name'], $tempname)) { print '<p> Your file has been uploaded. </p>'; } else { print '<p> Your file was NOT uploaded. </p>'; print ($_FILES['thefile']['error']); } } ?> This would however, make things a bit harder when retrieving the files/file names another easy alternative is to replace the filename with the current timestamp Link to comment https://forums.phpfreaks.com/topic/170780-solved-rename-image-on-upload/#findComment-900688 Share on other sites More sharing options...
alemapo Posted August 18, 2009 Author Share Posted August 18, 2009 Thanks play_. This solution worked. I just had to add in the "." as below: $tempname = 'C:/Users/Pamela/Desktop/put_here/'.md5($_FILES['thefile']['name']) . '.' .$ext; I don't think I will have a problem retrieving them since the names will be stored in a database - it doesn't really matter what they are named. Also, using the timestamp is a little risky since it is a multi-user system and will allow for multiple image uploads at one time it is quite feasible to have duplicates (which would actually write over another image). I think this will work for me. Thanks so much for your help! Link to comment https://forums.phpfreaks.com/topic/170780-solved-rename-image-on-upload/#findComment-900692 Share on other sites More sharing options...
play_ Posted August 18, 2009 Share Posted August 18, 2009 No prob. for the timestamp, it could be the timestamp, plus a randomly generated # between 0 - 2000 Keep in mind also that there could be duplicate names with md5 hashed strings as well. if user 'john doe' uploads a file called hello.jpg, and 'jane smith' also uploads a file called hello.jpg, jane's will replace john's. so keep this in mind if you're storing all uploads in the same dir anyways, hope that helped. =] Link to comment https://forums.phpfreaks.com/topic/170780-solved-rename-image-on-upload/#findComment-900873 Share on other sites More sharing options...
alemapo Posted August 19, 2009 Author Share Posted August 19, 2009 play_ you said: "Keep in mind also that there could be duplicate names with md5 hashed strings as well." I thought of that after I sent the last reply. I changed it to use the md5 hashed string plus the timestamp. Hopefully that will be good enough! Thanks again! Link to comment https://forums.phpfreaks.com/topic/170780-solved-rename-image-on-upload/#findComment-901612 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.