我想將多個文件上傳到目錄。 我已經「寫入」的代碼適用於一個文件,但是當我嘗試上傳多個文件時,它不起作用。我試圖確定故障在哪裏,我相信它與計數有關。儘管當我嘗試回顯有多少文件正在計數時,無論有多少文件被選中,我都會得到'1'。我知道這隻適用於一個文件,因爲我返回的變量總是'1',因此只能用於一個文件。PHP count()無法正確計數
收集我使用HTML表單與POST方法 HTML文件:
<form id="upload" action="upload.php" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>HTML File Upload</legend>
<input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="300000" />
<div>
<label for="fileselect">Files to upload:</label>
<input type="file" id="fileselect" name="fileselect[]" multiple="multiple" />
<div id="filedrag">or drop files here</div>
</div>
<div id="submit">
<button type="submit">Upload Files</button>
</div>
</fieldset>
</form>
的JavaScript我使用使拖拽功能,並從被來源: http://www.sitepoint.com/html5-file-drag-and-drop/
罪魁禍首 PHP:
if(isset($_FILES['fileselect']['tmp_name'])) {
// Number of uploaded files
$num_files = count($_FILES['fileselect']['tmp_name']);
echo $num_files;
/** loop through the array of files ***/
for($i=0; $i <= $num_files;$i++) {
// check if there is a file in the array
if(!is_uploaded_file($_FILES['fileselect']['tmp_name'][$i])) {
$messages[] = 'No file uploaded';
}
else {
$unique = substr(number_format(time() * rand(),0,'',''),0,10);
$newImg = "img".$unique;
$filename = basename($_FILES['file']['name'][$i]);
$extension = pathinfo($filename, PATHINFO_EXTENSION);
//$new = md5($filename).'.'.$extension;
$the_file_type = $_FILES['fileselect']['type'][$i];
$the_file_size = $_FILES['fileselect']['size'][$i]/1024;
$the_file_name = $_FILES['fileselect']['name'][$i];
$ok=1;
//This is our size condition
if ($uploaded_size > 350000) {
$messages[] = "Your file is too large.<br>";
$ok=0;
}
//This is our limit file type condition
if ($uploaded_type =="text/php") {
$messages[] = "No PHP files<br>";
$ok=0;
}
//Here we check that $ok was not set to 0 by an error
if ($ok==0) {
$messages[] = "Sorry your file was not uploaded";
}
//If everything is ok we try to upload it
else {
if(move_uploaded_file($_FILES['fileselect']['tmp_name'][$i], "uploads/{$newImg}")) {
echo "The file ". basename($_FILES['uploadedfile']['name'][$i]). " has been uploaded";
echo "<br />";
//echo $the_file_type;
//echo "<br />";
//echo $unique;
//echo "<span>schmeckle!</span>";
//echo "<br />";
//echo $the_file_size;
//echo "<br />";
//echo $the_file_name;
$insertSQL = "INSERT INTO interviews_media_images SET ";
$insertSQL .= "fileType='$the_file_type', ";
$insertSQL .= "fileRef='$newImg', ";
$insertSQL .= "fileSize='$the_file_size', ";
$insertSQL .= "fileName='$the_file_name' ";
echo $insertSQL;
//mysql_query($insertSQL);
//echo mysql_error();
} else {
$messages[] = "Sorry, there was a problem uploading your file.";
}
}
}
}
}
你甚至'print_r($ _ FILES)'看看它實際上包含什麼?我相信它是'$ _FILES ['fileselect'] [$ i] ['attribute']'not'$ _FILES ['fileselect'] ['attribute'] [$ i]' – Twisted1919
上面的所有內容:「for($ i = 0; $ i <= $ num_files; $ i ++){... blah} else {「,是從棧上的另一個帖子引用的,這顯然是有效的。 鏈接:http://stackoverflow.com/questions/2233816/how-to-handle-multiple-file-upload-using-php 下面的一切工作。我的問題是「$ num_files = count($ _ FILES ['fileselect'] ['tmp_name']);」總是返回'1'。 –
我的第一個猜測是PHP'count()'函數中沒有錯誤 – chrislondon