Jump to content

PHP + forms?


Kenny Pollock

Recommended Posts

Right now, I have an upload form with 5 fields (5 uploads at once):

 

		$tmp_name = $_FILES['pictures']["tmp_name"][$key];
	$name = $_FILES['pictures']["name"][$key];
	move_uploaded_file( $tmp_name, "$garage_dir/$name" );

 

With that code, the filename stays the same when it's uploaded. Now I want to have it renamed to image1, image2, image3 before it's uploaded.

 

Any help is GREATLY appreciated!

Link to comment
https://forums.phpfreaks.com/topic/63031-php-forms/
Share on other sites

You'll probably want to do some string processing on the $name variable, Not sure but try:

$tmp_name = $_FILES['pictures']["tmp_name"][$key];
$name = $_FILES['pictures']["name"][$key];

list($filename, $ext) = explode('.', $name);

$name = $filename . $key . $ext;

move_uploaded_file( $tmp_name, "$garage_dir/$name" );

Link to comment
https://forums.phpfreaks.com/topic/63031-php-forms/#findComment-313905
Share on other sites

Well, make sure you have your form set up so that the name of each is "pictures[]", that way you have an array of files to upload.  Then what you need to do is something like this:

 

$count = 0;

foreach ($_FILES["pictures"]["error"] as $key => $error) {

 if ($error == UPLOAD_ERR_OK) {

   $tmp_name = $_FILES["pictures"]["tmp_name"][$key];

   $name = "image".$count;

   move_uploaded_file($tmp_name, "data/$name");

   $count++;

   }

 }

Link to comment
https://forums.phpfreaks.com/topic/63031-php-forms/#findComment-313908
Share on other sites

Thank you, hit the nail on the head!

 

Now, right below that code I have:

	mysql_query( "UPDATE garage_vehicles SET year = '" . $year . "', make = '" . $make . "', model = '" . $model . "', color = '" . $color . "', exterior_mods = '" . $exterior_mods . "', interior_mods = '" . $interior_mods . "', engine_mods = '" . $engine_mods . "', suspension_mods = '" . $suspension_mods . "', electronics = '" . $electronics . "', awards = '" . $awards . "', image1 = '" . $_FILES['pictures']['name'][0] . "', image2 = '" . $_FILES['pictures']['name'][1] . "', image3 = '" . $_FILES['pictures']['name'][2] . "', image4 = '" . $_FILES['pictures']['name'][3] . "', image5 = '" . $_FILES['pictures']['name'][4] . "' WHERE garage_id = '" . $_POST['garage_id'] . "'", $link );

 

What do I replace $_FILES... with?

Link to comment
https://forums.phpfreaks.com/topic/63031-php-forms/#findComment-313933
Share on other sites

Try:

 

$count = 0;
$names = array();
foreach ($_FILES["pictures"]["error"] as $key => $error) {
  if ($error == UPLOAD_ERR_OK) {
    $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
    $ext = substr(strtolower($tmp_name), strrpos($tmp_name, ".")+1, strlen($tmp_name));
    array_push($names, "image".$count.$ext);
    move_uploaded_file($tmp_name, "data/$name");
    $count++;
    }
  }

$query = "
UPDATE
          `garage_vehicles`
        SET
          `year` = '".$year."',
          `make` = '".$make."',
          `model` = '".$model."',
          `color` = '".$color."',
          `exterior_mods` = '".$exterior_mods."',
          `interior_mods` = '".$interior_mods."',
          `engine_mods` = '".$engine_mods."',
          `suspension_mods` = '".$suspension_mods."',
          `electronics` = '".$electronics."',";
          `awards` = '".$awards."',
          `image1` = '".$names[0]."',
          `image2` = '".$names[1]."',
          `image3` = '".$names[2]."',
          `image4` = '".$names[3]."',
          `image5` = '".$names[4]."'
WHERE
          `garage_id` = '".$_POST['garage_id']."'";
if(mysql_query($query)) {
  echo "Record successfully updated";
  } else {
  echo "Error, the record was not updated.";
  }

 

I also cleaned up your query to make it easier to read.

Link to comment
https://forums.phpfreaks.com/topic/63031-php-forms/#findComment-314807
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.