Olubunmi
Olubunmi

Reputation: 91

Uploading two images in php at the same time

I have two images that i want to upload to the server. If both file are set in the html, then the first query should execute, if image1 is selected and image2 is not the second query shoud execute, if image1 is not selected and inage2 is selected then third query should execute, if no image is selected in both image1 and image2, then query 4 should execute.

Note that I also have three textboxes that will be updated irrespective of whether image1 or image2 are selected or not.

The problem I'm having is that

When I do not choose any file in both image1 nad image2, the first code executes there by inserting an emoty value of image 1 and image2, which is nto meant to be.

Is there a cleaner and more efficient way of reducing the code duplication.

if (isset($_FILES['newsthumb']) && isset($_FILES['newsmain'])) {

  $title            = mysql_real_escape_string(trim($_POST['title']));
  $body             = mysql_real_escape_string(trim($_POST['body']));
  $mainimage_title  = mysql_real_escape_string(trim($_POST['mainimage_title']));
  $thumbimage = $_FILES['newsthumb'];
  $mainimage  = $_FILES['newsmain'];

$unique = time();

$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 = mysql_real_escape_string($thumbname);
 $mainmane  = mysql_real_escape_string($mainmane);

 $uploaddir = "images/newsimage/";

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

 $newsarticle = "UPDATE news 
              SET title = '$title', 
                  body = '$body', 
                  mainimage_title = '$mainimage_title' , 
                  thumbnail = '$thumbname', 
                  mainimage = '$mainmane', 
                  editdate = NOW()
                  WHERE id = '$article_id'"; 
              mysql_query($newsarticle) or die (mysql_error());
            }

  if (!isset($_FILES['newsthumb']) && isset($_FILES['newsmain'])) {

  $title            = mysql_real_escape_string(trim($_POST['title']));
  $body             = mysql_real_escape_string(trim($_POST['body']));
  $mainimage_title  = mysql_real_escape_string(trim($_POST['mainimage_title']));
// Commented out since it is not defined anywhere in your original posted code.
// You will have to implement that in.
  $mainimage  = $_FILES['newsmain'];
  $unique = time();
  $mainmane   = strtolower($mainimage['name']);
  $mainmane   = preg_replace("/[^A-Z0-9._-]/i", "_", $mainmane);
  $mainmane  = mysql_real_escape_string($mainmane);
  $uploaddir = "images/newsimage/";
  $mainsuccess  = move_uploaded_file($mainimage["tmp_name"], $uploaddir.$mainmane);
  $newsarticle = "UPDATE news 
                  SET title = '$title', 
                      body = '$body', 
                      mainimage_title = '$mainimage_title' , 
                      mainimage = '$mainmane', 
                      editdate = NOW()
                      WHERE id = '$article_id'"; 
                  mysql_query($newsarticle) or die (mysql_error());
                } 

if (isset($_FILES['newsthumb']) && (!isset($_FILES['newsmain']))) {

  $title            = mysql_real_escape_string(trim($_POST['title']));
  $body             = mysql_real_escape_string(trim($_POST['body']));
  $mainimage_title  = mysql_real_escape_string(trim($_POST['mainimage_title']));
// Commented out since it is not defined anywhere in your original posted code.
// You will have to implement that in.
  $thumbimage = $_FILES['newsthumb'];
  $unique = time();
  $thumbname  = strtolower($thumbimage['name']); 
  $thumbname  = preg_replace("/[^A-Z0-9._-]/i", "_", $thumbname);
  $thumbname = mysql_real_escape_string($thumbname);
  $uploaddir = "images/newsimage/";
  $thumbsuccess = move_uploaded_file($thumbimage["tmp_name"], $uploaddir.$thumbname);


  $newsarticle = "UPDATE news 
                  SET title = '$title', 
                      body = '$body', 
                      mainimage_title = '$mainimage_title' , 
                      thumbnail = '$thumbname', 
                      editdate = NOW()
                      WHERE id = '$article_id'"; 
                  mysql_query($newsarticle) or die (mysql_error());
                }

if (!isset($_FILES['newsthumb']) && (!isset($_FILES['newsmain']))) {

  $title            = mysql_real_escape_string(trim($_POST['title']));
  $body             = mysql_real_escape_string(trim($_POST['body']));
  $mainimage_title  = mysql_real_escape_string(trim($_POST['mainimage_title']));    
  $newsarticle = "UPDATE news 
                  SET title = '$title', 
                      body = '$body', 
                      mainimage_title = '$mainimage_title' , 
                      editdate = NOW()
                      WHERE id = '$article_id'"; 
                  mysql_query($newsarticle) or die (mysql_error());
                }

Upvotes: 0

Views: 90

Answers (1)

user1603136
user1603136

Reputation:

Maybe by creating a function that get the file in parameter and to his stuffs. It would help without duplicate.

For checking which file is selected or no with :

if ($_FILES["file"]["error"] > 0)

More explanations :

function uploadASingleFile($file){ 
// Do your stuff here for the upload (moving the file, sql request etc.) 
}

And then, check the files if they are selected (using ["error"]) and call the function

if($_FILES["file1"]["error"] == 0){
uploadASingleFile($_FILES["file1"]); // Obviously, handle errors...
}

if($_FILES["file2"]["error"] == 0){
uploadASingleFile($_FILES["file2"]); // Obviously, handle errors...
}

The code is less duplicated and clear. Have I answered your question ?

Upvotes: 1

Related Questions