PHP PDO文件上传并保存到MySQL数据库会导致文件损坏


PHP PDO file upload and save to MySQL database results in obscure file breakage

经过大量测试,我意识到我的文件上传过程出现了问题,尽管显示了正确的文件详细信息——大小、名称、mime类型等,但最终文件还是被破坏了。经过大量测试和尝试不同的选项,我意识到此问题并不是我最初认为的提取/显示错误。在上传/DB插入过程中,它肯定会在某个地方微妙地中断。该文件被保存到一个中等blob MySQL字段中,该字段具有BINARY属性。有人能告诉我在处理上传/存储过程的代码中哪里出了问题吗?我一点错误都没有。

 <form action="./app.fileUpload.php?lnk=<?=$lnk?>" method="post" enctype="multipart/form-data">
 <input type="hidden" name="MAX_FILE_SIZE" value="5632000">
 <label for='file'>Filename:</label> <input type="file" name="file" id="file" style="cursor: pointer;">
 <input class="button" style="margin-top: 12px;" type="submit" name="submit" value="Upload" title="Upload">
 </form>
<?php
    if (isset($_POST['submit'])) {
        $temp = explode('.', $_FILES['file']['name']);
        $extn = strtolower(end($temp));
        if(($extn == "doc") || ($extn == "docx") || ($extn == "pdf")) {
            // Filetype is correct. Check size
            if($_FILES['file']['size'] < 5632000) {
                // Filesize is below maximum permitted. Add to the DB.
                $mime = $_FILES['file']['type'];
                $size = $_FILES['file']['size'];
                $name = $_FILES['file']['name'];
                $tmpf = $_FILES['file']['tmp_name'];
                $file = fopen($_FILES['file']['tmp_name'], "rb");
                try {
                    $stmt = $conn->prepare("INSERT INTO $database.appFiles (`appID`, `uaID`, `uID`, `dateUploaded`, `applicationKey`, `fileName`, `fileMime`, `fileSize`, `file`)
                    VALUES
                    (?,?,?,?,?,?,?,?,?)");
                    // Bind Values
                    $stmt->bindParam(1, $appID, PDO::PARAM_INT, 11);
                    $stmt->bindParam(2, $uaID, PDO::PARAM_INT, 11);
                    $stmt->bindParam(3, $uID, PDO::PARAM_INT, 11);
                    $stmt->bindParam(4, time(), PDO::PARAM_INT, 11);
                    $stmt->bindParam(5, $applicationKey, PDO::PARAM_STR, 8);
                    $stmt->bindParam(6, $name, PDO::PARAM_STR, 256);
                    $stmt->bindParam(7, $mime, PDO::PARAM_STR, 128);
                    $stmt->bindParam(8, $size, PDO::PARAM_INT, 20);
                    $stmt->bindParam(9, $file, PDO::PARAM_LOB);
                    // Execute the query
                    $stmt->execute();
                } catch(PDOException $e) { catchMySQLerror($e->getMessage()); }
            } else {
                // Filesize is over our limit. Send error message
                $error = "Your file is too large. Please read the instructions about file type and size, above.";
            }
        } else {
            $error = "Your file was the incorrect type. Please read the instructions about file type and size, above.";
        }
    }
?>

EDIT通过在页面顶部包含一个文件来调用数据库信息。文件如下:

<?php
// DATABASE SETTINGS
$hostname = "127.0.0.1";
$username = "nameinhere";
$password = "passwordinhere";
$database = "dbnamehere";
try {
    $conn = new PDO("mysql:host=$hostname; dbname=$database", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $conn->exec("SET CHARACTER SET utf8");      // Sets encoding UTF-8
    // close the database connection
    //$conn = null;
} catch(PDOException $e) {
    echo $e->getMessage();
}
?>

随着事情的发展,我的PHP实际上根本没有问题。开发服务器上的设置阻止了它的工作,当在隐藏的子域下测试时,它在生产服务器上完美地工作,没有任何代码更改。

感谢所有试图在这方面提供帮助的人。

C