Learn how to a build a cloud-first strategyRegister Now


PHP/MySQL - Image Upload and Rename issues

Posted on 2010-01-06
Medium Priority
Last Modified: 2013-12-20

I am uploading 5 image names to a database, firstly i copy the files to the new location, then the image name is stored in the db. I was wondering if there was a way to rename the file to give it unique id. by rename i mean as the file is copied to the new location the name is change and the changed file name is stored in the db.

any help would be appricated.

the code below shows the copy process and insert code.
$path1= "images/cars/".$_FILES['images']['name'][0];
	$path2= "images/cars/".$_FILES['images']['name'][1];
	$path3= "images/cars/".$_FILES['images']['name'][2];
	$path4= "images/cars/".$_FILES['images']['name'][3];
	$path5= "images/cars/".$_FILES['images']['name'][4];
	copy($_FILES['images']['tmp_name'][0], $path1);
	copy($_FILES['images']['tmp_name'][1], $path2);
	copy($_FILES['images']['tmp_name'][2], $path3);
	copy($_FILES['images']['tmp_name'][3], $path4);
	copy($_FILES['images']['tmp_name'][4], $path5);

	$description = mysql_prep($_POST['editor1']);
	$value = mysql_prep($_POST['value']);
	$status = mysql_prep($_POST['status']);
	$year = mysql_prep($_POST['year']);
	$display = mysql_prep($_POST['display']);
	$special_offer = mysql_prep($_POST['special_offer']);
	$car_type = mysql_prep($_POST['car_type']);
	$small_pic=	$_FILES['images']['name'][0];
	$large_pic1= $_FILES['images']['name'][1];
	$large_pic2= $_FILES['images']['name'][2];
	$large_pic3= $_FILES['images']['name'][3];
	$large_pic4= $_FILES['images']['name'][4];

    $query = "INSERT INTO cars (description, value, status, year, published, special_offer, car_type, small_pic, large_pic1, large_pic2, large_pic3, large_pic4) VALUES ('{$description}', '{$value}', '{$status}', '{$year}', '".(isset($display)?1:0)."', '".(isset($special_offer)?1:0)."', '{$car_type}', '{$small_pic}', '{$large_pic1}', '{$large_pic2}', '{$large_pic3}', '{$large_pic4}');";

Open in new window

Question by:davidcairns90
LVL 32

Accepted Solution

Daniel Wilson earned 2000 total points
ID: 26195536
Put your unique ID into lines 1-5.  when the copy statements in 7-11 execute, they will copy w/ the new names.
LVL 111

Expert Comment

by:Ray Paseur
ID: 26196444
You can get pretty unique names from something like this
$str = md5(rand());

That will give a 32-character string that is useful in a file name, but not very meaningful.  If you have clients who log in, you might consider using their client ids and a DATETIME string in the name.  Might be able to keep a visual track of the file names that way.

HTH, ~Ray

Expert Comment

ID: 26197305

$newFilename = $randFilename.".".$oldFileExt;

move_uploaded_file($_FILES['userfile']['tmp_name'], $filePath.$newFilename);

copy($_FILES['userfile']['tmp_name'], $filePath.$newFilename);

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses
Course of the Month20 days, 15 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question