Solved

ftp_fget not working, how to troubleshoot?

Posted on 2012-04-02
3
650 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
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 83

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

863 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

20 Experts available now in Live!

Get 1:1 Help Now