1

Most problems I see are with ONE image input. But what if you have 2 or 3 or more?

My script for one image input worked but I am trying to have one single-image input and one multiple image input in my form. I'm testing out having 2 single-image inputs on my form but not working.

Here is the HTML for one single-image input (assume it is inside a < form > tag):

  <input type="hidden" name="size" value="350000"/>
  <input type="file" name="schoollogo"/><br/>

The PHP:

  $target = "images/logo/";
  $target = $target . basename( $_FILES['schoollogo']['name']);

  $schoollogo = $_FILES['schoollogo']['name'];

  //Writes the information to the database

  mysql_query("INSERT INTO Colleges (schoollogo) VALUES ('$schoollogo')") or die(mysql_error()) ;

  //Writes the logo to the server
  if (move_uploaded_file($_FILES['schoollogo']['tmp_name'], $target)) {
      //Tells you if its all ok
      echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
  } else {
      //Gives and error if its not
      echo "Sorry, there was a problem uploading your file.";
  }

For the second single-image input, I just added a second input code...

 <input type="hidden" name="size" value="350000"/>
 <input type="file" name="otherimage"/><br/>

And then added the variable to the PHP:

  $targetotherimage = "images/otherimage/";
  $targetotherimage = $targetotherimage . basename( $_FILES['otherimage']['name']);

  mysql_query("INSERT INTO Colleges (schoollogo,otherimage) VALUES ('$schoollogo','$otherimage')") or die(mysql_error()) ;

  //Writes the logo to the server
  if (move_uploaded_file($_FILES['schoollogo']['tmp_name'], $target)) && (move_uploaded_file($_FILES['otherimage']['tmp_name'], $targetotherimage)) {

but when adding that code into the PHP, the code does not work. No error messages or anything. The page is blank.

By the way, here is my other question where I am attempting to add a multiple-image upload input into my form with an already-working single-image upload input.

Community
  • 1
  • 1
thomas
  • 163
  • 2
  • 13
  • Possible duplicate of [Reference - What does this error mean in PHP? - Nothing is seen. The page is empty and white.](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12772851#12772851) – CBroe Jun 08 '16 at 07:57

2 Answers2

1

Wrong closing of if condition.

  if(move_uploaded_file($_FILES['schoollogo']['tmp_name'], $target)) && (move_uploaded_file($_FILES['otherimage']['tmp_name'], $targetotherimage)) {
                                                                  ^^^^// In your code if condition close here

It is used as

  if(move_uploaded_file($_FILES['schoollogo']['tmp_name'], $target) 

&& (move_uploaded_file($_FILES['otherimage']['tmp_name'],  $targetotherimage)))//if condition close here 
{
// rest of code

}
Saty
  • 22,443
  • 7
  • 33
  • 51
  • I have, but nothing changed. No errors. Still blank page after I made the changes you suggested. – thomas Jun 08 '16 at 07:42
  • use `ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);` to check error on page – Saty Jun 08 '16 at 07:47
  • Works, thanks! Now that I have 2 single-file upload inputs working, I gotta figure out how to convert the second input into a multiple-file upload input. – thomas Jun 08 '16 at 11:02
  • The other answer was good but your answer helped solve my code directly as is without changing the code as a whole. – thomas Jun 08 '16 at 11:17
1

This will work for you, replace your form with this

<form method="post" enctype="multipart/form-data" action="upload.php">
      <input type="file" name="file['schoollogo']" >
      <input type="file" name="file['otherimage']" >
      <input type="submit" value="submit">
</form>

You will get your files in this format:

Array
(
    [file] => Array
        (
            [name] => Array
                (
                    ['schoollogo'] => 1465386599_Location_Outline-36.png
                    ['otherimage'] => STONE_Website_v7E.jpg
                )

            [type] => Array
                (
                    ['schoollogo'] => image/png
                    ['otherimage'] => image/jpeg
                )
        ) 
) 

And in upload.php file

<?php 
if (isset($_FILES['file'])) 
{
     $schoollogo=$_FILES['file']['name']['schoollogo'];
     $target = "images/logo/".basename($schoollogo);;

     $otherimage = $_FILES['file']['name']['otherimage'];
     $targetotherimage = "images/otherimage/".basename($otherimage);

      $tmp_name_school = $_FILES['file']['tmp_name']['schoollogo'];
      $tmp_name_other = $_FILES['file']['tmp_name']['otherimage'];

      if ($tmp_name_school != "" && $tmp_name_other !== "")
      {
        //Upload the file 
        if(move_uploaded_file($tmp_name_school,$target) && move_uploaded_file($tmp_name_other, $targetotherimage)) 
        {
          mysql_query("INSERT INTO Colleges (schoollogo,otherimage) VALUES ('$schoollogo','$otherimage')") or die(mysql_error()) ;
          echo'Done!';
        }
        else
        {
            echo "Not Moved";
        }
      }
}   
?>
Manjeet Barnala
  • 2,975
  • 1
  • 10
  • 20