我有代码允许用户选择一个 PDF 文件并将其上传到 MySQL,它完美地工作。

// Gather all required data
        $name = $dbcon->real_escape_string($_FILES['ogx_pdf']['name']);
        $mime = $dbcon->real_escape_string($_FILES['ogx_pdf']['type']);
        $data = $dbcon->real_escape_string(file_get_contents($_FILES  ['ogx_pdf']['tmp_name']));
        $size = $dbcon->strval($_FILES['ogx_pdf']['size']);
        $unid = $_POST['unid'] ;
        $comment_id = $_POST['comment_id'] ;
        // Create the SQL query
        $call = "
            INSERT INTO `deal_attachment` (
                `att_name`, `att_mime`, `att_size`, `att_content`, `att_created` , `att_unid` , `att_comment_id`
            )
            VALUES (
                '{$name}', '{$mime}', {$_FILES['ogx_pdf']['size']}, '{$data}', NOW(), '{$unid}' , '{$comment_id}'
            )";

        // Execute the query

        $result = mysqli_query($dbcon,$call) ;

但这需要$_FILES 的输入(我在这里研究的所有例子和其他网站都是相同的路线)。

现在我需要扩展它以上传许多 PDF 文件并适当地存储它们。代码按顺序读取文件列表,找到文件并将其上传即可,但是当我尝试从数据库中读取附件时,PDF 应用程序失败 - 内容出现问题。

$file = fopen($path ."ogx_attach.txt","r");

while(! feof($file))
{
    $line =fgetcsv($file);
    // Gather all required data
    $name = $dbcon->real_escape_string($path .  $line[0]);
    $data = $dbcon->real_escape_string(file_get_contents($name));
    $size =  (string)(strlen($data));
    $filename = trim($name) ;
    $dateymd =  date("Y/m/d") ;
    $query = "
        INSERT INTO `deal_attachment` (
            `att_name`, `att_mime`, `att_size`,  `att_created`, `att_content`
        )
        VALUES ('{$name}', 'application/pdf', {$size},  '{$dateymd}', '" . $data . "')";

    $result = mysqli_query($dbcon,$query) ;

    // Check if it was successfull
    if (false === $result) {
        echo mysqli_error($dbcon);
        die ;
    } else
    { 
        //echo 'Success! Your row was successfully added!';

行中的 blob 确实包含某些内容,但在尝试打开时包含 PDF 对象。我认为这两个过程有所不同。 $_FILES [5] [6]指向文件夹 binaries/tmp 中的临时文件,它以不同的格式存储。看起来 file_get_contents($name)不等于 file_get_contents($_FILES [7] [8])。

任何帮助赞赏。