Receiving empty file when downloaded

I'm trying to download files form server using PHP by passing the path and file name through URL, like this:

<a class="downloadBtn" style="float:left;" href="download_file.php?folder=<?php echo $codrepresentante.'&file='.$nomeArquivo ?>" >download</a>

Then I receive this PHP file:


$fileName = $_GET['file'];

$path= "http://localhost/portal/boletos/".$_GET["folder"];
$filePath = $path.'/'.$coisa;
echo "caminho: ".$filePath;

    // Define headers

    header("Content-Description: File Transfer");
    header("Content-Transfer-Encoding: binary");
    header("Content-Disposition: attachment; filename=".$coisa);
    header('Content-Type: application/octet-stream');
    header('Cache-Control: must-revalidate');
    header('Content-Length: ' . filesize($coisa));
    // Read the file
    echo readfile($filePath);
} else {
    echo 'The file does not exist.';

But I always get empty files or the The file does not exist response.

I got the $filePath variable and used in the browser to see if the path was wrong, but it worked, so the path is correct.

Could someone help me by indicating where I made a mistake?

1 answer

  • answered 2018-03-13 21:58 symcbean

    Receiving empty file

    Based on the code you've shown us, there shouldn't be any download at all.

    echo "caminho: ".$filePath;

    You're not seeing the errors PHP is reporting to you.


    That's the filename - not its full path.

    but i always get empty files or the 'The file does not exist.'

    So you don't get any download, zero length or other.

    got the $filePath variable and used in the browser to see if the path was wrong

    In the browser you specify a path relative to the document root - but in your PHP code your paths should be relative to the filesystem root.

    You need to start by

    • learning how to describe an issue accurately
    • making sure you are capturing the error and warning messages PHP is telling you about
    • add instrumentation to your code so you can capture the internal state as the execution progresses
    • breakdown the coponent parts of what you are trying to achieve and test them in isolation