Jump to content

[SOLVED] Upload 2 images with one form


dtjester

Recommended Posts

Hi, I am pretty new to PHP and I am haveing trouble with this script I wrote. The object is to take form data and two images, upload the 2 images it 2 different dir. and take the paths to those images, and the other form data and insert into my MySQL table. Everything works, except I cannot find the images. The paths to the images are correct and they are being stored in the table, but the images aren't there. I don't get it.

 

Also, I am trying to add the date added to the table, but it just gets entered as 0000-00-00 in the table. Is my syntax correct?

 

Below is my script:

 


 

if ((($_FILES["img_thumb"]["type"] == "image/gif")
|| ($_FILES["img_thumb"]["type"] == "image/jpeg")
|| ($_FILES["img_thumb"]["type"] == "image/pjpeg"))
&& ($_FILES["img_thumb"]["size"] < 200000))
  
  {

if ($_FILES['img_thumb']['error'] > 0)
  {
  echo "Error: " . $_FILES['img_thumb']['error'] . "<br />";
  } else {
  echo "File was uploaded successfully!";
  }
  
    if (file_exists("images/favors/thumbs/" . $_FILES['img_thumb']['name']))
      {
      echo $_FILES['img_thumb']['name'] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES['img_thumb']['tmp_name'],
      "images/favors/thumbs/" . $_FILES['img_thumb']['name']);
      
      }
  
  }
  
   else {
  echo "Invalid file";
  }
  
$thumb_path = "images/favors/thumbs/" . $_FILES['img_thumb']['name'];
  
//*********end thumbnail upload************

//Upload Fullsize Image
  
if ((($_FILES["img_full"]["type"] == "image/gif")
|| ($_FILES["img_full"]["type"] == "image/jpeg")
|| ($_FILES["img_full"]["type"] == "image/pjpeg"))
&& ($_FILES["img_full"]["size"] < 200000))
  
  {

if ($_FILES['img_full']['error'] > 0)
  {
  echo "Error: " . $_FILES['img_full']['error'] . "<br />";
  } else {
  echo "File was uploaded successfully!";
  }
  
    if (file_exists("images/favors/" . $_FILES['img_full']['name']))
      {
      echo $_FILES['img_full']['name'] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES['img_full']['tmp_name'],
      "images/favors/" . $_FILES['img_full']['name']);
     
      }
  
  }
  
   else {
  echo "Invalid file";
  }

$full_path = "images/favors/" . $_FILES['img_full']['name'];

//**************End upload fullsize image*************

//add info to database

$db_name = '*****';
$hostname = '*****';
$username = '*****';
$password = '*****';
$tablename = '*****';



$connection = @mysql_connect($hostname, $username, $password)
	or die(mysql_error());

$db = @mysql_select_db($db_name, $connection) or die(mysql_error());

$sql = "INSERT INTO $tablename VALUES ('$_POST[id]', '$_POST[name]', '$_POST[desc]', '$_POST[cost]', '$_POST[category]', '$_POST[style]', 'NOW()', '$thumb_path', '$full_path')";

$result = mysql_query($sql,$connection) or die(mysql_error());

if ($result) {
$msg = "New Favor has been added!";
}



echo $msg; 

?>

Link to comment
https://forums.phpfreaks.com/topic/102461-solved-upload-2-images-with-one-form/
Share on other sites

Try using basename() for the move_uploaded_file AND use NOW(), not 'NOW()' the later is a string and won't get evaluated as a function.

 

if ((($_FILES["img_thumb"]["type"] == "image/gif")
|| ($_FILES["img_thumb"]["type"] == "image/jpeg")
|| ($_FILES["img_thumb"]["type"] == "image/pjpeg"))
&& ($_FILES["img_thumb"]["size"] < 200000))
  
  {

if ($_FILES['img_thumb']['error'] > 0)
  {
  echo "Error: " . $_FILES['img_thumb']['error'] . "<br />";
  } else {
  echo "File was uploaded successfully!";
  }
  
    if (file_exists("images/favors/thumbs/" . $_FILES['img_thumb']['name']))
      {
      echo $_FILES['img_thumb']['name'] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES['img_thumb']['tmp_name'],
      "images/favors/thumbs/" . basename($_FILES['img_thumb']['name']));
      
      }
  
  }
  
   else {
  echo "Invalid file";
  }
  
$thumb_path = "images/favors/thumbs/" . $_FILES['img_thumb']['name'];
  
//*********end thumbnail upload************

//Upload Fullsize Image
  
if ((($_FILES["img_full"]["type"] == "image/gif")
|| ($_FILES["img_full"]["type"] == "image/jpeg")
|| ($_FILES["img_full"]["type"] == "image/pjpeg"))
&& ($_FILES["img_full"]["size"] < 200000))
  
  {

if ($_FILES['img_full']['error'] > 0)
  {
  echo "Error: " . $_FILES['img_full']['error'] . "<br />";
  } else {
  echo "File was uploaded successfully!";
  }
  
    if (file_exists("images/favors/" . $_FILES['img_full']['name']))
      {
      echo $_FILES['img_full']['name'] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES['img_full']['tmp_name'],
      "images/favors/" . basename($_FILES['img_full']['name']));
     
      }
  
  }
  
   else {
  echo "Invalid file";
  }

$full_path = "images/favors/" . $_FILES['img_full']['name'];

//**************End upload fullsize image*************

//add info to database

$db_name = '*****';
$hostname = '*****';
$username = '*****';
$password = '*****';
$tablename = '*****';



$connection = @mysql_connect($hostname, $username, $password)
	or die(mysql_error());

$db = @mysql_select_db($db_name, $connection) or die(mysql_error());

$sql = "INSERT INTO $tablename VALUES ('$_POST[id]', '$_POST[name]', '$_POST[desc]', '$_POST[cost]', '$_POST[category]', '$_POST[style]', NOW(), '$thumb_path', '$full_path')";

$result = mysql_query($sql,$connection) or die(mysql_error());

if ($result) {
$msg = "New Favor has been added!";
}



echo $msg; 

?>

 

I'd say its a rights issue. You dont have the sufficient rights to mvoe the file into the specified folder. You might want to consult your service provider to check that the server has the rights for that folder. ( that is, if you cant check your self :) )

 

-Nikki

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.