Insert image/file to database with random name

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Insert image/file to database with random name

Post by simonmlewis »

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:

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

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(); }
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
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Insert image/file to database with random name

Post by simonmlewis »

Hi

For anyone who has read this, or has the same problem - I've cracked it.

Code: Select all

$photo=$_POST['photo'];
 
$target = $_SERVER['DOCUMENT_ROOT']."/dealers/images/productphotos/";
srand(time());
$random = (rand()%99999999);
$pic=($_FILES['photo']['name']);
$newname="$random"."$pic";
 
$target = $target . $newname;
 
include "dbconn.php";
 
mysql_query("INSERT INTO dxvehicles
(userid, make, title, description, video, price, photoprimary) VALUES 
('$userid', '$make', '$title', '$description', '$video', '$price', '$newname')");
Basically I used the original code, plus the method I found to add something to the end of it.
The...

Code: Select all

$pic=($_FILES['photo']['name']);
...part is where it finds the file name, so I worked out how to use that and combine it with the random numbers.

Obviously when someone is uploading photographs, two files could so easily have the same filename and balls the whole system up for everyone.

Thanks to anyone who has opened this with a view to solving it.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply