• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2114
  • Last Modified:

Unable to download using FTP

I am trying to download a file from a website to my local computer using FTP.   ( Ultimately I would like to download several files but can't past this basic step. )

The web server is running UNIX.  I am able to log in to the FTP server successfully with code not included here using a special FTP account I have created for this purpose.  Currently this account has permission '0755' set for the server directories used in the code.

When I use ftp_get or ftp_fget the following warnings are returned and no file transfer occurs:

  Warning: ftp_fget() [function.ftp-fget]: bind() failed: Permission denied (13) in <php file line ..>
  Warning: ftp_fget() [function.ftp-fget]: Type set to I in <php file line ..>

In the code included below $ftp seems to be a valid connection handle since login using it succeeds.  I have tried this both with and without having created the local directory for the downloaded file ( ie. c:\pg09\ ) and using two variants of the remote server path to the file I want to download.   These are
  "./grads/Images/46/46_1.jpg" ( this is the full path to the file on the server )
     and
  "./Images/46/46_1.jpg" ( this is the path that the logged on user is restricted to on the server )

The local machine is a PC running WinXP Pro SP3.  I have set NTFS permissions on the local folder "c:\pg09" to Modify for Everyone.  This did not change the result.

Please help me understand how to resolve this.

Thanks for your help.
ftp_get( $ftp, "c:\pg09\46_1.jpg", ".grads/images/46/46_1.jpg", FTP_BINARY );

Open in new window

0
crm-serv
Asked:
crm-serv
  • 9
  • 8
1 Solution
 
mostartCommented:
Before using PHP functions you should try to make sure you can download the file "by hand" without PHP but with any ftp client. Using exactly the same user credentials as you would using php.

maybe the path is still wrong or you are missing some permissions on the directories.
0
 
Ionut A. TudorPHP ProgrammerCommented:
Hi,
you need to move through directories of the remote server, read here: http://www.php.net/manual/en/function.ftp-chdir.php
Hope it solves your problem.
0
 
crm-servAuthor Commented:
FTP download using and FTP client works fine.

I set the directory with this code, checked the current directory and then tried ftp_get() with the attached code
   ftp_chdir( $ftp, "./grads/images/46");
   echo "<br>current directory is ".ftp_pwd( $ftp );
   ftp_get( $ftp, "c:\pg09\46_1.jpg", "./46_1.jpg", FTP_BINARY );

Result returned was
  current directory is /grads/images/46
  and the same warnings as in the original post




 
   ftp_chdir( $ftp, "./grads/images/46"); 
   echo "<br>current directory is ".ftp_pwd( $ftp );
   ftp_get( $ftp, "c:\pg09\46_1.jpg", "./46_1.jpg", FTP_BINARY ); 

Open in new window

0
Industry Leaders: 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!

 
Ionut A. TudorPHP ProgrammerCommented:
don't include full path in the destination, see below

ftp_chdir( $ftp, "./grads/images/46/");
echo "<br>current directory is ".ftp_pwd( $ftp );
ftp_get( $ftp, "46_1.jpg", "46_1.jpg", FTP_BINARY );
 
// now move the file
copy("46_1.jpg", "C:\pg09\46_1.jpg");

Open in new window

0
 
crm-servAuthor Commented:
With the proposed code I get the same result as before plus the following warning:

  Warning: copy(46_1.jpg) [function.copy]: failed to open stream: No such file or directory in ...
0
 
Ionut A. TudorPHP ProgrammerCommented:
You need to match the pathnames. As we don't have access to your server we can't know what path the script needs.
ftp_chdir( $ftp, "/grads/images/46/");
are you sure this is the abosulte path ? if you enter on your shell and write cd /grads/images/46/ you arrive at your pictures ?
Again you mus write the correct path of the folder you need throu it.
Good luck

0
 
crm-servAuthor Commented:
"/grads/images/46/" is in the root directory.  Also the script in in the /grads/ directory.
0
 
Ionut A. TudorPHP ProgrammerCommented:
"/grads/images/46/" is in the root directory
You mean the web server root ? if you access http://example.com/grads/images/46/ and your script is in http://example.com/grads/script.php ?
please answer above and also try this to find out the path:
<?php

// 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);

// check connection
if ((!$conn_id) || (!$login_result)) {
    die("FTP connection has failed !");
}

echo "Current directory: " . ftp_pwd($conn_id) . "\n";

// close the connection
ftp_close($conn_id);
?>
 
What does the above output ? As i said your problem is with the pathnames and you need to move throu directories like this:
ftp_chdir($conn_id, "dir1");
ftp_chdir($conn_id, "dir2");
ftp_chdir($conn_id, "dir3");
Hope its clear now.
0
 
crm-servAuthor Commented:
Your guidance is clear but I am getting the same result.  I have posted the code I ran based on your suggestion in your  last post.   And here is the output in the browser.  It shows the movement through the directories.

Current directory: /grads
Current directory: /grads/images
Current directory: /grads/images/46
( followed by the same warning as before )

Perhaps I am still not providing the arguments for ftp_get and copy correctly.

Thanks for  your patience and for staying with me on this.  
// check connection
if ((!ftp) || (!$login_result)) {
   die("FTP connection has failed !");
}
 
ftp_chdir($ftp, "grads");
echo "Current directory: " . ftp_pwd($ftp) . "<br>";
ftp_chdir($ftp, "images");
echo "Current directory: " . ftp_pwd($ftp) . "<br>";
ftp_chdir($ftp, "46");
echo "Current directory: " . ftp_pwd($ftp) . "<br>";
 
//ftp_get( $ftp,"46_1.jpg","c:\pg09\46\46_1.jpg",FTP_BINARY ); //gets the warnings
 
ftp_get( $ftp, "46_1.jpg", "46_1.jpg", FTP_BINARY );	//gets the warnings
 
copy ( "46_1.jpg", "c:\pg09\46\46_1.jpg" );	//gets warning reported previously
 
// close the connection
ftp_close($ftp);

Open in new window

0
 
Ionut A. TudorPHP ProgrammerCommented:
1. Just to be sure, when you manually login to the ftp, you arrive in the root and you enter first directory: grads, then you go to images and 46, after that can you view in your ftp program the picture named 46_1.jpg ?

2. Do you have permision of read in folder 46 ? if not chmod it to full permisions 777.

3. In your local machine where the php script is located do you have permision to write ? Try the following: file_put_contents("test.txt", "just a test");

4. After checking the above, please put error_reporting(E_ALL); and show the exact errors/warnings you are receiving

and try putting the picture in your php script's folder for now:
ftp_get( $ftp, "46_1.jpg", "46_1.jpg", FTP_BINARY);

also try this
$files = ftp_rawlist($conn_id, '/grads/images/46');
print_r($files);
and see if the picture 46_1.jpg is there

Waiting for you with the results of the above tests, Cheers
0
 
crm-servAuthor Commented:
1. Just to be sure, when you manually login to the ftp, you arrive in the root and you enter first directory: grads, then you go to images and 46, after that can you view in your ftp program the picture named 46_1.jpg ?

  That is correct.
2. Do you have permision of read in folder 46 ? if not chmod it to full permisions 777.
    Permissions for each folder involved is currently set to 755.  
3. In your local machine where the php script is located do you have permision to write ? Try the following: file_put_contents("test.txt", "just a test");
      I have set NTFS permissions on the folder \pg09\  to Modify for Everyone.
Oops!  I have the script on the server in the \grads\ directory.  Your point 3 indicates you understand the script to be located on the local machine.  
Is that the problem here?  
I noticed just now that I have created a file in the \grads\ directory with file name "c:\pg09&_1.jpg".  I could not open the file in the browser using that name but after changing the name to "1.jpg" I am able to load it in the browser and it is the image file "grads\images\46_1.jpg".
When I ran file_put_contents( ) it created a file in the \grads\ directory.
 
0
 
crm-servAuthor Commented:
I am not receiving the warnings now.  This is after turning on PASSIVE mode by running
ftp_pasv( $ftp, true );
0
 
Ionut A. TudorPHP ProgrammerCommented:
if you're having the php script already up on the server with the pictures then how would you transfer it to your local machine ? And why would you connect with ftp client when you have the php file already there and you can simply use file_get_contents("images/46/picture.jpg");
It just doesn't make sense, how would you transfer that file from the web server if the php script is located on the same web server and why connect through ftp on the same server ? Can you explain further your thoughts. Cheers
0
 
crm-servAuthor Commented:
Is PHP the best way to do what I am trying to do?  Is there a better approach ?
0
 
Ionut A. TudorPHP ProgrammerCommented:
Well i don't know if its the best way, but its a good way.
But again what are you trying to achieve ? Your initial question sound like you have php installed on your local machine, and you have a script that connects to an ftp server and trying to take a picture. But you told me now that you have the php file already on the server, so its a bit unclear what you're trying to achieve. If you have the php file on the server then why to connect to the same server's ftp to get a file and where in this equation is your local machine ? Its a bit unclear at this moment, can you clarify ?
0
 
crm-servAuthor Commented:
Various contributors upload image files to a web server.   These files are to be used for assembling a presentation that will be developed on a local computer.

I need to get the image files from web server to the local computer.

My intent has been to determine if I can download the image files by simply using the browser on the local computer ( to avoid having to install PHP on the local computer ).  In trying to use PHP FTP functions I clearly mis-understood the technology.

Your suggestions would be appreciated.  Also let me especially thank you for staying with me on this in order to uncover the basic problem in my approach.
0
 
Ionut A. TudorPHP ProgrammerCommented:
If you have the images uploaded on your remote server, then you can use a ftp client to connect and get the images. I recommend you Filezilla which is free and has good suport: http://filezilla-project.org/, but if you find it not meeting your needs you can always choose a paid ftp client with more options something like Ipswitch FTP: http://www.ipswitchft.com/products/ws_ftp_professional/
Hope this helps
0
 
crm-servAuthor Commented:
Thanks for the help.  I understand the basics of using PHP for FTP now.  

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!

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now