如何使用 PHP 获取文档内容并上传到 Mysql blob 字段?
当我运行此代码时,$testpage 是一个字母数字字符串,它会完美上传。但是,当使用 file_get_contents(...) 定义 $testpage 时,它根本不上传。
<?
...
...
mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die("Unable to select database");
$testpage = file_get_contents('http://us3.php.net/manual/en/function.file-get-contents.php');
$testpage = mysql_real_escape_string($testpage);
mysql_query("INSERT INTO theTable(Description,Document) VALUES('PHP Webpage test','$testpage')");
mysql_close;
?>
我从 PHP 文档中了解到 file_get_contents(...) 是将文件转换为可以存储在二进制字段中的字符串的首选方法。我知道我还需要处理一些更多的安全问题,但首先我只想能够进行原始上传并从那里继续。是否有任何原因导致这不起作用?如果是,最好的方法是什么?或者我只是错过了什么?
谢谢!
右
When I run this code, and $testpage is an alphameric string, it is uploaded perfectly. However, when $testpage is defined using file_get_contents(...), it does not upload at all.
<?
...
...
mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die("Unable to select database");
$testpage = file_get_contents('http://us3.php.net/manual/en/function.file-get-contents.php');
$testpage = mysql_real_escape_string($testpage);
mysql_query("INSERT INTO theTable(Description,Document) VALUES('PHP Webpage test','$testpage')");
mysql_close;
?>
I understood from the PHP docs that file_get_contents(...) was the preferred way to convert files into a string that could be stored in a binary field. I am aware that there are some more security issues that I will have to deal with but first I just want to be able to do the raw upload and proceed from there. Is there any reason why this should not work and if so, what is the best way to do this? Or am I just missing something?
Thanks!
R
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要正确转义字符串。
You need to properly escape the string.
我真的会考虑为您的数据库查询提供一个包装类。我在 http://stefangabos.ro/php-libraries/zebra- 上使用了一个很棒的数据库数据库/。
使用该包装器,查询将很简单:
由于包装器已经自动转义字符串,因此您可以在保留安全性和功能的同时减少代码。
I would really look into getting yourself a wrapper class for your database queries. There's a great one that I use at http://stefangabos.ro/php-libraries/zebra-database/.
Using that wrapper, the query would simply be:
As the wrapper already escapes strings automatically, you reduce code while retaining security and functionality.
如果您想实际请求网页并将其保存在 MySQL 中,我建议您使用
stream_get_contents
而不是file_get_contents
:我认为您可以更改 fopen 并将其指向您上传的文档(或文件):
希望这有帮助!
I suggest you use
stream_get_contents
instead offile_get_contents
if you would like to actually request for a webpage and save it in MySQL:I think you can just alter the fopen and point it to your uploaded document (or file):
Hope this helps!