this is html code:
<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>
code of 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";
}
}
If i upload file type is Image or MP4, they upload ok.
i try var_dump : upload a image.
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.
Upload a 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
But i can't upload file pdf.
Why can't upload file pdf?
asked May 16, 2017 at 11:32
D T
3,77611 gold badges50 silver badges95 bronze badges
1 Answer 1
First of all there is an issue, check the following line:
<input type="file" id="fileselect" name="fileselect" multiple="multiple" />
input file with multiple selection must have its name as an array so that it can hold multiple files name like:
<input type="file" id="fileselect" name="fileselect[]" multiple="multiple" />
Without this you cannot iterate over all the files.
answered May 16, 2017 at 11:38
Mayank Pandeyz
26.3k4 gold badges43 silver badges61 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
D T
thank,i miss when post question. but that is not my problem
D T
Upload a 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
lang-php
MAX_FILE_SIZEvalue ?$myFile["name"][$i]['error']whenever you do an upload. It will normally show you what the problem is or at least lead you to the solution