简体   繁体   中英

file_get_contents doesn't work with tmp file

My problem: I want to INSERT a image into a MySQL table with BLOB. In the same project I has upload a file but just the link in a VARCHAR column, and it works. Now I tried with file_get_contents and fread and both of them returns empty string. What's wrong with my code? Or is something wrong with the configuration of php.ini? The code is:

    $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 your results first:

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

taken from php manual

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM