Arbitrary php file name

Well, using the snippet I found on Google to upload the downloaded user image and put it in your directory in the Content section

But Im worried about duplicates, so I was going to load the image as a random number

Well here is my code, which you probably understand what it is going to do through it anyway

<label for="file">Profile Pic:</label> <input type="file" name="ProfilePic" id="ProfilePic" /><br /> <input type="submit" name="submit" value="Submit" /> $ProfilePicName = $_FILES["ProfilePic"]["name"]; $ProfilePicType = $_FILES["ProfilePic"]["type"]; $ProfilePicSize = $_FILES["ProfilePic"]["size"]; $ProfilePicTemp = $_FILES["ProfilePic"]["tmp_name"]; $ProfilePicError = $_FILES["ProfilePic"]["error"]; $RandomAccountNumber = mt_rand(1, 99999); echo $RandomAccountNumber; move_uploaded_file($ProfilePicTemp, "Content/".$RandomAccountNumber.$ProfilePicType); 

And then, basically, after that Im Im trying to get him to put this random number in my database

Someone gave me a new snippet that looks like it will do what I want, but now the file does not reach my directory

 $RandomAccountNumber = uniqid(); echo $RandomAccountNumber; move_uploaded_file($ProfilePicName,"Content/".$RandomAccountNumber); 
+7
source share
6 answers

try using php uniqid method to generate the unique id you need

http://php.net/manual/en/function.uniqid.php

 $RandomAccountNumber = uniqid(); move_uploaded_file($ProfilePicTemp, "Content/" . $RandomAccountNumber); 
+17
source

When I upload images, I usually save it as sha1() the image content ( sha1_file() ). Thus, you will get two birds with one stone: you will never (if you want, go to the nearest lottery) get duplicate file names, and you will prevent duplicate images (since duplicated images will have the same checksum).

Then you have a database for sorting which image is and displaying them correctly to the user.

+5
source

This is what I use when loading images: a combination of session_id () , time () and a random string:

 $rand = genRandomString(); $final_filename = $rand."_".session_id()."_".time(); function genRandomString() { $length = 5; $characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWZYZ"; $real_string_length = strlen($characters) ; $string="id"; for ($p = 0; $p < $length; $p++) { $string .= $characters[mt_rand(0, $real_string_length-1)]; } return strtolower($string); } 

I hope this help.

+4
source

random != unique

No matter which method you use to create a β€œrandom” file name, you probably want to do this to avoid collisions.

 $path = '/path/to/directory/'; do { $filename = some_function(); } while( file_exists($path.$filename) ); 

This is not very necessary, but if you are just looking for peace of mind in the event of a clash of names in one million, then these additional lines will do the trick.

+3
source

One of my favorite Coding Horror articles explains why this approach is deeper than it sounds, and you should use something like uniqid instead of mt_rand(1, 99999); ...

+1
source

All Articles