我創建了一個HTML表單,允許我將圖像上傳到唯一命名的目錄(即日期/時間/ IP地址的組合)。現在我試圖上傳兩張圖片(最終是多張)。我已經複製了我的「文件1」內容,並將其唯一地命名爲「文件2」。我很快將「2」附加到每個變量和後置字段。這裏是我的上傳表單HTML:PHP:同時上傳多個圖像
<!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
有多大的文件? – MiiinimalLogic 2014-12-02 20:38:05
各種大小:<500kb,一些最多4或5MB。 – sparecycle 2014-12-02 20:38:51
1)文件大小的總和是否超過最大上傳大小? 2)你是否檢查過PHP信息以查看名爲max_file_uploads的變量? – 2014-12-02 20:39:01