Php 文件_get_内容不';不能使用tmp文件
我的问题是:我想用BLOB将一个图像插入MySQL表。在同一个项目中,我上传了一个文件,但只是一个VARCHAR列中的链接,它可以正常工作。现在我尝试了Php 文件_get_内容不';不能使用tmp文件,php,mysql,file-get-contents,fread,Php,Mysql,File Get Contents,Fread,我的问题是:我想用BLOB将一个图像插入MySQL表。在同一个项目中,我上传了一个文件,但只是一个VARCHAR列中的链接,它可以正常工作。现在我尝试了file\u get\u contents和fread,它们都返回空字符串。我的代码怎么了?还是php.ini的配置有问题?代码是: $imgdata = NULL; $imgext = NULL; $file = $_FILES['foto']; if (!in_array($file['type'], $con
file\u get\u 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>');
}
}
}
$imgdata=NULL;
$imgext=NULL;
$file=$_文件['foto'];
如果(!in_数组($file['type'],$con->ext)){
出口(“无许可证”);
}否则{
如果($file['error']==FALSE){
退出('Error'.$file['Error']);
}否则{
$attachtmp=$file['tmp_name'];
$imgext=$file['type'];
如果(文件_存在($attachtmp)){
如果(是否上载了文件($attachtmp)){
$fp=fopen($attachtmp,'r+b');
$imgdata=fread($fp,filesize($attachtmp));
fclose($fp);
//if(空(文件获取内容($attachtmp))){
//$imgdata=$con->real\u escape\u字符串(文件获取内容($attachtmp));
//}
}否则{
退出(“服务内部错误”);
}
}否则{
退出('Error Error interno del servidor');
}
}
}
首先检查您的结果:
// Check $_FILES['foto']['error'] value.
switch ($_FILES['foto']['error']) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_NO_FILE:
throw new RuntimeException('No file sent.');
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
throw new RuntimeException('Exceeded filesize limit.');
default:
throw new RuntimeException('Unknown errors.');
}
取自
$file=$\u文件['foto']['name']
或$file=$\u文件['foto']['tmp\u名称']代码>你从来没有检查过上传是否成功。你只是假设是这样。如果上载失败,则['tmp_name']
将指向不存在或已损坏的文件。如何验证文件是否已损坏?echo“错误:”$_文件[“foto”][“error”]。“
”代码>似乎?要么是,要么不是。你需要用一个安全的名字把文件移到一个安全的地方,然后打开它,请参阅我的帖子中的链接示例。是的,它已正确上载,因为该文件存在于临时php文件中。我以同样的方式上传了文件,只更改了移动上传文件的文件获取内容
,效果良好。