Viewed   58 times

Whats the best what to enable a user to click a download button that then automatically gets a image file and then downloads it to their generic downloads folder or prompts them to save it?

i have tried the following but with no success:

  $file = $art[0]['location']; // this is the full url to image http://....image.jpg
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_POST, 0); 
  curl_setopt($ch,CURLOPT_URL, $file); 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  $file_content = curl_exec($ch);

ect or am i going about this all wrong (probably)




Before you echo the data:

header('Content-Disposition: attachment; filename=save_as_name.jpg');


echo file_get_contents("http://...");
Thursday, August 11, 2022

You could try something like this:

$file_name = 'file.avi';
$file_url = '' . $file_name;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename="".$file_name."""); 

I just tested it and it works for me.

Please note that for readfile to be able to read a remote url, you need to have your fopen_wrappers enabled.

Wednesday, September 14, 2022

First you must make sure your application has permission to write to the sdcard. To do this you need to add the uses permission write external storage in your applications manifest file. See Setting Android Permissions

Then you can you can download the URL to a file on the sdcard. A simple way is:

URL url = new URL ("file://some/path/anImage.png");
InputStream input = url.openStream();
try {
    //The sdcard directory e.g. '/sdcard' can be used directly, or 
    //more safely abstracted with getExternalStorageDirectory()
    File storagePath = Environment.getExternalStorageDirectory();
    OutputStream output = new FileOutputStream (new File(storagePath,"myImage.png"));
    try {
        byte[] buffer = new byte[aReasonableSize];
        int bytesRead = 0;
        while ((bytesRead =, 0, buffer.length)) >= 0) {
            output.write(buffer, 0, bytesRead);
    } finally {
} finally {

EDIT : Put permission in manifest

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Friday, August 12, 2022

You should be able to do it like this:

using (WebClient Client = new WebClient ())
    Client.DownloadFile("", "12345.jpg");
Sunday, November 27, 2022

EF BB BF is the UTF-8 encoding Byte Order Mark (BOM). I suspect there is some configuration option to turn off the BOM.

Edit: File editors should allow you to turn off the BOM when saving an file in relevant character encodings (e.g. UTF-8).

Thursday, September 8, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :