Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 571
  • Last Modified:

PHP/FTP save to client???

Hello, How can i write a script to login to a ftp server, then download a file using a save as box?

I have the connect and download part working but how can i force it to download to the client machine, and give a save as window?

Here is the code i have so far:

// login with username and password

$login_result = ftp_login($conn_id, "$ftpUser", "$ftpPass");



// check connection

if ((!$conn_id) || (!$login_result)) {

        echo "Ftp connection has failed!";

        echo "Attempted to connect to $ftpServer for user $ftpUser";

        die;

    } else {

        echo "Connected to $ftpServer, for user $ftpUser";

    }
$download = ftp_get ($conn_id,"download.doc", "test.doc", FTP_BINARY);
....
0
91mustang
Asked:
91mustang
  • 10
  • 8
  • 3
  • +2
2 Solutions
 
thecode101Commented:
The ftp_get command will not give you the pop up save window. If a web server is running, what you could do is redirect to the filename and then it would ask you to download the file.
0
 
91mustangAuthor Commented:
>>what you could do is redirect to the filename and then it would ask you to download the file.

How would i do that? Would that involve saving file to web server and then downloading to client as that is not an option( the files to be downloaded are huge!!)
0
 
thecode101Commented:
Without saving it to the webserver, you could just redirect it to the address in the browser for example, ftp://yourhost.com/thefile.doc

You could either make that a link or use
<?php
Header("Location: ftp://yourhost.com/thefile.doc");
?>
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
91mustangAuthor Commented:
Ok, i tried that but it does not work.

I add a link to this: ftp://user:pass@ftp.domain.com:21/test.doc, but

It links me to :

ftp://ftp.domain/test.doc (with no port or user/pass).

Its weird, when i hover over the link it displays ftp://user:pass@ftp.domain.com/test.doc (no port)

When i paste the correct address in the address bar, i get the option to save the file, so i know the address is correct. Any ideas?

0
 
Marcus BointonCommented:
It doesn't display a port because 21 is the standard port for FTP.
0
 
91mustangAuthor Commented:
But Why does it not allow me to download the file from a link, but it will from typing the saem address in the add bar?
0
 
91mustangAuthor Commented:
OK, so apparantly IE has a patch out that prevents linking to a ftp file like described above. Is there any other way to have the user download this file???
0
 
thecode101Commented:
Try linking it this way:
ftp:user:pass@ftp.domain.com/test.doc
0
 
91mustangAuthor Commented:
No, unfortunatley that does not work either :)

If someone can solve this I will increase to 500 points
0
 
ThaSmartUnoCommented:
this is what I do ...
<?php

  $filename = 'test.doc';
  $folder = '/home/test/';

  $fname=tempnam("/tmp","getftp");
  if ( ($ftp_conn = ftp_connect($hostname, $port)) && 
        ftp_login($username, $password) &&
        ftp_chdir($ftp_conn, $folder) &&
        ftp_get($ftp_conn,$fname,basename($filename),FTP_BINARY) &&
        ftp_quit($ftp_conn) ) {
        $user_agent=strtolower($_SERVER["HTTP_USER_AGENT"]);
        header("Cache-Control: no-cache");
        header("Connection: close");
        header("Content-type: application/octet-stream");
        header("Content-Length: ".filesize($fname));
        $attachment = (strstr($user_agent, "msie")) ? "attachment" : "";
        header("Content-Disposition: ".$attachment."; filename=\"".basename($sel_file)."\"");
        readfile($fname);
        unlink($fname);
        exit;
   } else {
        echo "Unable to connect to ftp.";
   }
?>
0
 
ThaSmartUnoCommented:
    ftp_login($username, $password) &&

should be
     ftp_login($ftp_conn, $username, $password) &&

(but doesn't have to be)
0
 
ThaSmartUnoCommented:
actually i think it does have to be there =)
0
 
91mustangAuthor Commented:
Hey  ThaSmartUno,

I tried that but it gives me an error:

"Internet Explorer Cannot download test.php from 127.0.0.1

Internet Explorer was not able to open this Internet site. The requested Site is either unavailable or cannot be found. Please try again later.

0
 
ThaSmartUnoCommented:
using exactly from above ? (I'm assuming you added username, password, hostname, and port variables)
0
 
91mustangAuthor Commented:
This is what i used:

<?php
  $filename = 'mike.doc';
  $folder = 'test\';
      $hostname= 'ftp.priszmgraphics.com';
      $port = '21';
      $username='xxx';
       $password='xxx';
  $fname=tempnam("/tmp","getftp");
  if ( ($ftp_conn = ftp_connect($hostname, $port)) && 
        ftp_login($ftp_conn,$username, $password) &&
       // ftp_chdir($ftp_conn, $folder) &&
        ftp_get($ftp_conn,$fname,basename($filename),FTP_BINARY) &&
        ftp_quit($ftp_conn) ) {
        $user_agent=strtolower($_SERVER["HTTP_USER_AGENT"]);
        header("Cache-Control: no-cache");
        header("Connection: close");
        header("Content-type: application/octet-stream");
        header("Content-Length: ".filesize($fname));
        $attachment = (strstr($user_agent, "msie")) ? "attachment" : "";
        header("Content-Disposition: ".$attachment."; filename=\"".basename($sel_file)."\"");
        readfile($fname);
        unlink($fname);
        exit;
   } else {
        echo "Unable to connect to ftp.";
   }
?>
0
 
ThaSmartUnoCommented:
$folder = 'test\';

should have a second \
$folder = 'test\\';

if you wanted test\
0
 
91mustangAuthor Commented:
Yeah I tried that as well. What do you run this code on? I am using php 5.0/ IIS 5.1/ win XP
0
 
ThaSmartUnoCommented:
I was using php 4.3.8 /apache/linux ... I can try 5.0/apache/xp ... but don't have a IIS server
0
 
ThaSmartUnoCommented:
and it does work with 5.0 apache xp
0
 
91mustangAuthor Commented:
maybe i will try apache then... This Microslop stuff makes me mad!!
0
 
ThaSmartUnoCommented:
you could try taking it all out of the if statement ... so its just

<?php

 $filename = 'mike.doc';
  $folder = 'test\';
     $hostname= 'ftp.priszmgraphics.com';
     $port = '21';
     $username='xxx';
      $password='xxx';
  $fname=tempnam("/tmp","getftp");
  $ftp_conn = ftp_connect($hostname, $port);
  ftp_login($ftp_conn,$username, $password);
  ftp_get($ftp_conn,$fname,basename($filename),FTP_BINARY);
  ftp_quit($ftp_conn);
        $user_agent=strtolower($_SERVER["HTTP_USER_AGENT"]);
        header("Cache-Control: no-cache");
        header("Connection: close");
        header("Content-type: application/octet-stream");
        header("Content-Length: ".filesize($fname));
        $attachment = (strstr($user_agent, "msie")) ? "attachment" : "";
        header("Content-Disposition: ".$attachment."; filename=\"".basename($sel_file)."\"");
        readfile($fname);
        unlink($fname);
?>
0
 
KoorooCommented:
hey folks

<?php

$file = "ftp://username:pass@URL/filename.ext";
header("Content-Description: File Transfer");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".basename($file));
@readfile($file);

?>

If this doesn't work, you might have to fiddle with bufferinng using freads, but it seems to me like there shouldn't be any issues.

 I strongly advise testing on a non-production box since you said the files may be prohibitively large and you may run into memory usage issues or file corruption at some point. I tested it with a 20MB tgz file. Also, bear in mind, this code snippet essentially streams the file from the ftp to the user via the web server, so there will likely be bandwidth issues. I don't think you can actually do this without passing through the web service though anyways. Just an FYI.

ThaSmartUno's solution is also very good since it utilizes the local disk store and you would be able to write caching mechanisms into it fairly easily, but according to the response you gave thecode101, this seemed like it wasn't an option.
0
 
91mustangAuthor Commented:
Sorry for the delay in closing this.

It turns out it was a problem with the ftp server itself. I tried using a different server and both solutions work like a charm. Baffles me why one server will not take links, but you can paste the url in the address bar and it works. any who thats another battle, for another day

thanks to all!!!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 8
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now