0

我有两张图片要上传到服务器。如果在 html 中设置了两个文件,则应执行第一个查询,如果选择了 image1 且 image2 不是第二个查询,如果未选择 image1 并且选择了 inage2,则应执行第三个查询,如果没有选择图像在 image1 和 image2 中,查询 4 ​​应该执行。

请注意,我还有三个文本框,无论是否选择了 image1 或 image2,它们都会更新。

我遇到的问题是

当我在 image1 和 image2 中都没有选择任何文件时,第一个代码通过插入 image 1 和 image2 的 emoty 值来执行,这是不应该的。

是否有一种更清洁、更有效的方法来减少代码重复。

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());
                }
4

1 回答 1

1

也许通过创建一个函数来获取参数中的文件和他的东西。没有重复会有所帮助。

用于检查选择或不选择哪个文件:

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

更多解释:

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

然后,检查文件是否被选中(使用 ["error"])并调用函数

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

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

代码较少重复和清晰。我回答你的问题了吗?

于 2013-07-28T22:58:01.907 回答