FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » FUDforum Development » Bug Reports » Download a large file with the admin panel (adm/admbrowse.php file download problem)
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Download a large file with the admin panel [message #164887] Tue, 29 March 2011 13:14 Go to previous message
Atomicrun is currently offline  Atomicrun   Sweden
Messages: 54
Registered: November 2010
Location: Lund
Karma:
Member
I made a backup of my server (Ghost drive-dump). Now, the server is a bit restricted in its configuration, so to move the backup file from the server, I tried to download using the admin file controls. I don't have FTP and I have turned off most other alternatives, like local network.

The admin file-controls-download is OK for small files, but it didn't work with my "bigger" file.

	/* Download file code. */
	if (isset($_GET['down']) && $dest && @file_exists($cur_dir .'/'. $dest)) {
		if (is_file($cur_dir .'/'. $dest)) {
			header('Content-type: application/octet-stream');
			header('Content-Disposition: attachment; filename='. $dest);
			readfile($cur_dir .'/'. $dest);
		} else {
			header('Content-type: application/x-tar');
			header('Content-Disposition: attachment; filename='. $dest .'.tar');
			echo make_tar($cur_dir .'/'. $dest);
		}
		exit;
	}


The PHP cannot run a large file by "readfile". The "readfile" is a problematic spot on the php, and you find many fixes, problems and issues on the www.php.net

I just need the file so I put in the following fix:

  • I included a function that send the file in "chunks".
  • I supply the file length to the download browser.
  • I disable (go around) the php timeout by sending "sleep( 0);"


Insert this function in the adm/admbrowse.php file, among the other functions:

function readfile_chunked( $filename, $retbytes=true) 
{
   $chunksize = 10*1024*1024; // how many bytes per chunk
   $buffer = '';
   $cnt =0;
   $handle = fopen($filename, 'rb');
   if ( $handle === false) 
   {
       return false;
   }
   
   while ( !feof( $handle)) 
   {
       $buffer = fread( $handle, $chunksize);
       echo $buffer;
       ob_flush();
       flush();
       sleep( 0);
       if ( $retbytes) 
       {
           $cnt += strlen($buffer);
       }
   }

   $status = fclose( $handle);
   if ( $retbytes && $status) 
   {
       return $cnt; // return num. bytes delivered like readfile() does.
   }
   return $status;
}



And then modify the file download to use this function (the function above):

	/* Download file code. */
	if (isset($_GET['down']) && $dest && @file_exists($cur_dir .'/'. $dest)) {
		if (is_file($cur_dir .'/'. $dest)) {
			header('Content-type: application/octet-stream');
			header('Content-Disposition: attachment; filename='. $dest);
			header('Content-Length: '. filesize( $cur_dir .'/'. $dest));
			// readfile($cur_dir .'/'. $dest);
                  readfile_chunked( $cur_dir .'/'. $dest);
		} else {
			header('Content-type: application/x-tar');
			header('Content-Disposition: attachment; filename='. $dest .'.tar');
			echo make_tar($cur_dir .'/'. $dest);
		}
		exit;
	}

#############################

We note that I cannot upload the backup file (that is large..) again when it has been downloaded. The upload would need a similar adjustment.
I have not included the fix into the directory download.











[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: No such tag: JEI -> $ ban_expiry.
Next Topic: an undefined function ...
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 24 10:14:13 GMT 2024

Total time taken to generate the page: 0.04655 seconds