无法在PHP,MySql中上传3mb +文件

时间:2014-12-01 05:11:24

标签: php mysql pdf file-upload

我可以上传小于3 MB的文件,但我无法通过编码上传3mb +文件。 :/ 我搜索了它并更改了upload_max_filesize = 64M和post_max_size = 128M仍然得到了相同的错误。错误是"错误!没有发送文件!"这是我设置的:P,这是我的编码,请帮助我.....在此先感谢:)

数据库查询

CREATE TABLE `file` (
`id`        Int Unsigned Not Null Auto_Increment,
`name`      VarChar(255) Not Null Default 'Untitled.txt',
`mime`      VarChar(50) Not Null Default 'text/plain',
`size`      BigInt Unsigned Not Null Default 0,
`data`      MediumBlob Not Null,
`created`   DateTime Not Null,
PRIMARY KEY (`id`)
)

的index.html

 <!DOCTYPE html>
 <head>
<title>MySQL file upload example</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
 <body>
<form action="add_file.php" method="post" enctype="multipart/form-data">
    <input type="file" name="uploaded_file"><br>
    <input type="submit" value="Upload file">
</form>
<p>
    <a href="list_files.php">See all files</a>
</p>
</body>
</html>

add_file.php

<?php
 // Check if a file has been uploaded
if(isset($_FILES['uploaded_file'])) {
    // Make sure the file was sent without errors
    if($_FILES['uploaded_file']['error'] == 0) {
        // Connect to the database
        $dbLink = new mysqli('127.0.0.1', 'root', '', 'cart');
       if(mysqli_connect_errno()) {
            die("MySQL connection failed: ". mysqli_connect_error());
         }

    // Gather all required data
    $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
    $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
    $data = $dbLink->real_escape_string(file_get_contents($_FILES  ['uploaded_file']['tmp_name']));
    $size = intval($_FILES['uploaded_file']['size']);

    // Create the SQL query
    $query = "
        INSERT INTO `file` (
            `name`, `mime`, `size`, `data`, `created`
        )
        VALUES (
            '{$name}', '{$mime}', {$size}, '{$data}', NOW()
        )";

    // Execute the query
    $result = $dbLink->query($query);

    // Check if it was successfull
    if($result) {
        echo 'Success! Your file was successfully added!';
    }
    else {
        echo 'Error! Failed to insert the file'
           . "<pre>{$dbLink->error}</pre>";
    }
}
else {
    echo 'An error accured while the file was being uploaded. '
       . 'Error code: '. intval($_FILES['uploaded_file']['error']);
}

// Close the mysql connection
$dbLink->close();
}
else {
echo 'Error! A file was not sent!';
}

 // Echo a link back to the main page
echo '<p>Click <a href="index.html">here</a> to go back</p>';
?>

1 个答案:

答案 0 :(得分:0)

更改upload_max_filesize = 64M和post_max_size = 128M后,我需要重新启动服务器,然后编码才能正常工作。 :)