PDO将pdf文件上传到sql-database会导致文件损坏

时间:2015-06-30 17:49:30

标签: php sql-server pdo

我正在尝试将pdf文件上传到sql server。我曾经为此目的使用SQL,这对我没有任何问题。但是,为了保持最新状态,我正在转向PDO。我现在遇到的问题是,当我尝试下载数据时文件已损坏。下载脚本在我使用旧的SQL上载脚本时有效,所以我认为下载脚本没有任何问题。在PDO方面,我是一个完整的新手,所以我不确定我的语法是否有任何问题。

$dbConnection = new PDO('mysql:host=server;dbname=database', 'user', 'pass');
$dbConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$fileName = $_FILES['userfile']['name'];
$ext = substr($fileName, strpos($fileName,'.'), strlen($fileName)-1);

if($_FILES['userfile']['size'] > 0 && $ext == ".pdf")
    {
    $tmpName  = $_FILES['userfile']['tmp_name'];
    $fileSize = $_FILES['userfile']['size'];
    $fileType = $_FILES['userfile']['type'];
    $userid = //some user id is //

    $fp      = fopen($tmpName, 'r');
    $content = fread($fp, filesize($tmpName));
    $content = addslashes($content);
    fclose($fp);

    if(!get_magic_quotes_gpc())
        {
        $fileName = addslashes($fileName);
        }

    $sqlfil = "UPDATE table SET filename = :ins_fileName, filesize = :ins_fileSize, filetype = :ins_fileType, filecontent = :ins_content WHERE id = :userid ";

    $statement = $dbConnection->prepare($sqlfil);
    $statement->bindValue(':ins_fileName', $fileName, PDO::PARAM_STR, 256);
    $statement->bindValue(':ins_fileSize', $fileSize, PDO::PARAM_INT, 20);
    $statement->bindValue(':ins_fileType', $fileType, PDO::PARAM_STR, 256);
    $statement->bindValue(':ins_content', $content, PDO::PARAM_LOB);
    $statement->bindValue(':userid', $aid, PDO::PARAM_INT, 11);
    $statement->execute();

总之,脚本只会将80%的数据上传到LONGBLOB(由sql-database中的内容判断),因此下载时文件已损坏。任何帮助将不胜感激。

根据Marc B. 工作代码:

的建议
$dbConnection = new PDO('mysql:host=server;dbname=database', 'user', 'pass');
$dbConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$fileName = $_FILES['userfile']['name'];
$ext = substr($fileName, strpos($fileName,'.'), strlen($fileName)-1);

if($_FILES['userfile']['size'] > 0 && $ext == ".pdf")
    {
    $tmpName  = $_FILES['userfile']['tmp_name'];
    $fileSize = $_FILES['userfile']['size'];
    $fileType = $_FILES['userfile']['type'];
    $userid = //some user id is //

    $fp      = fopen($tmpName, 'r');
    $content = fread($fp, filesize($tmpName));
    fclose($fp);

    $sqlfil = "UPDATE table SET filename = :ins_fileName, filesize = :ins_fileSize, filetype = :ins_fileType, filecontent = :ins_content WHERE id = :userid ";

    $statement = $dbConnection->prepare($sqlfil);
    $statement->bindParam(':ins_fileName', $fileName, PDO::PARAM_STR, 256);
    $statement->bindParam(':ins_fileSize', $fileSize, PDO::PARAM_INT, 20);
    $statement->bindParam(':ins_fileType', $fileType, PDO::PARAM_STR, 256);
    $statement->bindParam(':ins_content', $content, PDO::PARAM_LOB);
    $statement->bindParam(':userid', $aid, PDO::PARAM_INT, 11);
    $statement->execute();

0 个答案:

没有答案