Olubunmi
Olubunmi

Reputation: 91

Uploading multiple images at the same time

I am trying to upload two images at the same time using php. When I run the code below nothing shows as error and images are not uploaded to the folder I moved them to. The first is a thumbnail image while the second is the actual image.

I have added error_reporting(E_ALL); ini_set('display_errors', '1'); and no error is shown on the screen.

<?php  
error_reporting(E_ALL); ini_set('display_errors', '1');
if(isset($_POST['submit'])){   
if (isset($_FILES['newsthumb']) && isset($_FILES['newsmain'])) {


  $allowedExts = array("jpg", "jpeg", "gif", "png");

  $thumbimage = $_FILES['newsthumb'];
  $mainimage  = $_FILES['newsmain'];

  $thumbname  = strtolower($thumbimage['name']); 
  $mainmane   = strtolower($mainimage['name']);

  $thumbname  = preg_replace("/[^A-Z0-9._-]/i", "_", $thumbname);
  $mainmane   = preg_replace("/[^A-Z0-9._-]/i", "_", $mainmane);

  $thumbname = $thumbname.uniqid();
  $mainmane  = $thumbname.uniqid();

  if (($thumbimage['size'] > 350000) || ($mainmane['size'] > 350000)) { 
   $error[] = "One/Both of the files are too large.<br>"; 
   }

  $uploaddir = "images/newsimage/";

  $thumbsuccess = move_uploaded_file($thumbimage["tmp_name"], $uploaddir.$thumbname);
  $mainsuccess  = move_uploaded_file($mainimage["tmp_name"], $uploaddir.$mainmane );
  }
 }

 ?> 

Here is the HTML form I'm using :

        <form enctype="multipart/form-data" action="<?php echo htmlentities($_SERVER['SCRIPT_NAME']) ?>" method="post">
          <input type="hidden" name="MAX_FILE_SIZE" value="9000000">
          <h3>Thumbnail News Image: </h3>
          <input name='newsthumb' type="file" class='Input_file' />
          <h3>Full Image:></h3>
          <input name='newsmain' type="file" class='Input_file' />
          <input type="submit" name="commit" value="send">
        </form>

Upvotes: 0

Views: 1512

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74216

I found a few problems with your code.

One of which is this: <input type="submit" name="commit" value="send">

You checked if if(isset($_POST['submit'])){ when your submit button was called commit; FAIL.

Your if (($thumbimage['size'] > 350000) || ($mainmane['size'] > 350000)) { is wrong.

It needs to be like this:

if (($_FILES["newsthumb"]["size"] > 350000) || ($_FILES["newsmain"]["size"] > 350000)) {

I replaced $error[] = "One or Both of the files are too large.<br>"; with:

echo "One or Both of the files are too large.<br>"; 

exit;

The $error variable wasn't mentioned anywhere else and it didn't do anything, so I replaced it with an echo and an exit; to stop execution and not upload anything to the server.

SPECIAL NOTES:
I also added an else as well as assigned uniqid(); to $uniqid variable, so that both uploaded files will have the same number at the beginning.

I added an echo'ed "Success" if files were successfully uploaded.

Here is working/tested code:

<form enctype="multipart/form-data" action="<?php echo htmlentities($_SERVER['SCRIPT_NAME']) ?>" method="post">
    <input type="hidden" name="MAX_FILE_SIZE" value="9000000">
    <h3>Thumbnail News Image: </h3>
    <input name="newsthumb" type="file" class='Input_file' />
    <h3>Full Image:</h3>
    <input name="newsmain" type="file" class='Input_file' />
    <input type="submit" name="submit" value="send">
</form>

<?php

error_reporting(E_ALL); ini_set('display_errors', '1');
if(isset($_POST['submit'])){   
if (isset($_FILES['newsthumb']) && isset($_FILES['newsmain'])) {

// Commented out since it is not defined anywhere in your original posted code.
// You will have to implement that in.
//  $allowedExts = array("jpg", "jpeg", "gif", "png");

  $thumbimage = $_FILES['newsthumb'];
  $mainimage  = $_FILES['newsmain'];

  $thumbname  = strtolower($thumbimage['name']); 
  $mainmane   = strtolower($mainimage['name']);

  $thumbname  = preg_replace("/[^A-Z0-9._-]/i", "_", $thumbname);
  $mainmane   = preg_replace("/[^A-Z0-9._-]/i", "_", $mainmane);


$uniqid = uniqid();

  $thumbname = $uniqid."_".$thumbname;
  $mainmane  = $uniqid."_".$mainmane;


if (($_FILES["newsthumb"]["size"] > 350000) || ($_FILES["newsmain"]["size"] > 350000)) { 

// You can also use "die", just not with "exit;".
// die("SORRY");

echo "One or Both of the files are too large.<br>"; 

exit;
   }

else{

  $uploaddir = "images/newsimage/";

$thumbsuccess = move_uploaded_file($thumbimage["tmp_name"], $uploaddir.$thumbname);
$mainsuccess  = move_uploaded_file($mainimage["tmp_name"], $uploaddir.$mainmane);

echo "Success!!!";

  }
 }

} // else ending bracket
?>

Upvotes: 1

John
John

Reputation: 155

this should solve your problem...

$thumbsuccess = move_uploaded_file($thumbimage["tmp_name"]['tmp_name'], $uploaddir.$thumbname);   
$mainsuccess  = move_uploaded_file($mainimage["tmp_name"]['tmp_name'], $uploaddir.$mainmane );

Upvotes: 1

Related Questions