Solved

ftp_fget not working, how to troubleshoot?

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
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 …

734 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