?
Solved

ftp copy file to localhost  (do not know file name)

Posted on 2012-09-20
12
Medium Priority
?
714 Views
Last Modified: 2012-10-17
I have one folder
and one file to download


<?php
		error_reporting(E_ALL);

       
        $folder_path = "YOUR FOLDER PATH";
        $local_file = "LOCAL FILE PATH";
        $server_file = "SERVER FILE PATH";

        $ftp_server = "IP ADDRESS"; // Address of FTP server.
        $ftp_user_name = "USERNAME"; // Username
        $ftp_user_pass = "PASSWORD"; // Password
              
        $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 $server_file and save to $local_file
        if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) 
		{
            echo "Successfully written to $local_file\n";
        } 
		else 
		{
            echo "There was a problem in the ftp connection \n";
        }
        
        ftp_close($conn_id);
?>
 

Open in new window



this works


but I do not know the file name
because file name contains the date
Master File 20_09_2012.xlsx
0
Comment
Question by:rgb192
12 Comments
 
LVL 11

Accepted Solution

by:
Slimshaneey earned 668 total points
ID: 38420708
You could use the following code to get all files in your directory:

$contents = ftp_nlist($conn_id, "."); 
    foreach ($contents as $file) { 
    
        if ($file == '.' || $file == '..') 
            continue; 
        
        if (@ftp_chdir($conn_id, $file)) { 
            ftp_chdir ($conn_id, ".."); 
            ftp_sync ($file); 
        } 
        else 
            ftp_get($conn_id, $file, $file, FTP_BINARY); 
    } 

Open in new window

0
 
LVL 1

Author Comment

by:rgb192
ID: 38420749
I dont understand


where do I place your code


do I place after ftp connect
0
 
LVL 11

Expert Comment

by:Slimshaneey
ID: 38420757
Right after the ftp login. What my code does is get a listing of all files in the folder you connected to. It then downloads each file.
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!

 
LVL 36

Assisted Solution

by:Loganathan Natarajan
Loganathan Natarajan earned 668 total points
ID: 38420790
just check this example also, http://www.laughing-buddha.net/php/dirlist

you can loop through and get files to download.
0
 
LVL 16

Assisted Solution

by:AlexPace
AlexPace earned 664 total points
ID: 38422083
Does it have to be php?  If not, here is a Robo-FTP sample script that downloads only files with yesterday's date in the filename: http://kb.robo-ftp.com/script_library/show/45

... If it has to be php, maybe you can translate the Robo-FTP logic into php syntax.
0
 
LVL 1

Author Comment

by:rgb192
ID: 38422793
I can only use php




if there is only one file in the directory, what variable is that one filename

http://www.laughing-buddha.net/php/dirlist
<?php

  function getDirectoryList ($directory) 
  {

    // create an array to hold directory list
    $results = array();

    // create a handler for the directory
    $handler = opendir($directory);

    // open directory and walk through the filenames
    while ($file = readdir($handler)) {

      // if file isn't this directory or its parent, add it to the results
      if ($file != "." && $file != "..") {
        $results[] = $file;
      }

    }

    // tidy up: close the handler
    closedir($handler);

    // done!
    return $results;

  }

?>

Open in new window

0
 
LVL 11

Expert Comment

by:Slimshaneey
ID: 38422834
If there is only one then the file name will be $results[0]
0
 
LVL 1

Author Comment

by:rgb192
ID: 38424193
do I combine these 3 php

if so how

<?php
		error_reporting(E_ALL);

       
        $folder_path = "YOUR FOLDER PATH";
        $local_file = "LOCAL FILE PATH";
        $server_file = "SERVER FILE PATH";

        $ftp_server = "IP ADDRESS"; // Address of FTP server.
        $ftp_user_name = "USERNAME"; // Username
        $ftp_user_pass = "PASSWORD"; // Password
              
        $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 $server_file and save to $local_file
        if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) 
		{
            echo "Successfully written to $local_file\n";
        } 
		else 
		{
            echo "There was a problem in the ftp connection \n";
        }
        
        ftp_close($conn_id);
?>
 

Open in new window


<?php

  function getDirectoryList ($directory) 
  {

    // create an array to hold directory list
    $results = array();

    // create a handler for the directory
    $handler = opendir($directory);

    // open directory and walk through the filenames
    while ($file = readdir($handler)) {

      // if file isn't this directory or its parent, add it to the results
      if ($file != "." && $file != "..") {
        $results[] = $file;
      }

    }

    // tidy up: close the handler
    closedir($handler);

    // done!
    return $results;

  }

?>

Open in new window



$results[0]
0
 
LVL 1

Author Comment

by:rgb192
ID: 38426604
or what should I do
0
 
LVL 11

Expert Comment

by:Slimshaneey
ID: 38426628
OK, your second script is only for local directories, not for ftp directories.

Its not really clear what you are wanting to do. Do you want to download the file or just get the filename? My example from earlier will do both.
0
 
LVL 1

Author Comment

by:rgb192
ID: 38431648
>>
Its not really clear what you are wanting to do. Do you want to download the file or just get the filename? My example from earlier will do both.

download the file

I do not understand where to put your code (downloading the file) after the code to connect to the ftp
0
 
LVL 1

Author Closing Comment

by:rgb192
ID: 38507988
thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.
Suggested Courses

616 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