Jump to content

Uploading Picture Problem


krash11554

Recommended Posts

I have code that checks if a file is selected do this. But my problem is it is running that code even though i dont have a selected file in a input box. heres the code.

if(isset($_FILES['editcar_fileupload']) && empty($_POST) === false){

//check where to start uploading pictures
$image_name = $_FILES['editcar_fileupload']['name'];
$image_size = $_FILES['editcar_fileupload']['size'];
$image_temp = $_FILES['editcar_fileupload']['tmp_name'];
$allowed_ext = array('jpg','jpeg','png','gif');
$count = count($image_name);

if (isset($image_name) && empty($image_name) === false){
 foreach($image_name as $image_names){
 $image_ext = strtolower(end(explode('.', $image_names)));
 if(in_array($image_ext, $allowed_ext) === false && $true === true){
 $errors[] = 'File type is not allowed';
 }
 }
 if($count > 10){
 $errors[] = 'You can not upload more than 10 pictures for your car.';
 }
}


}

here is the html

<input type="file"  onchange="this.form.editcar_fakeinput.value = this.value;" name="editcar_fileupload[]" multiple="multiple" id="editcar_fileupload" />

it says the $image_name has values in it but it really dosent.

Link to comment
https://forums.phpfreaks.com/topic/269021-uploading-picture-problem/
Share on other sites

The $_FILES array will be set and not empty for most of the possible error conditions. You must test if the file was actually uploaded, the ['error'] element is set and equal to zero, before you can access any of the uploaded file information.

could check to see if the field is empty?

 

The HTML:

 

<html>
<form action="" method="POST" enctype="multipart/form-data">
<label>File Upload</label><input type="file" name="file" />
<input type="submit" name="submit" value="Upload" />
</form>
</html>

 

The PHP:

 

<?php
if(!$_POST["file"]){
echo "No file selected.";
}else{

//processing code

}
?>

Sigh.

I suggest to use empty() instead of isset()

Won't work.

The $_FILES array will be set and not empty for most of the possible error conditions. You must test if the file was actually uploaded, the ['error'] element is set and equal to zero, before you can access any of the uploaded file information.

^ THIS

could check to see if the field is empty?

<?php
if(!$_POST["file"]){
echo "No file selected.";
}else{

//processing code

}
?>

Won't work.

I have errror 4 which is no file is selected but how come it passes the isset()? if there is no file selected??

 

If you saw a value of 4 in the error element - $_FILES['editcar_fileupload']['error'], wouldn't that mean that $_FILES['editcar_fileupload'] ISSET?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.