Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 474
  • Last Modified:

save .csv file to a particular directory

Hello,

the code below is fetching a csv file for me from an FTP account. it works fine except that it totally ignores where i want the csv file to be saved.

where is says 'YOUR FOLDER PATH' i've been putting in here C:inetpub\wwwroot\mydomain\temp

but instead of putting it in the /temp dir it just dropps the csv file in the same dir as from where the script is run from which is a completly different dir.

any ideas?

thanks

zac


<?php
                // define some variables
        $folder_path = "YOUR FOLDER PATH"; 
        $local_file = "the_file.csv";
        $server_file = "the_file.csv";
        
        //-- Connection Settings
        $ftp_server = "IP ADDRESS"; // Address of FTP server.
        $ftp_user_name = "USERNAME"; // Username
        $ftp_user_pass = "PASSWORD"; // Password
        #$destination_file = "FILEPATH"; 
        
        // 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 $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\n";
        }
        
        // close the connection
        ftp_close($conn_id);
?>

Open in new window

0
bede123
Asked:
bede123
  • 3
  • 2
1 Solution
 
leakim971PluritechnicianCommented:
Hello bede123,

http://www.php.net/manual/en/function.ftp-get.php

line 20 :
if (ftp_get($conn_id, $folder_path . $local_file, $server_file, FTP_BINARY))

with $folder_path = "/path/to/folder/";

Regards.

0
 
bede123Author Commented:
thanks so is this the web path or the actual server path like:

C:\inetpub\wwwroot\etc etc etc

sorry, this is my first ever attempt at PHP

<?php
                // define some variables
        $folder_path = "C:\Inetpub\Www_root\mydomain\temp"; 
        $local_file = "file.csv";
        $server_file = "file.csv";
        
        //-- Connection Settings
        $ftp_server = "ftp.server.co.uk"; // Address of FTP server.
        $ftp_user_name = "user"; // Username
        $ftp_user_pass = "pass"; // Password
        #$destination_file = "FILEPATH"; 
        
        // 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 $server_file and save to $local_file
        if (ftp_get($conn_id, $folder_path . $local_file, $server_file, FTP_BINARY))
		with $folder_path = "C:\Inetpub\Wwwroot\mydomain\temp";  {
            echo "Successfully written to $local_file\n";
        } else {
            echo "There was a problem\n";
        }
        
        // close the connection
        ftp_close($conn_id);
?> 

Open in new window

0
 
leakim971PluritechnicianCommented:
Try :

<?php
                // define some variables
        $folder_path = "C:/Inetpub/Www_root/mydomain/temp/"; 
        $local_file = "file.csv";
        $server_file = "file.csv";
        
        //-- Connection Settings
        $ftp_server = "ftp.server.co.uk"; // Address of FTP server.
        $ftp_user_name = "user"; // Username
        $ftp_user_pass = "pass"; // Password
        #$destination_file = "FILEPATH"; 
        
        // 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 $server_file and save to $local_file
        if (ftp_get($conn_id, $folder_path . $local_file, $server_file, FTP_BINARY)) {
            echo "Successfully written to $local_file\n";
        } else {
            echo "There was a problem\n";
        }
        
        // close the connection
        ftp_close($conn_id);
?>

Open in new window

0
 
bede123Author Commented:
well done that man! perfect, thank you.

zac
0
 
leakim971PluritechnicianCommented:
You're welcome! Thanks for the points and Merry Christmas!
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.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now