Jump to content

php download


jagguy

Recommended Posts

I am testing the download code.

 

When i run this code the file downloads OK but it appends the html code to the file so i get the file and html file it is in?

How does this happen?

I just want a link to a file then it downloads.

 

<body>
<div id="boxlayer1" >
<div id="boxlayer2" class="lbox">5eareresae56</div>
<div id="boxlayer3" class="lbox">reerertertert</div>
<div id="boxlayer4" class="lbox">retererertert</div>
</div>
<div id="content1" >
jlkjkljkljlkjkljklj
<?php

$file="linuxhelp.txt";

//    $dir      = "../log/exports/";
    if ((isset($file))&&(file_exists($file))) {
       header("Content-type: application/force-download");
       header('Content-Disposition: inline; filename="' .$file . '"');
       header("Content-Transfer-Encoding: Binary");
       header("Content-length: ".filesize($file));
       header('Content-Type: application/octet-stream');
       header('Content-Disposition: attachment; filename="' . $file . '"');
       readfile("$file");
    } else {
       echo "No file selected";
    } //end if


?>
</div>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/57237-php-download/#findComment-283923
Share on other sites

I put the code to download in a seprate file and it downloads without extra html as before.

 

q)So if I called a script to download via a Get(div2.html) then the php file runs (div3.html)and the browser doesn't goto div3.html , so i have no need for a redirect like

header( "Location: http://localhost/school/test/div2.html" );

why?

 

q) will this script handle pdf .xls and other file types?

 

div2.html

...
<body>
<div id="boxlayer1" >
<div id="boxlayer2" class="lbox">5eareresae56</div>
<div id="boxlayer3" class="lbox">reerertertert</div>
<div id="boxlayer4" class="lbox">retererertert</div>
</div>
<div id="content1" >
jlkjkljkljlkjkljklj
<?php
$file="linuxhelp.txt";
echo   "<br> <a href='div3.html?file=".$file."'>file download</a>";

 

div3.html

 

$file=$_GET['file'];
//echo "sss";

if ((isset($file))&&(file_exists($file))) {
  header("Content-type: application/force-download");
  header('Content-Disposition: inline; filename="' .$file . '"');
  header("Content-Transfer-Encoding: Binary");
  header("Content-length: ".filesize($file));
  header('Content-Type: application/octet-stream');
  header('Content-Disposition: attachment; filename="' . $file . '"');
  readfile("$file");
// exit();
} else {
    echo "No file selected";
} //end if

Link to comment
https://forums.phpfreaks.com/topic/57237-php-download/#findComment-284589
Share on other sites

 

There you go ok.

<?php

$filename = $_GET['file'];

// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
  ini_set('zlib.output_compression', 'Off');


$file_extension = strtolower(substr(strrchr($filename,"."),1));

if( $filename == "" ) 
{
  echo "<html><title>eLouai's Download Script</title><body>ERROR: download file NOT SPECIFIED. USE force-download.php?file=filepath</body></html>";
  exit;
} elseif ( ! file_exists( $filename ) ) 
{
  echo "<html><title>eLouai's Download Script</title><body>ERROR: File not found. USE force-download.php?file=filepath</body></html>";
  exit;
};
switch( $file_extension )
{
  case "pdf": $ctype="application/pdf"; break;
  case "exe": $ctype="application/octet-stream"; break;
  case "zip": $ctype="application/zip"; break;
  case "doc": $ctype="application/msword"; break;
  case "xls": $ctype="application/vnd.ms-excel"; break;
  case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
  case "gif": $ctype="image/gif"; break;
  case "png": $ctype="image/png"; break;
  case "jpeg":
  case "jpg": $ctype="image/jpg"; break;
  default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers 
header("Content-Type: $ctype");
// change, added quotes to allow spaces in filenames, by Rajkumar Singh
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();

?>
    

Link to comment
https://forums.phpfreaks.com/topic/57237-php-download/#findComment-284593
Share on other sites

Hi,

I can force a download file with this code and it works.

 

I know what the switch statement does but I am unsure about the rest.

Is all this code needed for a forced download?

 

 

$file_extension = strtolower(substr(strrchr($file,"."),1));

if ((isset($file))&&(file_exists($file)))

{

switch( $file_extension )

{

case "pdf": $ctype="application/pdf"; break;

case "exe": $ctype="application/octet-stream"; break;

...

}

header("Pragma: public"); // required

header("Expires: 0");

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

header("Cache-Control: private",false); // required for certain browsers

header("Content-type: application/force-download");

header('Content-Disposition: inline; filename="' .$file . '"');

header("Content-Transfer-Encoding: Binary");

header("Content-length: ".filesize($file));

 

header("Content-Type: $ctype");

header('Content-Disposition: attachment; filename="' . $file . '"');

readfile("$file");

Link to comment
https://forums.phpfreaks.com/topic/57237-php-download/#findComment-287114
Share on other sites

Just a quick problem I am downloading a file from a dir and it works fine if I store the file in the same dir as the php file.

 

I want to download from a f'iles/filename' dir but I  can't get it to work from that dir. Also having a / or \ slash for dir as I am using windows(don't ask) but will host it on linux which is better.

 

Thi code fails because of the file to download is not in current dir

 

$file= $_GET['file'];

$path= $_GET['path'];

 

$file_path=$file.'/'.$path;

 

    $file_extension = strtolower(substr(strrchr($file,"."),1));

    if ((isset($file))&&(file_exists($file_path)))

      {

        switch( $file_extension )

          {

          case "pdf": $ctype="application/pdf"; break;

        ..  default: $ctype="application/force-download";

          }

         

                header("Content-type: application/force-download");

          header('Content-Disposition: inline; filename="' .$file_path . '"');

          header("Content-Transfer-Encoding: Binary");

          header("Content-length: ".filesize($file));

     

          header("Content-Type: $ctype");

          header('Content-Disposition: attachment; filename="' . $file . '"');

          readfile("$file");

 

      }

      else

        echo "No file selected";

 

Link to comment
https://forums.phpfreaks.com/topic/57237-php-download/#findComment-287513
Share on other sites

My downloads fail to work since i moved them into another folder.

I am getting tired and cant work it out. The files exists and I get nothing in the downloaded files. They are all txt files .


$file= $_GET['file'];
$path= $_GET['path'];

//$file_path=$path.'/'.$file;
$file_path = $_SERVER['DOCUMENT_ROOT'] . '/school/test/files/' . $file;

     $file_extension = strtolower(substr(strrchr($file,"."),1));
     if ((isset($file))&&(file_exists('files/'.$file)))
       {


         switch( $file_extension )
          {
          case "pdf": $ctype="application/pdf"; break;
          case "exe": $ctype="application/octet-stream"; break;
          case "zip": $ctype="application/zip"; break;
          case "doc": $ctype="application/msword"; break;
          case "xls": $ctype="application/vnd.ms-excel"; break;
          case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
          case "gif": $ctype="image/gif"; break;
          case "png": $ctype="image/png"; break;
          case "jpg": $ctype="image/jpg"; break;
          default: $ctype="application/force-download";
          }
          
             header("Content-type: application/force-download");
           header('Content-Disposition: inline; filename="' .$file_path . '"');
           header("Content-Transfer-Encoding: Binary");
           header("Content-length: ".filesize($file));
       
           header("Content-Type: $ctype");
           header('Content-Disposition: attachment; filename="' . $file . '"');
           readfile("$file");

       }
       else
         echo "No file selected";

Link to comment
https://forums.phpfreaks.com/topic/57237-php-download/#findComment-289441
Share on other sites

This works

...

    header("Content-type: application/force-download");

        //  header('Content-Disposition: inline; filename="' .$file_path . '"');

          header("Content-Transfer-Encoding: Binary");

        //  header("Content-length: ".filesize($file_path));

            header("Content-length: ".filesize($file_path));

 

          header("Content-Type: $ctype");

        //  header('Content-Disposition: attachment; filename="' . $file_path . '"');

            header('Content-Disposition: attachment; filename="' . $file . '"');

 

          // readfile("$file_path");

            readfile("$file_path");

Link to comment
https://forums.phpfreaks.com/topic/57237-php-download/#findComment-289486
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.