Search code examples
phpfileget

Php script file download


Brief description I'm trying to download png which is kept beside php file in same folder.

Below is my test2.php code

$file_name = $_GET['home.png'];
$file_url = 'http://192.168.0.113:90/download-test/' . $file_name;
header('Content-Type: application/png');
header("Content-disposition: attachment; filename=\"".$file_name."\""); 
readfile($file_url);
exit;

test2.html code

<a href="test2.php?file=home.png">File1</a>

Error : File which is downloaded is test2.php where it should have been home.png.

Please guide what is wrong in the above code.


Solution

  • Your <a href="test2.php?file=home.png">File1</a> is what you need to $_GET in your first line you're using $file_name = $_GET['home.png']; Change that to

    $file_name = $_GET['file'];
    

    and it should do what you want it to do