0

我正在尝试使用 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>
4

2 回答 2

1

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

$thumbsuccess = move_uploaded_file($thumbimage["tmp_name"]['tmp_name'], $uploaddir.$thumbname);   
$mainsuccess  = move_uploaded_file($mainimage["tmp_name"]['tmp_name'], $uploaddir.$mainmane );
于 2013-07-23T11:11:20.337 回答
1

我发现你的代码有一些问题。

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

您检查了是否if(isset($_POST['submit'])){调用了提交按钮commit;失败。

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变量在其他任何地方都没有提及,它没有做任何事情,所以我用 anecho和 an替换它exit;以停止执行并且不将任何内容上传到服务器。

特别说明:
我还添加了一个else以及分配的uniqid(); $uniqid变量,以便两个上传的文件在开始时将具有相同的编号。

如果文件成功上传,我添加了一个回显的“成功”。

这是工作/测试代码:

<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
?>
于 2013-07-23T15:28:59.557 回答