我无法将pdf上传到我的mysql数据库 [英] I can't upload pdf to my mysql database

查看:191
本文介绍了我无法将pdf上传到我的mysql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP初学者,我正在尝试将PDF上传到我的MySQL数据库.我尝试添加一些代码以使其与pdf兼容,但是它无法正常工作,因此我将其删除,并且我拥有可以上载.txt,word文档,图像等但不能上载PDF的PHP脚本.您建议我添加些什么,使其适用于PDF.这是我的脚本.

    <html>
<head></head>
<body>
<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1"
cellspacing="1" class="box">
<tr>
<td>Select a file to upload</td>
</tr>
<tr>
<td>
<input type="hidden" name="MAX_FILE_SIZE"
value="16000000">
<input name="userfile" type="file" id="userfile"> 
</td>
</tr>
<tr>
<td width="80"><input name="upload"
type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
</body>
</html>
<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fileType=(get_magic_quotes_gpc()==0 ? mysql_real_escape_string(
$_FILES['userfile']['type']) : mysql_real_escape_string(
stripslashes ($_FILES['userfile'])));
$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}
$con = mysql_connect('localhost', 'root', '') or die(mysql_error());
$db = mysql_select_db('test', $con);
if($db){
$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
mysql_query($query) or die('Error, query failed'); 
mysql_close();
echo "<br>File $fileName uploaded<br>";
}else { echo "file upload failed"; }
} 
?>

解决方案

魔术引号已被弃用很长时间了.您不应该再使用它.从PHP 5.4开始,它已 已从语言中删除 .特别是在编写新脚本时,应避免使用此废弃功能.

如果文件很大,可以使用PHP文件上传脚本进行处理,那么您可能会对更改post_max_size等设置感兴趣.有关更多详细信息,请参见此主题:增加最大帖子大小

您可以使用通用文字处理功能添加斜杠来代替应该使用与您的数据库系统匹配的转义功能.在这种情况下,它是 mysqli_real_escape_string . 由于PDF文件包含二进制数据而没有文本,因此在保存和阅读后(文本处理),您不应添加和删除斜杠.将数据插入数据库时​​,只需使用适当的MySQL函数逃逸二进制内容blob. 适用于整个文件的列类型为 MEDIUMBLOB .它允许的数据长度最大为〜16 MB.

在讨论了php方面之后,还有一些关于MySQL的提示. MySQL限制了发送给它的数据包的长度.如果您使用共享托管平台(没有专用服务器),则很有可能被限制为仅1 MB.相关配置选项为 max_allowed_pa​​cket .此设置将限制在数据库中存储文档的能力.有关如何解决此问题的想法,请参见此线程. >

在我看来,在大多数情况下将整个文档存储到关系数据库中是个坏主意.我通常将文件元数据(大小,文件名,MIME类型...)放入数据库表中,并将上传的二进制数据存储在公众不可读的普通文件系统目录中(例如/srv/uploads).这样,文件就可以随心所欲地变大而不会牺牲数据库的性能.

I am a PHP beginner and I am trying to upload PDF to my MySQL database. I tried adding some code to make it pdf compatible but it didn't work so i removed it and I have the PHP script that can upload .txt, word docs, images, etc but not PDF. What do you you suggest I should add to it so it works for PDF. Here's my script.

    <html>
<head></head>
<body>
<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1"
cellspacing="1" class="box">
<tr>
<td>Select a file to upload</td>
</tr>
<tr>
<td>
<input type="hidden" name="MAX_FILE_SIZE"
value="16000000">
<input name="userfile" type="file" id="userfile"> 
</td>
</tr>
<tr>
<td width="80"><input name="upload"
type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
</body>
</html>
<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fileType=(get_magic_quotes_gpc()==0 ? mysql_real_escape_string(
$_FILES['userfile']['type']) : mysql_real_escape_string(
stripslashes ($_FILES['userfile'])));
$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}
$con = mysql_connect('localhost', 'root', '') or die(mysql_error());
$db = mysql_select_db('test', $con);
if($db){
$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
mysql_query($query) or die('Error, query failed'); 
mysql_close();
echo "<br>File $fileName uploaded<br>";
}else { echo "file upload failed"; }
} 
?>

解决方案

Magic quotes have been deprecated for a long time. You shouldn't use it anymore. Since PHP 5.4 it is removed from the language. Especially, when writing new scripts you should avoid this abandoned feature.

If your file is to big to be processed using a PHP file upload script, you might be interested in changing settings like post_max_size. See this thread for more details: Increasing the maximum post size

Instead of the general-use text manipulating function addslashes you should use the escaping function matching your database system. In this case it is mysqli_real_escape_string. As PDF files contain binary data and no text, you shouldn't add and remove slashes on saving and after reading (text processing). Just escape the binary content blob using the adequate MySQL function when inserting the data into the database. A suitable column type for entire files is MEDIUMBLOB. It allows a data length of up to ~16 MB.

After having talked about the php side, a few more hints concerning MySQL. MySQL limits the length of data packets being sent to it. If you use a shared hosting platform (and no dedicated server), chances are high of being limited to only 1 MB. The relevant configuration option is max_allowed_packet. This setting will limit the ability to store documents in the database. See this thread for ideas on how to resolve this problem.

In my opinion it's a bad idea in most cases to store entire documents into a relational database. I usually put the file meta data (size, filename, MIME type, ...) into a database table and store the uploaded binary data in a normal file system directory that isn't readable to the public (e.g. /srv/uploads). Then your files can become as big as you want without sacrificing your database's performance.

这篇关于我无法将pdf上传到我的mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆