gpt4 book ai didi

php - 将 PHP 文件上传到 MySQL mediumblob 的错误

转载 作者:行者123 更新时间:2023-11-29 03:05:57 26 4
gpt4 key购买 nike

我有一个脚本,允许用户上传 DOC、DOCX、PDF 和 TXT 类型的文件。文件确实会上传,尽管没有换行符。例如,像这样的 TXT 文件:

//////////
THIS IS A TEXT FILE
//////////

下载回来:

//////////THIS IS A TEXT FILE//////////

现在这不是文本文件的“大”问题,但这种换行符的丢失会完全破坏 PDF、DOC 和 DOCX 类型。

同样,在记事本中查看的 PDF 最初看起来像这样:

%PDF-1.5
%µµµµ
1 0 obj
<</Type/Catalog/Pages 2 0 R/Lang(en-US) /StructTreeRoot 10 0 R/MarkInfo<</Marked true>>>>
endobj
2 0 obj
....

但是,上传和下载后,它看起来像一个长字符串,有些字符丢失了:

%PDF-1.5%µµµµ1 0 obj<>>>endobj2 0 obj

因此,当在 Adob​​e PDF 阅读器中打开文件时,它说它已损坏。


这是我用来上传文件的 PHP 脚本的摘录:

$_POST['request_id'] = $_POST['rid'];
$tmpName = $_FILES['exam_file']['tmp_name'];
$_POST['name'] = $_FILES['exam_file']['name'];
$_POST['type'] = $_FILES['exam_file']['type'];
$_POST['size'] = $_FILES['exam_file']['size'];
//File types allowed are PDF, DOC, DOCX, TXT
if ( ( $_POST['type'] == "application/pdf") || ($_POST['type'] == "application/msword") || ($_POST['type'] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") || ($_POST['type'] == "text/plain") ){
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
fclose($fp);
$file_id = $model -> addFiles($_POST, $content);
}

$model中的addFiles()函数如下:

function addFiles($data, $file){
$table = 'faculty_files';
$data = parent::clean($data);
$keys = array('request_id', 'name', 'type', 'size', 'content');
$data = parent::cleanAndPick( $keys, $data );
$data['content'] = $file;
$data['faculty_user_id'] = $_SESSION['user_details']['id'];
$data['inserted_on'] = date('Y-m-d H:i:s');
parent::insert( $table, $data );
return mysql_insert_id();
}

parent::insert() 只是获取所有相关细节,例如表名和字段,并将它们放入 SQL 语句中。

这是函数 parent::clean() 尽管 $file 没有通过它运行:

public function clean($data = NULL){
$data = str_replace("\n", "", $data);
$data = str_replace("\r", "", $data);
$data = stripslashes($data);
return mysql_real_escape_string($data);
}

函数 parent::cleanAndPick()

public function cleanAndPick($keys, $data = NULL) {

$clean = array();
foreach ($keys as $key) {
if (array_key_exists($key, $data))
$clean[$key] = $this -> clean($data[$key]);
}
return $clean;

}

函数 parent::insert()

protected function insert($table, $data ) {

//Build sql
$data = $this -> clean($data);
$sql = "INSERT INTO " . $this -> clean($table);
$sql .= " (" . implode(",", array_keys($data)) . ") VALUES (";
foreach ($data as $value)
$sql .= ($value == NULL ? "NULL," : "'" . $value . "',");
$sql = substr($sql, 0, -1) . ")";
return $this -> query($sql);

}

这是我用来下载文件的 PHP 脚本:

$file = $model -> retrieveFile($_POST['fid']);
header("Content-length: " . $file[0]['size'] . "");
header("Content-type: " . $file[0]['type'] . "");
header("Content-Disposition:attachment; filename=" . $file[0]['name'] . "");
print $file[0]['content'];

我不知道是什么导致了这个问题。我将不胜感激任何意见!

编辑:有人在 PHP forum 上遇到了类似的问题.摘录:

I have been trying unsuccessfully to upload and read a Mac OS file on a Linux server. Lots of records show up a just one big using only the following:

<?php $fhandle = fopen($file, 'r'); ?>    or  <?php $fhandle =
> fopen($file, 'rb'); ?>

It does work, however, this way:

> <?php  ini_set('auto_detect_line_endings', TRUE);  $fhandle =
> fopen($file, 'r'); ?>

我确实试过了,但它对我不起作用。

最佳答案

如果您不想对其内容进行任何更改,请使用 fopen($tmpName, 'rb');

然后是:

protected function insert($table, $data ) {
//Build sql
$data = $this -> clean($data); //<---- here......

关于php - 将 PHP 文件上传到 MySQL mediumblob 的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15228540/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com