0

I'm trying to get an image file sent from html form in order to insert it in a database. It's unfortunately not working because the file is not posted.

All the other variables are working, I echoed them to check, but the file isn't. Where is my problem? I think it's at the html form, the inserted variables are also in the same form but in different divisions, so the post actually send all data in the form to post.php but not the image file.

As you can see I already include "enctype="multipart/form-data"".

Any help please?

1) HTML markup:

 <form action="post.php" method="post" enctype="multipart/form-data">
     <input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="300000"/>
    <div>
        <input type="file" id="fileselect" name="fileselect[]" multiple accept="image/*"/>
        <div id="filedrag">or drop files here</div>
    </div>

    <div id="submitbutton">
        <button type="submit">Upload Files</button>
    </div>
</form> 

2) PHP code:

$title=isset($_POST['title'])?$_POST['title']:false;
$desc=isset($_POST['description'])?$_POST['description']:false;
$date=date("Y-M-D");
$city=isset($_POST['city'])?$_POST['city']:false;
$user=isset($_POST['name'])?$_POST['name']:false;
$price=isset($_POST['price'])?$_POST['price']:false;
$table=isset($_POST['option'])?$_POST['option']:false;
$imgfile=isset($_FILE['file']['name'])?$_FILE['fileselect']['name']:false;
$tmp_dir=isset($_FILE['fileselect']['tmp_name'])?$_FILE['fileselect'['tmp_name']:false;

$img_size=isset($_FILE['fileselect']['size'])?$_FILE['fileselect']['size']:false;
$upload_dir='images/'; // directory
     $imgExt=strtolower(pathinfo($imgfile,PATHINFO_EXTENSION)); // GET IMAGE EXTENSION

     $valid_ext=array('jpeg','jpg','png','gif');

     //rename uploading image

     $item_img=rand(1000,1000000).".".$imgExt;
     // allow valid image format

     if(in_array($imgExt, $valid_ext))
     {
         if($img_size < 500000)
         {
             move_uploaded_file($tmp_dir,$upload_dir.$item_img);
         }
         else{
             $erMsg="Sorry , your file is too large";
         }
     }
     else{
         $erMsg="Sorry,only JPG,JPEG,PNG & GIF files are accepted :)";
     }


$query = $pdo->prepare('INSERT INTO '.$table .'(title,description,image,date,city,user,price) VALUES(:title, :description, :image, :date, :city, :user, :price)');

$query->bindParam(':title', $title);
$query->bindParam(':description', $desc);
$query->bindParam(':image', $item_img);
$query->bindParam(':date', $date);
$query->bindParam(':city', $city);
$query->bindParam(':user', $user);
$query->bindParam(':price', $price);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jean Ouédraogo
  • 234
  • 2
  • 6
  • 16

3 Answers3

2

to send files from your form it should have correct enctype attribute.

<form action="script.php" enctype="multipart/form-data" method="post">
    <input type="file" name="file">
    <input type="submit">
</form>
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Sergey Fedirko
  • 659
  • 1
  • 7
  • 17
1

if you are working with file such as images, use the <form enctype="multipart/form-data">

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
J. Zend
  • 88
  • 7
0

Did you see you have a typo at the line ?

$tmp_dir=isset($_FILE['fileselect'['tmp_name'])$_FILE['fileselect'['tmp_name']:false;

you are missing ] after fileselect'

Check that!!

Hamza Zafeer
  • 2,360
  • 13
  • 30
  • 42
Amon Bazongo
  • 315
  • 4
  • 10