Solved

PHP/FTP save to client???

Posted on 2004-08-09
23
512 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
 
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:Squinky
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now