Jump to content

Really Complicated...


phpSensei

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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')

Link to comment
Share on other sites

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")');

 

 

Link to comment
Share on other sites

  • 3 months later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.