PHP MYSQL文件内容转义问题 [英] PHP MYSQL file contents escape problem
问题描述
我尝试使用php将.pdf文件上传到mysql数据库。
除文件内容外,无论如何我试图逃避特殊字符,查询总是失败,主要是未知的命令\ n。
我已经使用addslashes,mysql_real_escape_string,removalashes等。
有没有人有关于如何转义文件内容的任何想法?
非常感谢,
我以前使用过下面的序列,这似乎很好地工作,并将任何数据存储到数据库,包括图像,PDF文件,数据的数组等等...:)
存储数据(可以是字符串,数组,对象等);
首先将数据转换成base64编码的字符串
$ $ $ $ $ $ $ strData = strtr
base64_encode(
addslashes(
gzcompress(serialize($ dataToStore),9)
)
),'+ / =','-_,');
然后将该字符串数据存储在数据库中...
检索数据;
从db中提取字符串数据
将数据解码为您想要的(您可能需要在此之后执行一个额外的步骤,具体取决于输入数据,数组,图像等)。
$ b $ (
$ base $ _decode(
strtr($ strDataFromDb,'-_',
$> $ $ $ $ $ $ $ $ $ $ $ $ $ $> ,'+ / =')
)
)
)
);
这确实帮助我存储了我需要存储在MySQL数据库中的数据!
I am attempting to upload a .pdf file into a mysql database using php.
It is all good except for the contents of the file. No matter how I seem try to escape special characters, the query always fails, mostly with "Unknown Command \n".
I have used addslashes, mysql_real_escape_string, removeslashes etc.
Does anyone have any ideas on how to escape file contents?
Many Thanks,
I've used the following sequence before, which seems to work nicely, and will store any data into the db, including images, pdfs, arrays of data, etc... :)
Storing the data (can be a string, array, object, etc.);
First, turn the data into a base64 encoded string
$strData = strtr(
base64_encode(
addslashes(
gzcompress( serialize($dataToStore) , 9)
)
) , '+/=', '-_,');
Then store that string data in the db...
Retrieving the data;
Extract the string data from the db
decode the data back to what you want (you may need to perform an extra step after this depending on the input data, array, image, etc.)
$returnData = unserialize(
gzuncompress(
stripslashes(
base64_decode(
strtr($strDataFromDb, '-_,', '+/=')
)
)
)
);
This certainly helped me to store what I needed to store in a mySQL db!
这篇关于PHP MYSQL文件内容转义问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!