1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Unicode output in downloaded code

Discussion in 'PHP' started by mirosoft1, Nov 13, 2017.

  1. #1
    This code is to download images, doc and PDF files, it is not working it gives to me Unicode characters on the web page not download any format of files like this
    %PDF-1.3 1 0 obj <> stream ����JFIF����C $.' ",#(7),01444'9=82<.342��C �� "v"�� ���}!1AQa"q2���#B��R��$3br� %&'(�����������������������������������������

    the code is
    
    <?php
    session_start();
    include("global.php");
    $path = strip_tags(intval($_GET["path"])) ;
    $type = strip_tags(intval($_GET["type"])) ;
    
    function output_file($file, $name)
    {
    
    //Check the file premission
    if(!is_readable($file)) die('File not found or inaccessible!');
    
    $size = filesize($file);
    $name = rawurldecode($name);
    
    
    //turn off output buffering to decrease cpu usage
    @ob_end_clean();
    
    // required for IE, otherwise Content-Disposition may be ignored
    if(ini_get('zlib.output_compression'))
    ini_set('zlib.output_compression', 'Off');
    
    header("Content-Type: application/force-download");
    header('Content-Disposition: attachment; filename="'.$name.'"');
    header("Content-Transfer-Encoding: binary");
    header('Accept-Ranges: bytes');
    
    /* The three lines below basically make the
    download non-cacheable */
    header("Cache-control: private");
    header('Pragma: private');
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    
    // multipart-download and download resuming support
    if(isset($_SERVER['HTTP_RANGE']))
    {
    list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2);
    list($range) = explode(",",$range,2);
    list($range, $range_end) = explode("-", $range);
    $range=intval($range);
    if(!$range_end) {
    $range_end=$size-1;
    } else {
    $range_end=intval($range_end);
    }
    
    /* */
    
    $new_length = $range_end-$range+1;
    header("HTTP/1.1 206 Partial Content");
    header("Content-Length: $new_length");
    header("Content-Range: bytes $range-$range_end/$size");
    } else {
    $new_length=$size;
    header("Content-Length: ".$size);
    }
    
    /* Will output the file itself */
    $chunksize = 1*(1024*1024); //you may want to change this
    $bytes_send = 0;
    if ($file = fopen($file, 'r'))
    {
    if(isset($_SERVER['HTTP_RANGE']))
    fseek($file, $range);
    
    while(!feof($file) &&
    (!connection_aborted()) &&
    ($bytes_send<$new_length)
    )
    {
    $buffer = fread($file, $chunksize);
    print($buffer); //echo($buffer); // can also possible
    flush();
    $bytes_send += strlen($buffer);
    }
    fclose($file);
    } else
    //If no permissiion
    die('Error - can not open file.');
    //die
    die();
    }
    
    
    if($type==1){
    $TheTask=tasksDalc::getById($path);
    $file_path="media/attachment/$TheTask->compid/";
    $files =$TheTask->attachment;
    
    }elseif($type==2){
    $compid = strip_tags(intval($_GET["compid"])) ;
    $PostTask=postsDalc::getById($path);
    $file_path="media/task/$compid/";
    $files =$PostTask->attachment;
    
    }elseif($type==3){
    $TheTickets=ticketsDalc::getById($path);
    $file_path="media/support/$TheTickets->id/";
    $files =$TheTickets->attachment;
    }
    
    set_time_limit(0);
    $fileName =$file_path.$files;
    output_file($fileName, $files);
    ?>
    
    PHP:
     
    mirosoft1, Nov 13, 2017 IP
  2. phpmillion

    phpmillion Member

    Messages:
    145
    Likes Received:
    11
    Best Answers:
    4
    Trophy Points:
    45
    #2
    Would you please re-write your question as clearly as possible? It's a hard task to understand the exact issue right now, but if you describe it clearly (with an example, if possible), I'm sure we will be able to assist you further.
     
    phpmillion, Nov 13, 2017 IP
  3. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    OK ,Now i have a form to download an attachment like images ,pdf and doc files ,This files stored in media folder included many folders with numbers of the tasks ,the link of download is
    
    <a href="download.php?path=<?php echo $TaskPost[$s]->id ;?>&compid=<?php echo $TheTask->compid ;?>&type=2">
    
    PHP:
    The type =2 here i check which type of folder i will download the attach from it
    the download.php
    
    <?php 
    session_start();
    include("global.php");
    $path = strip_tags(intval($_GET["path"])) ;
    $type = strip_tags(intval($_GET["type"])) ;
    
    function output_file($file, $name)
    {
    
     //Check the file premission
     if(!is_readable($file)) die('File not found or inaccessible!');
    
     $size = filesize($file);
     $name = rawurldecode($name);
    
    
     //turn off output buffering to decrease cpu usage
     @ob_end_clean(); 
    
    
     // required for IE, otherwise Content-Disposition may be ignored
     if(ini_get('zlib.output_compression'))
      ini_set('zlib.output_compression', 'Off');
    header(" Content-Type: text/html; charset=utf-8");
    header("Content-Type: application/force-download"); 
    header('Content-Disposition: attachment; filename="'.$name.'"');
     header("Content-Transfer-Encoding: binary");
     header('Accept-Ranges: bytes');
    
     /* The three lines below basically make the 
        download non-cacheable */
     header("Cache-control: private");
     header('Pragma: private');
     header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    
     // multipart-download and download resuming support
     if(isset($_SERVER['HTTP_RANGE']))
     {
        list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2);
        list($range) = explode(",",$range,2);
        list($range, $range_end) = explode("-", $range);
        $range=intval($range);
        if(!$range_end) {
            $range_end=$size-1;
        } else {
            $range_end=intval($range_end);
        }
       
        /*     */
       
        $new_length = $range_end-$range+1;
        header("HTTP/1.1 206 Partial Content");
        header("Content-Length: $new_length");
        header("Content-Range: bytes $range-$range_end/$size");
     } else {
        $new_length=$size;
        header("Content-Length: ".$size);
     }
    
     /* Will output the file itself */
     $chunksize = 1*(1024*1024); //you may want to change this
     $bytes_send = 0;
     if ($file = fopen($file, 'r'))
     {
        if(isset($_SERVER['HTTP_RANGE']))
        fseek($file, $range);
    
        while(!feof($file) && 
            (!connection_aborted()) && 
            ($bytes_send<$new_length)
              )
        {
            $buffer = fread($file, $chunksize);
            print($buffer); //echo($buffer); // can also possible
            flush();
            $bytes_send += strlen($buffer);
        }
     fclose($file);
     } else
     //If no permissiion
     die('Error - can not open file.');
     //die
    die();
    }
    
    
    if($type==1){
    $TheTask=tasksDalc::getById($path);
    $file_path="media/attachment/$TheTask->compid/";
    $files =$TheTask->attachment;
    
    }elseif($type==2){
    $compid = strip_tags(intval($_GET["compid"])) ;
    $PostTask=postsDalc::getById($path);
    $file_path="media/task/$compid/";
    $files =$PostTask->attachment;   
    
    }elseif($type==3){
    $TheTickets=ticketsDalc::getById($path);
    $file_path="media/support/$TheTickets->id/";
    $files =$TheTickets->attachment;   
    }
    
    set_time_limit(0);
    $fileName =$file_path.$files;
    output_file($fileName, $files);
    ?>
    
    PHP:

    when i run the code it open the attachment like Unicode characters and didn't download it
    there is no errors in error log
    
    $PostTask=postsDalc::getById($path);
    
    PHP:
    it is retrieve the data from database and it is working well
    i don't know why the file open like that and not downloaded well
    help me ,thanks
     
    mirosoft1, Nov 13, 2017 IP
  4. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #4
    The output of file check attach please
     

    Attached Files:

    mirosoft1, Nov 13, 2017 IP
  5. phpmillion

    phpmillion Member

    Messages:
    145
    Likes Received:
    11
    Best Answers:
    4
    Trophy Points:
    45
    #5
    You may want to try this code to download files, it works great for me:

    $fp=fopen("PATH_TO_YOUR_FILE", "rb");
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=NAME_OF_YOUR_FILE");
    header("Content-Length: ".filesize(PATH_TO_YOUR_FILE));
    fpassthru($fp);
    PHP:
     
    phpmillion, Nov 14, 2017 IP
  6. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #6
    Did you mean to replace this code
    
    header(" Content-Type: text/html; charset=utf-8");
    header("Content-Type: application/force-download");
    header('Content-Disposition: attachment; filename="'.$name.'"');
    header("Content-Transfer-Encoding: binary");
    header('Accept-Ranges: bytes');
    
    PHP:
    with your code??
    
    $fp=fopen("PATH_TO_YOUR_FILE", "rb");
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=NAME_OF_YOUR_FILE");
    header("Content-Length: ".filesize(PATH_TO_YOUR_FILE));
    fpassthru($fp);
    
    PHP:
     
    mirosoft1, Nov 14, 2017 IP
  7. phpmillion

    phpmillion Member

    Messages:
    145
    Likes Received:
    11
    Best Answers:
    4
    Trophy Points:
    45
    #7
    You may want to give a try. If that doesn't help, we will at least know other part of code needs to be debugged.
     
    phpmillion, Nov 14, 2017 IP
  8. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #8
    Yes i try your code and didn't work ,it is still open the file with Unicode characters
     
    mirosoft1, Nov 14, 2017 IP
  9. phpmillion

    phpmillion Member

    Messages:
    145
    Likes Received:
    11
    Best Answers:
    4
    Trophy Points:
    45
    #9
    OK, so we know the issue is not with headers. Did you try debugging (echocing) some essential variables like $name and others? Maybe your function is trying to open some file which actually doesn't exist (especially if you use rawurldecode, as it can be decoded wrong or not actually encoded at some point in your script).
     
    phpmillion, Nov 14, 2017 IP
  10. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #10
    Yes i debugging the code many times and the code open the file exactly and i check the database i see all is alright
     
    mirosoft1, Nov 14, 2017 IP