Solved

ftp_fget not working, how to troubleshoot?

Posted on 2012-04-02
3
644 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
3 Comments
 
LVL 10

Accepted Solution

by:
MadShiva earned 500 total points
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
That's the ticket :)

Thank you!

Chris
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
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.  …
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.

771 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

12 Experts available now in Live!

Get 1:1 Help Now