0

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.

3
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by astroboy44
0

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 ?>

0

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.

0

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.

Edited by astroboy44: n/a

0

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.

0

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.

Edited by astroboy44: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.