Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php content-disposition folder not working

I'm using header('Content-Disposition:...") to download files from a website. To set the path I use the next code:

$myPath = realpath('../wp-content/uploads/');

I know the path is OK by printing it in the console. So I use the following lines to download it:

$full_path=$myPath. '/myfile.csv';
header('Content-Disposition: attachment; filename=' . $full_path);
header("Pragma: no-cache");
header("Expires: 0");

Finally, it downloads the file, but the problem is the next one. Instead of downloading it in the right folder, it does it in the "downloads" folder of my computer, and the file name is the path of the folder but changing the '/' characters by '-'. For example: "-Applications-XAMPP-htdocs-myproject-wp-content-uploads-myfile.csv"

Any solution? Thank you so much

like image 421
borjacastillo Avatar asked Aug 06 '26 13:08

borjacastillo


1 Answers

you cannot choose where you user will download the file. This is a browser settings that you cannot change. If you think about it if a website is allowed to save files where it wants it could be a very security thread because it can ovverride important system files.

I'm afraid that you cannot do much about it. Just put an alert or a message telling the user to save the file in the location that you want if the location is crucial for your project

like image 82
wezzy Avatar answered Aug 09 '26 03:08

wezzy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!