[solved] problem with ftp_get finding file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

[solved] problem with ftp_get finding file

Post by Bill H »

I need to have my client creats a comma-delimited file from the database and download it. I have the process completed to the point of the file being created on the server, now I need to get it to my client and that needs to be simple to the point of "click here." Using the php ftp functionality seems the best way, but I can't seem to connect with the file on the server. The following tells me "successfully connected" and "Successfully logged in" and then and error that the file to be downloaded could not be found.

Code: Select all

<?php
$fil = @fopen("../support/datadump.csv","w");
if ($fil == FALSE) echo "Failed to open file...";
else
{    // stuff here to write to the file
     fclose($fil);
 
     $local_file = "datadump.csv";
     $server_file = "../support/datadump.csv";
     $ftp_server = "ftp.servername.com";
     $ftp_user_name = "username";
     $ftp_user_pass = "password";
 
     $conn_id = ftp_connect($ftp_server);
     if ($conn_id == FALSE) echo "ftp Connect error\n\n";
     else
     {
          echo "Successfully connected\n";
          $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
          if ($login_result == FALSE) echo "ftp login error\n\n";
          else
          {
               echo "Successfully logged in\n";
               if (ftp_get($conn_id, $local_file, $server_file, FTP_ASCII))
                    echo "Successfully written to $local_file\n";
               else echo "There was a problem writing\n";
          }
          ftp_close($conn_id);
     }
}
?>
 
The file is there, and I've tried about 20 different pathing methods to get to it, none of which work. What am I missing, or is there some simpler way to get a file from the server to the client? (Forget setting them up with an ftp client account. They are nowhere near sophisticated enough for that.)
Last edited by Bill H on Sun Feb 03, 2008 1:43 pm, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: problem with ftp_get finding file

Post by Christopher »

You define $server_file = "../support/datadump.csv";. Can you login via a FTP client and do "cd .."? Typically FTP servers will chroot you so you cannot do that.
(#10850)
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: problem with ftp_get finding file

Post by Bill H »

Excellent suggestion. I may try that just to see if it will work, however...

The reason I didn't want to use headers is that I assumed the process would take enough time that some sort of screen display would be needed, like "Working...." or something and one can't send headers after screen output. Creating the csv actually happens before the user can get his finger off of the key, so... As soon as they click on the menu selection the download dialog pops up, they save it and they're done.

Thanks for the good suggestion though.
Post Reply