Save file name with empty spaces html/php

Asked

Viewed 615 times

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 file download_file.php 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); 

?>

Everything works correctly, the file is downloaded. But the goal is the file to be stored under the following name "BD Fugas Gespost.xls" and is being guarded as "ficheiro_xls%2FBD+Fugas+Gespost.xls". I’ve tried how it’s up to use the %20 to replace space, but it doesn’t work

  • I think your problem is in the line that you speak: file=ficheiro_xls/BD%20Fugas%20Gespost.xls and then read: $file = $_GET["file"];. The problem is not in PHP, but in the instruction you give it. See if it is necessary to include this ficheiro_xml/.

  • @Freddutra, file not required/

3 answers

1

First, decide which is the Content-Type desired, you have 3 headers with Content-Type, and must have only one.

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

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

however, as it has folder name in the $file, maybe it’s a case of just taking the file name:

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


Alternatively:

Eventually you could do different, without the Encounter on the link:

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

keeping the urlencode in header:

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

and perhaps altering the opening of $file, but that depends on how the filesystem’s real name is:

$fp = fopen( urldecode( $file ), 'r' );
  • so the file is not saved with extension

  • @pc_oc one thing that may be getting in the way is the bar

  • I edited, try the solution with basename().

  • @Bacco, I think the problem is that it passes file=ficheiro_xls/BD%20Fugas%20Gespost.xls and then read: $file = $_GET["file"];. The problem is not in PHP, but in the instruction it passes to PHP.

  • @Freddutra is able. More or less what I suggested in the 2nd option, do without previous encoding.

  • @Bacco, with basename he tries to save only the file with the name "BD", IE, reaches the space and did not find the rest of the file name

  • @pc_oc you did not mix the 2 solutions right? Beware that in the answer I suggested or take the Encounter, or change the URL. Don’t do both at the same time. If so, I edit the answer to better separate the ideas. That OS vc is using?

  • @Bacco I am using Windows8.1. Using the alternative, he is replacing the " by "+".

    • and %20 are the same thing in Urlencode, that’s normal. If you got there all right. Did you have trouble opening the file? I added the urldecode in $file. If you can, test with basename( urldecode( $file ) ) in the header too.
  • has also rawurlencode and rawurldecode which are similar to urlencode and urldecode, but treat "+" differently.

Show 5 more comments

0

Use the str_replace of PHP.

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

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

That way you take these initials from the name.

Then repeat the operation by replacing the + for , thus: str_replace("+", " ", $variavel);

Documentation: Str_replace.

Tip: I don’t think it’s a good thing to use reg. Exp. because you’ll have to do two or three steps to achieve the same result you’ll get with srt_replace.

  • I tried that already and the file is saved with the same name. I think the problem may be in some configuration of download_file.php

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); 

        ?>
  • Thanks for the help! With this code the download does not start, IE, I am redirected to the download_file.php page and the download does not start

Browser other questions tagged

You are not signed in. Login or sign up in order to post.