Skip to content
Advertisement

php random image file name

Okay im using a snippet I found on google to take a users uploaded image and put it in my directory under Content

But Im worried about duplicates so I was going have it upload the image as a Random number

well here is my code you can probably understand what im going for through it anyways

<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 basicly after all this Im going try to get it to put that random number in my database

Someone gave me a new snippet that looks like it will do what I want but now the file isnt making it all the way to my directory

$RandomAccountNumber = uniqid();
echo $RandomAccountNumber;

move_uploaded_file($ProfilePicName,"Content/".$RandomAccountNumber);

Advertisement

Answer

try using the 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);
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement