file_get_contents不适用于tmp文件

时间:2014-09-11 16:21:21

标签: 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>');
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

首先检查您的结果:

// 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