同时上传多个图像

时间:2013-07-23 10:48:34

标签: php file-upload

我正在尝试使用php同时上传两张图片。当我运行下面的代码没有显示为错误和图像没有上传到我移动到的文件夹。第一个是缩略图,第二个是实际图像。

我添加了error_reporting(E_ALL); ini_set('display_errors', '1');,屏幕上没有显示错误。

<?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 );
  }
 }

 ?> 

以下是我正在使用的HTML表单:

        <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>

2 个答案:

答案 0 :(得分:1)

这应该可以解决你的问题...

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

答案 1 :(得分:1)

我发现您的代码存在一些问题。

其中之一是:<input type="submit" name="commit" value="send">

您在调用提交按钮if(isset($_POST['submit'])){时检查了commit; FAIL。

您的if (($thumbimage['size'] > 350000) || ($mainmane['size'] > 350000)) { 是错的。

它必须是这样的:

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

我替换了$error[] = "One or Both of the files are too large.<br>"; 用:

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

exit;

$error变量在其他任何地方都没有提及,它没有做任何事情,所以我用echoexit;替换它以停止执行而不上传任何内容服务器。

特别说明:
我还添加了else以及 uniqid(); $uniqid变量,以便两个上传的文件在开头都具有相同的数字。

如果文件成功上传,我添加了一个echo'ed“Success”。

以下是工作/测试代码:

<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
?>