Using the script below, I am trying to generate a hyperlink that when clicked on or right-clicked on, will enable the user to download the files that are contained within the specified directory.

However, when I attempt to open the page that contains the script, I get the following error message:

Warning: filesize(): Stat failed for content/documents/upload_files/Resource id #17 (errno=2 - No such file or directory) in /home/public_html/content/documents/doc_002.php on line 96

Warning: fread(): supplied argument is not a valid stream resource in /home/public_html/content/documents/doc_002.php on line 96

Warning: fclose(): supplied argument is not a valid stream resource in /home/public_html/content/documents/doc_002.php on line 98

<?php			
$current_dir = "content/documents/upload_files";
$dir = opendir($current_dir);
  while (false !== ($file = readdir($dir))) {
    if($file == $doc_name) {
      if ($file != "." && $file != "..") {
        $f4view = fopen("$current_dir/$file", "rb");
        $f4view = fread("$current_dir/$f4view", filesize("$current_dir/$f4view"));
        echo "<a href=\"content/documents/upload_files/$f4view\">$f4view</a><br>";
        fclose("$current_dir/$f4view");
      }
    }
  }
closedir($dir); 
?>

Any suggestions?

revez2002 😕

    read a bit more on [man]fopen[/man] [man]fread[/man] and [man]filesize[/man]. you are passing a resource into the functions where it shouldnt be.

      I have reviewed the documentation on all of the functions (fopen fread and filesize) that drew010 suggested. I also tried making different modifications to the code, but I still can't get it work.

      Any more hints?

      revez2002

        This line is right.

         $f4view = fopen("$current_dir/$file", "rb"); 

        Assuming that the file exists.

        This line is wrong.

        $f4view = fread("$current_dir/$f4view",//....

        fread() expects the first argument to be a file handle resource (as returned by fopen()), not a filename. Ditto for the fclose().

        So what you're doing is opening a file, putting the file handle (Resource #17 as it happens) into $f4view. Then you're (a) trying to use "Resource #17" as a file name in filesize() and fread() (which is why you get the error messages saying that the file "Resource #17" couldn't be found), and (b) even if you got something back, you're trying to put it into $f4view (losing your file handle in the process).

        Continued study of the manual pages should answer this. Eg., the first line of the description of fopen says:
        resource fopen ( string filename, string mode [, bool use_include_path [, resource zcontext]] )
        and that of fread says:
        string fread (resource handle, int length )
        (Emphasis mine.)

        Of course, both lines and the fclose() could be replaced by a single call to [man]file_get_contents[/man].

          Thank you very much for the additional insight Weedpacket.

          After going back and reviewing the documentation on the fopen() and fread() functions, I modified the code as follows:

          <?php	
            $upload_directory = "content/documents/upload_files"; 
            $dir = opendir($upload_directory); 
          
          while (false !== ($file = readdir($dir))) { 
            if($file == $doc_name) { 
              if ($file != "." && $file != "..") { 
                $handle = fopen("$upload_directory/$file", "rb"); 
                $f4view = fread($handle, filesize("$upload_directory/$file")); 
                echo "<a href=\"content/documents/upload_files/$file\">$file</a><br>"; 
                fclose($handle);
              } 
            } 						
          } 
          closedir($dir);
          ?>
          

          The page that this script appears on is no longer throwing errors, and I can now successfully download files that we have uploaded to our server.

          Thank you Weedpacket and drew010 for your guidance!

          Best regards,
          revez2002 🙂

            Write a Reply...