1

I get this error everytime i hit the submit button. Everything else is submitted to the database, only the image isn't. Warning: file_get_contents(): Filename cannot be empty Any idea? Here is my code.

if(isset($_POST['consultationbutton'])){        
    $image = addslashes(file_get_contents($_FILES['selectedfile']['tmp_name'])); //SQL Injection defence!
    $image_name = addslashes($_FILES['selectedfile']['name']);
    $checkedcondition = implode(",",$_POST['skincondition']);
    $checkedproduct = implode(",",$_POST['skincareinuse']);
    $consultquery="INSERT INTO counsel(nric,dateconsulted,editableface,skincarecurrentlyinuse,skincondition,imagename) VALUES('132','$_POST[storedate]','{$image}','$checkedproduct','$checkedcondition','{$image_name}')";
    mysqli_query($dbconn,$consultquery);
   // mysqli_escape_string($dbconn,$image);
    echo $checkedcondition;
    echo $checkedproduct;
}   

<form action="BAConsult.php" enctype="multipart/form-data" method='post'>
<img id="customerface" src="#" alt="your image" class ="consultimg"></img>
 Select a file to upload: <input type="file" name="selectedfile" onchange="readURL(this);">
<input type='submit' name='consultationbutton' class='consultationbutton' value='Complete Consultation' onclick='submitclick();'>
</form>

<script>
function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $('#customerface')
                .attr('src', e.target.result)
                .width(400)
                .height(400);
        };

        reader.readAsDataURL(input.files[0]);
    }
}
</script>
mctjl_1997
  • 163
  • 1
  • 3
  • 17

2 Answers2

6

In the $_FILES array, you've said you're getting [name]=>DSC_0770.JPG[type]=> [tmp_name]=>[error]=>1[size]=>0

If tmp_name is not given, that means the file was not uploaded.

The error you're given is 1, and according to the upload error messages explained page this means: that the uploaded file exceeds the upload_max_filesize directive in php.ini.

You can do the following to fix this.

  • Find your php.ini file and increase the size of upload_max_filesize.
  • Use ini_set('upload_max_filesize', '20M'); or whatever size you want at the start of your script.
  • Compress your images before uploading.
castis
  • 8,154
  • 4
  • 41
  • 63
  • Ahh i see, is there a way to increase the max upload file size? – mctjl_1997 Jun 15 '16 at 13:13
  • okay great im getting 0 error but now my SQL queries are having problems, it says MySQL Server has gone away – mctjl_1997 Jun 15 '16 at 13:27
  • That is a different problem altogether. That could happen if you're trying to store a giant image in the server. You probably dont want to store whole images in mysql. – castis Jun 15 '16 at 13:29
  • 1
    OMG it worked! It really is because my image is too big. Somehow, when i changed my max upload size, my sql database wasn't able to handle it even if my program is able to. Thank you very much :) – mctjl_1997 Jun 15 '16 at 13:38
  • You're welcome! Also check out the first answer to [this question](http://stackoverflow.com/questions/6472233/can-i-store-images-in-mysql) for some info on storing images in a sql server. – castis Jun 15 '16 at 13:40
3

make sure you add enctype="multipart/form-data" attribute in your <form> tag. like

<form action="your_action_file" method="post" enctype="multipart/form-data">