I have a website where user can click on the download button and the file in my drive will get downloaded in the user device. I made a code and it works file when I am doing it on laptop, then for testing I used KSWEB (for localhost server on my android). when I download the file in android it gets downloaded but in htdocs folder which is a folder of the web server KSWEB just same as the htdocs of XAMPP in computer. Now I am worried what will happen when user will download the file, where will it get stored? or will it give any other error like path does not exist? How can I download the file in all of the devices.
this the code which I have tried and which works in laptop
$dir = getenv("HOMEDRIVE") . getenv("HOMEPATH").'\Downloads\\';
$filelink="https://drive.google.com/uc?id=1Tvoj7JFUaL3RKWOHiLjsJZHUIEgVqriu&export=media";
$file=file_get_contents($filelink);
$s=file_put_contents($dir."file3.doc",$file);
My Answer:
To ensure that the file is downloaded to the user's device regardless of the device they are using, you can modify the code to prompt the user to download the file instead of saving it to a specific directory on the server.
You can achieve this by using the following code:
php
$filelink = "https://drive.google.com/uc?id=1Tvoj7JFUaL3RKWOHiLjsJZHUIEgVqriu&export=download";
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"file3.doc\"");
readfile($filelink);
exit;
This code will force the browser to download the file instead of displaying it in the browser. The user will be prompted to save the file to their device, and they can choose the location where they want to save it.
This way, the file will be downloaded to the user's device regardless of the device they are using, and they can choose where to save it.
Rate this post
5 of 5 based on 4764 votesComments