【问题标题】:PHP: Uploading multiple images at the same timePHP:同时上传多张图片
【发布时间】:2015-01-31 05:33:31
【问题描述】:

我创建了一个 HTML 表单,允许我将图像上传到一个唯一命名的目录(即日期/时间/IP 地址的组合)。现在我正在尝试上传两张图片(最终是多张)。我复制了我的“文件 1”内容并将其唯一命名为“文件 2”。我很快将“2”附加到每个变量和帖子字段。这是我的上传表单 HTML:

<!DOCTYPE html>
<html>
<body>

<form action="icanhazuploads.php" method="post" enctype="multipart/form-data">
Select image(s) to upload: <br /><br />
<input type="file" name="fileToUpload" class="fileToUpload"><br /><br />
<input type="file" name="fileToUpload2" class="fileToUpload2"><br /><br />
<input type="submit" name="submit">
</form>

</body>
</html> 

非常直接。两个上传字段和一个提交按钮。这是我的 PHP 处理器文件:

<?php
// Display Errors
ini_set('display_errors', 'On');
ini_set('html_errors', 0);

$ip = ip2long($_SERVER['REMOTE_ADDR']);
$datetime = date('YmdHis');  
$target_dir = "uploads/" . $datetime . $ip . "/";

// Create the unique diretory
if (!file_exists($target_dir)) {
    mkdir($target_dir, 0777, true);
}

// Start processing files

// File 1
echo "<br />1st file being added...<br />";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
echo "<br />Target File: " . $target_file. "<br />";
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".<br />";
        $uploadOk = 1;
    } else {
        echo "File is not an image.<br />";
        $uploadOk = 0;
    }
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Your file was not uploaded.<br />";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.<br />";
    } else {
        echo "There was an error uploading your file.<br />";
    }
}



// File 2
if(isset($_POST["fileToUpload2"])) {
echo "<br />2nd file being added...<br />";
$target_file2 = $target_dir . basename($_FILES["fileToUpload2"]["name"]);
echo "<br />Target File: " . $target_file2. "<br />";
$uploadOk = 1;
$imageFileType = pathinfo($target_file2,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload2"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".<br />";
        $uploadOk = 1;
    } else {
        echo "File is not an image.<br />";
        $uploadOk = 0;
    }
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Your file was not uploaded.<br />";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload2"]["tmp_name"], $target_file2)) {
        echo "The file ". basename( $_FILES["fileToUpload2"]["name"]). " has been uploaded.<br />";
    } else {
        echo "There was an error uploading your file.<br />";
    }
}

真的很神奇。我在浏览器中打开我的 HTML 表单,选择两个图像并点击提交。我可以使用 Firebug 或 Chrome 的检查器观看,并通过 POST 填充的两个图像字段查看它。但是,我的 PHP 文件将始终只看到一个。如果我只将图像放在表单的第二个字段中,它仍然就像我将它放在第一个字段中一样。错误日志中没有错误。知道为什么它拒绝查看多个文件吗?

我的 php.ini 设置:

; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 64M

; Maximum number of files that can be uploaded via a single request
max_file_uploads = 20

【问题讨论】:

  • 文件有多大?
  • 各种大小:
  • 1) 文件大小的总和是否超过了最大上传大小? 2)你检查过 php 信息以查看名为 max_file_uploads 的变量吗?
  • 如果文件很大并且负责,您的 php 也可以有一个最大执行时间,它们将超过该时间限制并且脚本将停止执行

标签: php post upload


【解决方案1】:

if(isset($_POST["fileToUpload2"])) {

这是错误的,它永远不会被设置。

试试

if(isset($_FILES["fileToUpload2"])) {

对于未来的调试,使用var_dump($_POST);var_dump($_FILES);,你会立即看到发生了什么

【讨论】:

  • 哇哦。你搞定了。谢谢@rjdown!我永远不会明白这一点。
  • 没问题。实际上, $_FILES["fileToUpload2"] 将始终设置(假设他们使用表单),因此您可能需要测试这些值是否也不是空的,例如tmp_name
  • 是的,我刚刚注意到了。他们实际上是从桌面应用程序提交 POST 数据。此 HTML 表单用于测试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 2011-09-18
  • 2021-12-01
  • 2018-11-07
相关资源
最近更新 更多