Insert image/file to database with random name
Posted: Sat Jun 27, 2009 5:13 pm
Hi
I need to make any files uploaded to the web site to be converted to a random name.
I know how to do part of this:
... but not all. I cannot gather the name of the file itself from the previous page and add the "$random" on the end of it.
It will generate the 'numbers' but all I get is either 234234 or 2342424_ if I stick an underscore in there too.
I also tried this method which is the original I found:
Basically, how do you get hold of that filename, ie... if you browse to your file the content of the File field has the full c:\ string in it.
All I need is how to get the filename, and I think I'm there.
Thanks
Simon
I need to make any files uploaded to the web site to be converted to a random name.
I know how to do part of this:
Code: Select all
$photo=$_POST['photo'];
srand(time());
$random = (rand()%99999999);
$_FILES['photo']['$random'];
$newname="$random"."$photo";
if($filename !="")
{
copy ("$file", "images/productphotos/$newname")
or die ("could not copy file");
}
//This gets all the other information from the form
include "dbconn.php";
mysql_query("INSERT INTO vehicles
(userid, make, title, description, video, price, photoprimary) VALUES
('$userid', '$make', '$title', '$description', '$video', '$price', '$newname')");
It will generate the 'numbers' but all I get is either 234234 or 2342424_ if I stick an underscore in there too.
I also tried this method which is the original I found:
Code: Select all
// Generate Random Number
srand(time());
$random = (rand()%99999999);
// Add Random Number to First of File Name
$newName="$random"."_"."$file_name";
// Copy to Fold Append New File Name
if($file_name !="")
{
copy ("$file", "images_upload/$newName")
or die ("could not copy file");
}
else { die(); }All I need is how to get the filename, and I think I'm there.
Thanks
Simon