Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

php - warning feof() expects parameter 1 to be resource

My error logs are getting out of control with the two below errors

warning feof() expects parameter 1 to be resource

and

warning fread() expects parameter 1 to be resource

The bit of code responsible is

<?php
    $file = '../upload/files/' . $filex;
    header("Content-Disposition: attachment; filename=" . urlencode($file));
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header("Content-Description: File Transfer");
    header("Content-Length: " . filesize($file));
    flush(); // this doesn't really matter.

    $fp = fopen($file, "r");
    while (!feof($fp)) {
        echo fread($fp, 65536);
        flush(); // this is essential for large downloads
    }
    fclose($fp);
?> 

I used this code for header downloads but its freaking out right now - before anyone asks what I have tried, I tried google but still don't fully understand the error message.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

fopen fails and returns false. false is not a resource, thus the warning.

You'd better test $fp before injecting it as a resource-like argument:

if(($fp = fopen($file, "r"))) {
    [...]
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...