0

So I made a php file to upload files. This is the code.

HTML:

<html>
 <head></head>
<body>

 <form method="post" action="" enctype="multipart/form-data">
    Upload File:
    <input type="file" name="upload" /><br> 
    <input type="submit" name="submit" value="Submit"/>
 </form>
</body>
</html>

PHP:

<?php
include("config.php");
if(isset($_POST['submit'])  )
{
if ($_FILES['upload']['size'] != 0 ){
$filename = $con->real_escape_string($_FILES['upload']['name']);
$filedata= $con->real_escape_string(file_get_contents($_FILES['upload']['tmp_name']));
$filetype = $con->real_escape_string($_FILES['upload']['type']);
$filesize = intval($_FILES['upload']['size']);
$query = "INSERT INTO contracts(`filename`,`filedata`, `filetype`,`filesize`) VALUES ('$filename','$filedata','$filetype','$filesize')" ;
if ($con->query($query) == TRUE) {
echo "<br><br> New record created successfully";
}
else 
{
echo "Error:<br>" . $con->error;
}
} else {
$filename = $con->real_escape_string($_FILES['upload']['name']);
$filetype = $con->real_escape_string($_FILES['upload']['type']);
$filesize = intval($_FILES['upload']['size']);
$query = "INSERT INTO contracts(`filename`, `filetype`,`filesize`) VALUES ('$filename','$filetype','$filesize')" ;

if ($con->query($query) == TRUE) {
echo "<br><br> New record created successfully";
} else {
echo "Error:<br>" . $con->error;
}
}
$con->close(); 
}   
?>

But it shows the warning like this.

Warning: mysqli::query(): MySQL server has gone away in C:\xampp\htdocs\contractdb\filetest.php on line 29

Warning: mysqli::query(): Error reading result set's header in C:\xampp\htdocs\contractdb\filetest.php on line 29
Error:
MySQL server has gone away

Even though I have modify the php.ini file and restarted apache. This is what I have edited in the php.ini file. I only edited this three things only.

memory_limit = 32M
upload_max_filesize = 24M
post_max_size = 32M

I try to upload the file only above 1MB and it still doesn't work. I want to upload large files. What did I do wrong to get the warning message?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Hazirah_Halim
  • 87
  • 2
  • 16

2 Answers2

0
    <?php
    if($_FILES["photo"]["error"] > 0){
        echo "Error: " . $_FILES["photo"]["error"] . "<br>";
    } else{
        echo "File Name: " . $_FILES["photo"]["name"] . "<br>";
        echo "File Type: " . $_FILES["photo"]["type"] . "<br>";
        echo "File Size: " . ($_FILES["photo"]["size"] / 1024) . " KB<br>";
        echo "Stored in: " . $_FILES["photo"]["tmp_name"];
    }
    ?>

<?php
if(isset($_FILES["photo"]["error"])){
    if($_FILES["photo"]["error"] > 0){
        echo "Error: " . $_FILES["photo"]["error"] . "<br>";
    } else{
        $allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
        $filename = $_FILES["photo"]["name"];
        $filetype = $_FILES["photo"]["type"];
        $filesize = $_FILES["photo"]["size"];

        // Verify file extension
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file format.");

        // Verify file size - 5MB maximum
        $maxsize = 5 * 1024 * 1024;
        if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");

        // Verify MYME type of the file
        if(in_array($filetype, $allowed)){
            // Check whether file exists before uploading it
            if(file_exists("upload/" . $_FILES["photo"]["name"])){
                echo $_FILES["photo"]["name"] . " is already exists.";
            } else{
                move_uploaded_file($_FILES["photo"]["tmp_name"], "upload/" . $_FILES["photo"]["name"]);
                echo "Your file was uploaded successfully.";
            } 
        } else{
            echo "Error: There was a problem uploading your file - please try again."; 
        }
    }
} else{
    echo "Error: Invalid parameters - please contact your server administrator.";
}
?>

try this above code it might work.

Saravanan Nandhan
  • 579
  • 10
  • 19
0

Make sure you've also set the max_allowed_packet_size in your MySQL settings (read more here: How to change max_allowed_packet size) and check that you don't have a .htaccess file blocking file uploads of more than 1MB, which might look something like this:

php_value upload_max_filesize 1M

You can always do an ini_get to check if your new php.ini settings have actually been set succesfully: http://php.net/manual/en/function.ini-get.php

echo(ini_get('upload_max_filesize'));
Community
  • 1
  • 1
Laurens Swart
  • 1,234
  • 9
  • 24