phpSensei Posted July 28, 2007 Share Posted July 28, 2007 I will explain the best way I can... OKAY! 1. User uploads a file from a simple form. 2. The file is moved to a directory called upload, and the filetype is md5 hashed. Filename is basically the name of the image you uploaded for example "Pic.gif", and it is Hashed to something like "29372139991008831238"... Now I have a list of latest images uploaded, and when you click on them you pass the ID from the URL and it sends you to a page called viewfile... The id is retreived and I used a mysql_query "SELECT * FROM uploads WHERE id='$id' ", and everything is fine. Now to show the user the image you need two things - 1.Filename 2.Filetype <img src="upload/' . $row['filename'] . '.' . $row['filetype'] . '"> It would look like this <img src="upload/image.gif"> Image is the Filename, and GIF is the file type. Although as I mentioned earlier, the filename does not equal the same filename as in the upload directory because I hashed it. Filename is hashed with a bunch of numbers, while the real file is left with the original filename... How can I convert or do something so that the file name changes with the same hashed one.. I really hope you understood lol. Quote Link to comment Share on other sites More sharing options...
Fadion Posted July 28, 2007 Share Posted July 28, 2007 There's no way u can decrypt it. First i thought u were going to hash only the file extension and like that u can do it comparing the hash with md5('gif'), md5('jpg') etc. Dont know any way to handle this and i cant think of why u are hashing the filename. Quote Link to comment Share on other sites More sharing options...
dewey_witt Posted July 28, 2007 Share Posted July 28, 2007 If statment With all the numbers in it =filename. Lotta keeping track there. Quote Link to comment Share on other sites More sharing options...
quandrie Posted July 28, 2007 Share Posted July 28, 2007 I guess you hash them because you want to make sure they're unique. You could hash just the name (without extension), add ".jpg" (or whatever the extension of the file turns out to be) to it, move the file using the new filename, and insert that same filename into the database. So you'll have 29372139991008831238.jpg both in the database and directory. Quote Link to comment Share on other sites More sharing options...
Fadion Posted July 28, 2007 Share Posted July 28, 2007 For the files to be unique, he can add a 5 digits random number to the filename (ex. photo-33425.gif) and insert the same in the database. It would be a more readable technique. Quote Link to comment Share on other sites More sharing options...
ss32 Posted July 28, 2007 Share Posted July 28, 2007 my personal favorite for generating unique filenames... its kinda long but it works well $nf = $filename . "_" . md5($file) . time() . $fileExtension; Quote Link to comment Share on other sites More sharing options...
quandrie Posted July 28, 2007 Share Posted July 28, 2007 I don't think his problem is how to randomize the name, but how to have the filename of the uploaded file the same as the filename in the database. There are many ways to get a randomized string. To recoup: get the extension of the uploaded file randomize the filename (however which way) $new_filename = randomized_filename.extension move_uploaded_file($new_filename, $path) insert into table(file) values('$new_filename') Quote Link to comment Share on other sites More sharing options...
phpSensei Posted July 28, 2007 Author Share Posted July 28, 2007 Its What quandrie said, but can you be a little more specific on how to do this...? Thankyou everyone for taking your time to help me out. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted July 28, 2007 Share Posted July 28, 2007 why not just add a field to the db called "original file name" ? Quote Link to comment Share on other sites More sharing options...
phpSensei Posted July 28, 2007 Author Share Posted July 28, 2007 I need it for security reasons... Quote Link to comment Share on other sites More sharing options...
quandrie Posted July 28, 2007 Share Posted July 28, 2007 Something like this (I'm assuming that the filename does not have dots in it, otherwise you'll have to split things in a different way, using the last occurrence of a dot in the string) : $array = explode('.', $_POST['files']['pic']['name']); //Split the filename $ext = $array[1]; $name = md5($array[0]); $new_name = $name.'.'.$ext; move_uploaded_file($new_name, $path); mysql_query('INSERT INTO table (pic) VALUES("$new_name")'); Quote Link to comment Share on other sites More sharing options...
dewey_witt Posted November 9, 2007 Share Posted November 9, 2007 Or if he wanted to definatly preserve a unique Identity $name = uniqid(md5($array[0]_)); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.