Jump to content

force download help


mike760534211

Recommended Posts

i am using a force download of files and the script i am using is:
<?php

$file = $_REQUEST['file'];

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

header("Content-Type: application/force-download");
header( "Content-Disposition: attachment; filename=".basename($file));

header( "Content-Description: File Transfer");
header('Accept-Ranges: bytes');
header('Content-Length: ' . filesize($file));
@readfile($file);

?>
and it is called from the following script:
<?php

function direcho($path) {
global $filetotal, $fullsize, $totaldirs;
  $ignore = array(".php",".gif");
    $ext = strtoupper(substr(strrchr($file_name, "."), 1));
if ($dir = opendir($path)) {
  while (false !== ($file = readdir($dir))) {
  if (is_dir($path."/".$file)) {                    // if it's a dir, check it's contents too
  if ($file != '.' && $file != '..' ) {            // but dont go recursive on '.' and '..'
    echo '<li><img src="folder.gif"><font color=red size=5>' . $file . '</font></li><ul>';
    direcho($path."/".$file );
    echo '</ul>';
    $totaldirs++;
  }
  }
  elseif(!stripos($file, ".php") && !stripos($file, ".gif")) {                      //if it's not a dir, just output.
  $tab = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  $filesize = $tab . '(' . filesize ($path.'/'.$file ) . ' bytes)';
  echo "<li><A HREF=download.php?file=$file>$file</a></li>";
  $fullsize = $fullsize + filesize ($path.'/'.$file);
  $filetotal++;
  }
}
closedir($dir);
}
}

direcho('.');

$fullsize = round($fullsize / 1024 / 1024, 2);

echo "<font size=4 face=\"Arial, Helvetica, sans-serif\" color=red>
Total files - $filetotal files

Total dirs - $totaldirs directories

Total size - $fullsize MB
";
?>
which lists all the files in a directory, generatesa a link to the file and then forces the download from the link.
the problem is that when the download runs it does not download the complete file.  it makes the file a random size when downloading and does not force download the entire file.

can someone help with this please.
mike
Link to comment
https://forums.phpfreaks.com/topic/33389-force-download-help/
Share on other sites

When I download one of the files and open it in note page it contains the following error message:
<br />
<b>Warning</b>:  filesize() [<a href='function.filesize'>function.filesize</a>]: stat failed for [file.ext] in <b>%webroot%\download.php</b> on line <b>14</b><br />
Link to comment
https://forums.phpfreaks.com/topic/33389-force-download-help/#findComment-156346
Share on other sites

in my script it lists the items in a directory and all the subdirectories when you download a file that is not in a sub-directory it will download the file fine.  but if you click a link to a file that is in a sub direcotry it wont download the file correctly because it cant read the file size.  how would i correct this.

function direcho($path) {
global $filetotal, $fullsize, $totaldirs;
  $ignore = array(".php",".gif");
    $ext = strtoupper(substr(strrchr($file_name, "."), 1));
if ($dir = opendir($path)) {
  while (false !== ($file = readdir($dir))) {
  if (is_dir($path."/".$file)) {                    // if it's a dir, check it's contents too
  if ($file != '.' && $file != '..' ) {            // but dont go recursive on '.' and '..'
    echo '<li><img src="folder.gif"><font color=red size=5>' . $file . '</font></li><ul>';
    direcho($path."/".$file );
    echo '</ul>';
    $totaldirs++;
  }
  }
  elseif(!stripos($file, ".php") && !stripos($file, ".gif")) {                      //if it's not a dir, just output.
  $tab = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  $filesize = $tab . '(' . filesize ($path.'/'.$file ) . ' bytes)';
  echo "<li><A HREF=download.php?file=$file>$file</a></li>";
  $fullsize = $fullsize + filesize ($path.'/'.$file);
  $filetotal++;
  }
}
closedir($dir);
}
}

Link to comment
https://forums.phpfreaks.com/topic/33389-force-download-help/#findComment-156402
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.