php - file_get_contents 不适用于 tmp 文件

标签 php mysql file-get-contents fread

我的问题:我想使用 BLOB 将图像插入到 MySQL 表中。在同一个项目中,我上传了一个文件,但只是 VARCHAR 列中的链接,它可以工作。现在我尝试使用 file_get_contentsfread 并且它们都返回空字符串。我的代码有什么问题?还是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>');
            }
        }
    }

最佳答案

首先检查你的结果:

// 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.');
}

取自php manual

关于php - file_get_contents 不适用于 tmp 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25792512/

相关文章:

php - 是否可以将 html 类添加到 phpMyEdit 生成的表中?

php - PDOStatement 对象的 var_dump 没有显示返回的数据

Go 语言中的 PHP file_get_contents

PHP 500 内部服务器错误 file_get_contents

php - 使用 PDO 的更新语法错误

php - 缓存网页数据源

PHP if 语句与 isset 不起作用

php - MySQL 'Group By' 搞乱虚拟列计数

mysql - htaccess redirectmatch 有条件需要查询数据库

php file_get_contents - 在 javascript 执行之后