file download issue

Discussion in 'PHP' started by dizyn, Nov 13, 2007.

  1. #1
    I am having problem with file download, It downloads file but when i try to open it it says file corrupted.

    here is my code:
    $dir=USER_DOCUMENT_PATH;
    if (isset($_REQUEST["action"])) {
        $file=$dir.$file;
        header("Content-type: application/force-download");
        header("Content-Transfer-Encoding: Binary");
        header("Content-length: ".filesize($file));
        header("Content-disposition: attachment; filename=\"".basename($file)."\"");
        readfile("$file");
    } else {
        echo "No file selected";
    }
    PHP:
    here is page link:
    http://www.ujueta.com/file_download.php?product_id=37&action=download&file_no=2

    any idea?
     
    dizyn, Nov 13, 2007 IP
  2. salmanshafiq

    salmanshafiq Well-Known Member

    Messages:
    260
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    128
    #2
    dear try this code it's working fine in my code


    
    <?php
    $filename = $_REQUEST['file'];
    
    // required for IE, otherwise Content-disposition is ignored
    /*
    if(ini_get('zlib.output_compression'))
      ini_set('zlib.output_compression', 'Off');
    */
    // addition by Jorg Weske
    $file_extension = strtolower(substr(strrchr($filename,"."),1));
    
    if( $filename == "" ) 
    {
      echo "<html><title>Salman'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>Salman'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: private"); // 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();
    ?>
    
    
    PHP:
     
    salmanshafiq, Nov 14, 2007 IP
  3. singh.ajit05

    singh.ajit05 Peon

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    <?php
    $extlimit == "yes";
    $limitedext = array(".jpg",".jpeg",".gif");
    $uploadfile="";
    $fileatt=$_FILES['fileatt']['tmp_name'];
    $fileatt_type = $_FILES['fileatt']['type'];
    $fileatt_name = $_FILES['fileatt']['name'];
    // code for check
    $ext = strrchr($_FILES['fileatt']['name'],'.');
    if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
    echo("Wrong file extension. ");
    exit();
    }

    if($fileatt_name!="")
    {
    $uploadfile=$cpath."resume/".$fileatt_name;
    if (($fileatt_type=="image/pjpeg") || ($fileatt_type=="image/jpeg") || ($fileatt_type=="image/gif"))
    {
    @move_uploaded_file($fileatt , $uploadfile) or die('Resume upload problem.');
    }
    }
    $to="singh.ajit05@hotmail.com";
    $from = $_POST['EMAIL_ADDRESS'];
    $subject = "Just send testing mail";
    $message = "
    Name :- ".$_POST[name]."\n
    E-mail Id :- ".$_POST
     
    singh.ajit05, Apr 1, 2008 IP