What could cause $_FILES['file']['name'] to be empty? - php

I have a form which includes a file upload. Everything in the form seems to be working fine except that the $_FILES['file']['name'] turns up empty.
HTML
<form ... >
<input class="file" type="file" name="file[]" />
<input class="file" type="file" name="file[]" />
<input class="file" type="file" name="file[]" />
</form>
PHP
foreach ($_FILES['file']['name'] as $index => $file) {
// Handle file upload
}
I get an error saying that the index $_FILES['file'] is not defined. I've checked that file upload is enabled in PHP. What else could be causing this to turn up empty?

Is the enctype right?
Try
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input class="file" type="file" name="file[]" />
<input class="file" type="file" name="file[]" />
<input class="file" type="file" name="file[]" />
</form>
Without enctype no files will be uploaded, so the $_FILES array will be empty.

Related

PHP/HTML is it possible to make file uploading by user not mandatory

I'm making a website where users can apply their cars in form , i choosed to make 10 image inputs because i thought how mobile user could select multiple of photos at once(maybe it's possible but i dont know) so i came with this idea. The problem im facing is that, if one of the uploading inputs are empty it give me an error
Fatal error: Uncaught PHPMailer\PHPMailer\Exception: Could not access file: in E:\XAMPP\htdocs\TEST\phpmailer\src\PHPMailer.php:2991 Stack trace: #0 E:\XAMPP\htdocs\TEST\contactform.php(58): PHPMailer\PHPMailer\PHPMailer->addAttachment('', '') #1 {main} thrown in E:\XAMPP\htdocs\TEST\phpmailer\src\PHPMailer.php on line 2991
<label>1 <input type="file" class="file" name="image" id="image" /></label>
<label>2 <input type="file" class="file" name="image1" id="image1" /></label>
<label>3 <input type="file" class="file" name="image2" id="image2" /></label>
<label>4 <input type="file" class="file" name="image3" id="image3" /></label>
<label>5 <input type="file" class="file" name="image4" id="image4" /></label>
<label>6 <input type="file" class="file" name="image5" id="image5" /></label>
<label>7 <input type="file" class="file" name="image6" id="image6" /></label>
<label>8 <input type="file" class="file" name="image7" id="image7" /></label>
<label>9 <input type="file" class="file" name="image8" id="image8" /></label>
<label>10 <input type="file" class="file" name="image9" id="image9" /></label>
$mail->AddAttachment($_FILES['image']['tmp_name'],
$_FILES['image']['name']);
$mail->AddAttachment($_FILES['image1']['tmp_name'],
$_FILES['image1']['name']);
$mail->AddAttachment($_FILES['image2']['tmp_name'],
$_FILES['image2']['name']);
$mail->AddAttachment($_FILES['image3']['tmp_name'],
$_FILES['image3']['name']);
$mail->AddAttachment($_FILES['image4']['tmp_name'],
$_FILES['image4']['name']);
$mail->AddAttachment($_FILES['image5']['tmp_name'],
$_FILES['image5']['name']);
$mail->AddAttachment($_FILES['image6']['tmp_name'],
$_FILES['image6']['name']);
$mail->AddAttachment($_FILES['image7']['tmp_name'],
$_FILES['image7']['name']);
$mail->AddAttachment($_FILES['image8']['tmp_name'],
$_FILES['image8']['name']);
$mail->AddAttachment($_FILES['image9']['tmp_name'],
$_FILES['image9']['name']);
You should change your file upload to allow multiple images, so you just need one file element.
<input type="file" class="file" name="image" id="image" multiple/>
Next, just loop through them in your PHP, and add them as an attachment to $mail.
foreach($_FILES['image']['name'] as $key => $name) {
$tmp_name = $_FILES['image'][$key]['tmp_name'];
$mail->AddAttachment($tmp_name, $name);
}

Multi image upload

I'm learning PHP and can not find a way to make this work. Did I write correct code to upload multiple images?
I can not think of a reason why this should be wrong.
$imageName = mysql_real_escape_string($_FILES["image, drie1, drie2, drie3, add, strip"]["name"]);
$imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
$imageType = mysql_real_escape_string($_FILES["image, drie1, drie2, drie3, add, strip"]["type"]);
HTML
<form action="file-upload.php" method="post" enctype="multipart/form-data">
Send these files:<br />
<input name="userfile[]" type="file" /><br />
<input name="userfile[]" type="file" /><br />
<input type="submit" value="Send files" />
</form>
PHP (file-upload.php)
var_dump($_FILES);
This will display the info of the uploaded files
You can add accept attribute to the input to limit the allowed filetypes by extention
Html
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="image_file[]" multiple=""> /* multiple tag is used to upload multiple files */
<input type="submit" name="Submit" value="Submit" />
</form>
Php
<?php
foreach($_FILES["image_file"]["name"] as $key => $value)
{
$name = $value;
$tmp_name = $_FILES["image_file"]["tmp_name"][$key];
$type = $_FILES["image_file"]["type"][$key];
echo $name." , ".$tmp_name." , ".$type."\n";
}
?>

Wordpress - file upload form in page - empty $_FILES array

I'm trying to create a simple fileupload form in my custom Wordpress template:
<form action="<?php echo $current_url = home_url(add_query_arg(array(),$wp->request)); ?>" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
On submit, the $_FILES array is empty.
I think the problem is in the action, as the url that is generated is the nice URL, instead of http://domain.com/somepost.php
What should I put into the action, so I get the $_FILES?
Try this
<?php
if(isset($_POST['submit'])){
if($_FILES["file"]["error"] > 0){
echo $_FILES["file"]["error"];
}
else{
echo $_FILES['file']["name"]; // your file name
}
}
?>
<form method="post" action="#" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" name="submit"/>
</form>

Upload an image and store it in two different directories

Hi, I have a form which has two file uploaders. I want to have the second file uploader upload an image and store it in two different folders.
Can anyone help me out? Here is my HTML code:
<input type="file" name="file" id="file"><br>
<input type="submit" name="upload" value="Upload Image">
<input type="file" name="thumb" id="thumb"><br>
<input type="submit" name="updthumb" value="Upload Thumb impression">
Here is my PHP code:
if(isset($_POST["upload"])){
$image ="upload/";
move_uploaded_file($_FILES["file"]["tmp_name"], $image . $_FILES["file"]["name"]);
$newfilepath2 = $image . $_FILES["file"]["name"];
mysql_query("UPDATE `candidate` SET `imgpath`='$newfilepath2' WHERE vouchno='$vouch'") or die(mysql_error());
header("refresh: 1; candproc.php?vouchno=$vouch");
}
Can anyone help in telling me how to upload the image and save it in a different folder?
Something like:
$thumbspath ="thumbs/";
$thumb = $thumbspath . $_FILES["thumb"]["name"];
move_uploaded_file($_FILES["thumb"]["tmp_name"], $thumb);
For the second file, saving into the directory thumbs
You should have set your form:
<form action="phpscript.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file"><br>
<input type="file" name="thumb" id="thumb"><br>
<input type="submit" name="upload" value="Upload files">
</form>
Note that enctype="multipart/form-data" is required for file uploads.

file array output printing

i am using an array of file, i mean input type ="file" to upload number of files at a time.
i want to print or echo that file array values , how can i do this.
please help me
Thanks
You can create an array by suffixing [] to the name of the field:
<from method="POST" enctype="multipart/form-data">
<input type="file" name="myfile[]" />
<input type="file" name="myfile[]" />
<input type="file" name="myfile[]" />
<!-- and so on -->
<input type="submit" name="submit" value="Submit" />
</form>
PHP
if (isset($_POST['submit']))
{
print_r($_FILES['myfile']);
}
Do you mean this?
var_dump($_FILES['myfield']);

Categories