Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ftp_fget not working, how to troubleshoot?

Posted on 2012-04-02
3
Medium Priority
?
686 Views
Last Modified: 2012-04-03
I need to grab a file from a remote server via ftp and save it on my own server.

I am trying to use the script below - it's taken pretty much from the sample on php.net - http://php.net/manual/en/function.ftp-fget.php

When I run it I get the 'there was a problem while downloading...' message after about 90 seconds.

How can troubleshoot this more and figure out what the problem is?

I have connected through an ftp program on my desktop computer, so I know the username/password/server-url are correct - and the connection and download is quick, just a few seconds.

I have also checked and the file name on the remote server is correct. And the local file is set with the permissions 777.

It will give me an empty file. If I have some text in it, it will clear that out and just leave an empty file. But isn't providing the text in the remote file and is delivering that error message.


//log in details
$ftp_user_name = 'cews';
$ftp_server = 'ftp.thesite.com';
$ftp_user_pass = 'oboikdf3';

// path to remote file
$remote_file = 'OBS.TXT';
$local_file = 'obs.txt';


// open some file to write to
$handle = fopen($local_file, 'w');

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// try to download $remote_file and save it to $handle
if (ftp_fget($conn_id, $handle, $remote_file, FTP_ASCII, 0)) {
 echo "successfully written to $local_file\n";
} else {
 echo "There was a problem while downloading $remote_file to $local_file\n";
}

// close the connection and the file handler
ftp_close($conn_id);


fclose($handle);
0
Comment
Question by:St_Aug_Beach_Bum
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 10

Accepted Solution

by:
Tobias earned 2000 total points
ID: 37799626
Dear,

Try with passive mode on:

//log in details
$ftp_user_name = 'cews';
$ftp_server = 'ftp.thesite.com';
$ftp_user_pass = 'oboikdf3';

// path to remote file
$remote_file = 'OBS.TXT';
$local_file = 'obs.txt';


// open some file to write to
$handle = fopen($local_file, 'w');

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

ftp_pasv($conn_id, true);

// try to download $remote_file and save it to $handle
if (ftp_fget($conn_id, $handle, $remote_file, FTP_ASCII, 0)) {
 echo "successfully written to $local_file\n";
} else {
 echo "There was a problem while downloading $remote_file to $local_file\n";
}

// close the connection and the file handler
ftp_close($conn_id);


fclose($handle);

Open in new window



Regards
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 37799648
Can you login and download that file with a regular FTP program like Filezilla or Microsoft command line FTP?
0
 

Author Closing Comment

by:St_Aug_Beach_Bum
ID: 37800852
That's the ticket :)

Thank you!

Chris
0

Featured Post

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!

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…

688 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