Hi guys,I am trying to force download files from ftp server to my local machine using the following code.

<?php
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\"");
    header("Content-Transfer-Encoding: binary");

    $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, 'ftp://username:password@localhost/'.$path.'/'.$filename);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_exec ($ch);
    curl_close($ch);
    ?>

The above code works perfectly for text files and pdf but when I try to download image or doc or any other files types ,the file gets corrupted i.e the size of the downloaded file and the file on the server is same but when I open them using appropriate application , it says file corrupted.
I even changed the content type to appropriate file types but no luck.I'm testing on my local machine with wamp server and filezilla server.
I'd be glad if someone could help me.
Thank you.

when I try to download image ...the file gets corrupted

Have you tried sending the appropriate MIME type for the file. Assuming you are dealing with a JPEG image, try sending header("Content-type: image/jpeg"); instead of header("Content-type: application/octet-stream"); Also, be sure to add exit(); right before the closing ?>

Have you tried sending the appropriate MIME type for the file. Assuming you are dealing with a JPEG image, try sending header("Content-type: image/jpeg"); instead of header("Content-type: application/octet-stream"); Also, be sure to add exit(); right before the closing ?>

Thanks hielo for the reply.I tried changing content type to appropriate file type and even added exit() but still doesn't work.

Do you need to use cURL for this? Why not use the FTP functions in php?
http://php.net/manual/en/book.ftp.php

You could also use the ftp://wrapper http://docs.php.net/manual/en/wrappers.ftp.php
with the filesystem functions

Hi mschroeder,I've tried wrapper functions but they don't allow me to download files directly to local machine instead the files are first downloaded to web server where php files reside and then from there I need to download them to local machine.But cURL allows me to download directly to local machine.

Try this:

<?php

$handle = fopen('ftp://user:password@ftp.server.com/path/to/image.jpg', 'rb');

header("Content-type: image/jpeg");
echo stream_get_contents($handle);

fclose($handle);

I just ran this code locally and could pull from a remote ftp server without any issue.
Essentially what would change is you would need to specify headers like you did originally and read the stream from the ftp connection to the browser. This should work for any file, just watch for timeouts.

Try this:

<?php

$handle = fopen('ftp://user:password@ftp.server.com/path/to/image.jpg', 'rb');

header("Content-type: image/jpeg");
echo stream_get_contents($handle);

fclose($handle);

I just ran this code locally and could pull from a remote ftp server without any issue.
Essentially what would change is you would need to specify headers like you did originally and read the stream from the ftp connection to the browser. This should work for any file, just watch for timeouts.

Thank you very much mschroeder.The above code works perfectly for me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.