Solved

PHP/FTP save to client???

Posted on 2004-08-09
23
532 Views
Last Modified: 2007-12-19
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
Comment
Question by:91mustang
  • 10
  • 8
  • 3
  • +2
23 Comments
 
LVL 3

Expert Comment

by:thecode101
ID: 11753870
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
 
LVL 4

Author Comment

by:91mustang
ID: 11754194
>>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
 
LVL 3

Accepted Solution

by:
thecode101 earned 75 total points
ID: 11754263
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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 4

Author Comment

by:91mustang
ID: 11755128
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
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 11755488
It doesn't display a port because 21 is the standard port for FTP.
0
 
LVL 4

Author Comment

by:91mustang
ID: 11755513
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
 
LVL 4

Author Comment

by:91mustang
ID: 11755665
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
 
LVL 3

Expert Comment

by:thecode101
ID: 11755666
Try linking it this way:
ftp:user:pass@ftp.domain.com/test.doc
0
 
LVL 4

Author Comment

by:91mustang
ID: 11755865
No, unfortunatley that does not work either :)

If someone can solve this I will increase to 500 points
0
 
LVL 10

Assisted Solution

by:ThaSmartUno
ThaSmartUno earned 50 total points
ID: 11755870
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
 
LVL 10

Expert Comment

by:ThaSmartUno
ID: 11755879
    ftp_login($username, $password) &&

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

(but doesn't have to be)
0
 
LVL 10

Expert Comment

by:ThaSmartUno
ID: 11755930
actually i think it does have to be there =)
0
 
LVL 4

Author Comment

by:91mustang
ID: 11756115
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
 
LVL 10

Expert Comment

by:ThaSmartUno
ID: 11756446
using exactly from above ? (I'm assuming you added username, password, hostname, and port variables)
0
 
LVL 4

Author Comment

by:91mustang
ID: 11756455
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
 
LVL 10

Expert Comment

by:ThaSmartUno
ID: 11756471
$folder = 'test\';

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

if you wanted test\
0
 
LVL 4

Author Comment

by:91mustang
ID: 11756496
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
 
LVL 10

Expert Comment

by:ThaSmartUno
ID: 11756512
I was using php 4.3.8 /apache/linux ... I can try 5.0/apache/xp ... but don't have a IIS server
0
 
LVL 10

Expert Comment

by:ThaSmartUno
ID: 11756515
and it does work with 5.0 apache xp
0
 
LVL 4

Author Comment

by:91mustang
ID: 11756521
maybe i will try apache then... This Microslop stuff makes me mad!!
0
 
LVL 10

Expert Comment

by:ThaSmartUno
ID: 11756557
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
 
LVL 4

Expert Comment

by:Kooroo
ID: 11758914
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
 
LVL 4

Author Comment

by:91mustang
ID: 11857385
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question