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
Solved

ftp_fget not working, how to troubleshoot?

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Query return divisible by 3 3 26
What is wrong with this PDO Delete Query? 2 17
Test if unique ID is in log file 5 22
if (is_singular not working 5 18
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

828 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