【发布时间】:2014-09-11 16:21:20
【问题描述】:
我的问题:我想用 BLOB 将图像插入到 MySQL 表中。在同一个项目中,我上传了一个文件,但只是 VARCHAR 列中的链接,它可以工作。现在我尝试使用file_get_contents 和fread,它们都返回空字符串。我的代码有什么问题?还是php.ini的配置有问题?代码是:
$imgdata = NULL;
$imgext = NULL;
$file = $_FILES['foto'];
if (!in_array($file['type'], $con->ext)) {
exit('Archivo no permitido');
} else {
if ($file['error'] === FALSE) {
exit('Error ' . $file['error']);
} else {
$attachtmp = $file['tmp_name'];
$imgext = $file['type'];
if (file_exists($attachtmp)) {
if (is_uploaded_file($attachtmp)) {
$fp = fopen($attachtmp, 'r+b');
$imgdata = fread($fp, filesize($attachtmp));
fclose($fp);
//if (empty(file_get_contents($attachtmp))) {
//$imgdata = $con->real_escape_string(file_get_contents($attachtmp));
//}
} else {
exit('<h3>Error interno del servidor<h3>');
}
} else {
exit('<h3>Error error interno del servidor<h3>');
}
}
}
【问题讨论】:
-
$file = $_FILES['foto']['name'];或$file = $_FILES['foto']['tmp_name']; -
您从来不用检查上传是否成功。你只是假设它做到了。如果上传失败,则
['tmp_name']将指向一个不存在或已损坏的文件。 -
如何验证文件是否损坏?
-
echo "Error: " . $_FILES["foto"]["error"] . "<br>";
标签: php mysql file-get-contents fread