Save filename with html / php whitespace

1

I have the following button that goes to a file:

<a href="download_file.php?file=ficheiro_xls/BD%20Fugas%20Gespost.xls">Download XLS</a>

In the download_file.php file I have the following:

<?php
header("Content-Type: application/octet-stream");

$file = $_GET["file"];
header("Content-Disposition: attachment; filename=" . urlencode($file));   
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");            
header("Content-Length: " . filesize($file));
flush(); // this doesn't really matter.
$fp = fopen($file, "r");
while (!feof($fp))
{
    echo fread($fp, 65536);
    flush(); // this is essential for large downloads
} 
fclose($fp); 

?>

It works correctly, the file is unloaded. But the goal is to save the file with the following name " BD Trails Gespost.xls " and is being saved as " xls% 2FBD + Trails + Gespost.xls " . I've tried as above to use %20 to replace space, but it does not work.

    
asked by anonymous 30.09.2014 / 15:41

3 answers

1

First, decide what Content-Type you want, you have 3 headers with Content-Type , and you should have one.

As the name of your file is already encoded, you can take urlencode from $file :

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

However, since it has a folder name in $file , it might be the case of just getting the file name:

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

Alternatively:

Eventually you could do it differently, without encode it in the link:

<a href="download_file.php?file=ficheiro_xls/BD FugasGespost.xls">Download XLS</a>

Keep urlencode in header :

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

and perhaps changing the opening of $file , but it depends on what the real file name is in the filesystem:

$fp = fopen( urldecode( $file ), 'r' );
    
30.09.2014 / 15:57
0

Use str_replace of PHP.

str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

First you will do the following: str_replace("ficheiro_xls%2F", "", $variavel);

That way you get those initial characters out of the name.

Then repeat the operation by replacing + with , like this: str_replace("+", " ", $variavel);

Documentation : Str_replace .

Tip : I do not think it's good to use reg. exp. because you will have to do two or three steps to achieve the same result you will have with srt_replace.

    
30.09.2014 / 15:51
0

Dude, try this:

        <?php
        header("Content-Type: application/octet-stream");

        #Tente usar este aqui.
        #Em um index terá o nome do arquivo completo, como veio do GET, mas sem barra ao contrario
        #E no outro, um outro array onde conterá, a(s) pasta(s) do arquivo, e no ultimo index o nome do arquivo
        $arrFile = array(
          str_replace("\","/", $_GET["file"]),
          explode("/",str_replace("\","/", $_GET["file"]))
          );


        header("Content-Disposition: attachment; filename='" . urldecode(end($arrFile[1]))."'";
 #Aqui em cima, você deveria usar o URLDECODE, pois a url já veio com os caracteres especiais, não precisaria "encodar" denovo   
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header("Content-Description: File Transfer");            
        header("Content-Length: " . filesize($arrFile[0])); #Aqui você usa o caminho completo do arquivo
        flush(); // this doesn't really matter.
        $fp = fopen($arrFile[0], "r"); #Aqui você usa o caminho completo do arquivo
        while (!feof($fp))
        {
            echo fread($fp, 65536);
            flush(); // this is essential for large downloads
        } 
        fclose($fp); 

        ?>
    
01.10.2014 / 12:41