PHP/MySQL - Image Upload and Rename issues

Posted on 2010-01-06
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

    Put your unique ID into lines 1-5.  when the copy statements in 7-11 execute, they will copy w/ the new names.
    LVL 107

    Expert Comment

    by:Ray Paseur
    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
    LVL 8

    Expert Comment


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

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

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

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
    The viewer will learn how to dynamically set the form action using jQuery.
    Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now