1

The problem is the $_FILES['thumbnailPic'] is not picking values from HTML form .I have tried so many solutions but its just not working :( The HTML code is :

    <label >Thumbnail Picture<text>*</text></label><br>
    <input type="file" name='thumbnailPic' id="pic"><br>
    <label >Original Picture<text>*</text></label><br>
    <input type="file" name='originalPic' id="pic"><br>
    <input  type="submit" name="AddStock" id= "formAddStock" value="Add Stock">

The php code is :

if(!isset($_FILES['thumbnailPic']))
    {
     echo '<p>Please select a file</p>';

}
    else
    {
  $spID=NULL;
$Quant=$_POST['quantity'];
$Siz=$_POST['Size'];

    $imgfp = fopen($_FILES['originalPic']['tmp_name'],'rb');
 $stmt = $connection->prepare("INSERT INTO stitchedproduct (sp_id,quantity,size,p_id,color_id,sp_thumbnail,sp_OriginalPic) VALUES (?,?, ?, ?,?,?,?)");
    $stmt->bindParam(1, $spID);
    $stmt->bindParam(2, $Quant);
    $stmt->bindParam(3, $Siz);
    $stmt->bindParam(4, $ProductID);
    $stmt->bindParam(5, $colour);
      $stmt->bindParam(6, $imgfp);
    $stmt->bindParam(7, $imgfp);
    //$stmt->bindParam(1, $spID);


     $stmt->execute();
    }
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Sylvia Gerald
  • 49
  • 1
  • 2
  • 6
  • "doesn't work" or "not working" is not a useful metric to debug or solve your problem. More information is needed to address the issue you are experiencing. For example, start by: describing what you expect the code to do; what it's doing that you didn't expect; and include all error information relevant to your issue. – Amal Murali Mar 22 '14 at 04:25
  • check your form tag with enctype= multipart/form-data and let us know or paste your var_dump($_POST); and file to – sismaster Mar 22 '14 at 04:26
  • possible duplicate of [Why would $\_FILES be empty when uploading files to PHP?](http://stackoverflow.com/questions/3586919/why-would-files-be-empty-when-uploading-files-to-php) – mario Mar 22 '14 at 04:27
  • I want to save the picture in the database but the $_FILES simply do not pic anything from HTML form . – Sylvia Gerald Mar 22 '14 at 04:27

3 Answers3

12

First of all verify that in your HTML code in the <form> tag you have added the enctype="multipart/form-data" attribute.

So your form starting will be like:

<form action="" method="post" enctype="multipart/form-data">
    <!-- It's a file-upload form -->

Refer to POST method uploads - Handling file uploads for more.


To check if a file has been successfully uploaded use the "error" key:

if (UPLOAD_ERR_OK === $_FILES["thumbnailPic"]["error"]){
    // File uploaded
}

Refer to Error Messages Explained - Handling file uploads for more.


Tip regarding image uploads:

To check if the uploaded file is an image you can check it as:

if (getimagesize($_FILES["thumbnailPic"]["tmp_name"])){
    // It's perhaps an image
}
else
{
    // Can not be identified as an image
}

Refer here for more.

(and think about the consequences for a moment, what if an image format is known to be used in attacks? what if the getimagesize function returns true but it actually is not an image, but a trojan horse of an image?)

hakre
  • 193,403
  • 52
  • 435
  • 836
  • @SylviaGerald : try placing the `name` in double quotes => `""` – NoobEditor Mar 22 '14 at 04:35
  • I can see there are two file types `originalPic` and `thumbnalPic` please verify in what you are uploading –  Mar 22 '14 at 04:36
  • @SylviaGerald double quotes wont do anything I guess correct me if I am wrong –  Mar 22 '14 at 04:48
3

To check file is Uploaded or not :

if (file_exists($_FILES['file_name']['tmp_name']) || is_uploaded_file($_FILES['file_name']['tmp_name'])) {
    echo 'Your file has been uploaded';
}
else
{
    echo 'Please Select file to Upload';
}

Working 100% for me.

Irshad Khan
  • 5,670
  • 2
  • 44
  • 39
2

Several possibilities and solution for this issues.

  • You have to use the enctype="multipart/form-data in your form.
  • Check the post_max_size and upload_max_filesize in our php.ini file. if you are uploading image size greater than the values stored in the post_max_size and upload_max_filesize , then file won't get uploaded
  • You can check the error in the file using $_FILES["thumbnailPic"]["error"]
Ananth
  • 1,520
  • 3
  • 15
  • 28