PHP - 无法上传文件甚至超过1MB并且在编辑完php.ini之后

时间:2017-03-27 09:06:10

标签: php mysql

所以我制作了一个php文件来上传文件。这是代码。

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(); 
}   
?>

但它显示了这样的警告。

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

即使我修改了php.ini文件并重新启动了apache。这是我在php.ini文件中编辑的内容。我只编辑了这三件事。

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

我尝试上传的文件只有1MB以上,但仍然无效。我想上传大文件。我收到警告信息的错误是什么?

2 个答案:

答案 0 :(得分:0)

{{1}}

尝试以上代码可能会有效。

答案 1 :(得分:0)

确保您还在MySQL设置中设置了max_allowed_pa​​cket_size(在此处阅读更多内容:How to change max_allowed_packet size)并检查您是否有.htaccess文件阻止超过1MB的文件上传,可能看起来像这样:

php_value upload_max_filesize 1M

您可以随时执行ini_get来检查您的新php.ini设置是否已成功设置:http://php.net/manual/en/function.ini-get.php

echo(ini_get('upload_max_filesize'));