这是html代码:
<form id="upload" action="upload.php" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>html upload</legend>
<input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="300000" />
<div>
<label for="fileselect">select file</label>
<input type="file" id="fileselect" name="fileselect[]" multiple="multiple" />
</div>
<div >
<button type="submit">upload</button>
</div>
</fieldset>
</form>
upload.php的代码:
$myFile = $_FILES['fileselect'];
$fileCount = count($myFile["name"]);
for ($i = 0; $i < $fileCount; $i++) {
$target_dir = "uploads/";
$target_file = $target_dir . basename( $myFile["name"][$i]);
// echo $myFile["tmp_name"][$i];
if (move_uploaded_file( $myFile["tmp_name"][$i], $target_file)) {
echo "The file ". basename( $myFile["name"][$i]). " has been uploaded.";
} else {
echo "can not move";
}
}
如果我上传的文件类型是Image或MP4,则上传确定。
我尝试var_dump: 上传图片。
array(1) { ["fileselect"]=> array(5) { ["name"]=> array(1) { [0]=> string(10) "header.png" } ["type"]=> array(1) { [0]=> string(9) "image/png" } ["tmp_name"]=> array(1) { [0]=> string(24) "C:\xampp\tmp\phpED23.tmp" } ["error"]=> array(1) { [0]=> int(0) } ["size"]=> array(1) { [0]=> int(32416) } } } The file header.png has been uploaded.
上传pdf:
array(1) { ["fileselect"]=> array(5) { ["name"]=> array(1) { [0]=> string(15) "Untitled-12.pdf" } ["type"]=> array(1) { [0]=> string(0) "" } ["tmp_name"]=> array(1) { [0]=> string(0) "" } ["error"]=> array(1) { [0]=> int(2) } ["size"]=> array(1) { [0]=> int(0) } } } can not move
但我无法上传文件pdf。
为什么无法上传文件pdf?
答案 0 :(得分:1)
首先有一个问题,请检查以下一行:
<input type="file" id="fileselect" name="fileselect" multiple="multiple" />
具有多个选择的输入文件必须将其名称作为数组,以便它可以包含多个文件名,如:
<input type="file" id="fileselect" name="fileselect[]" multiple="multiple" />
如果没有这个,你就无法迭代所有文件。