0

Im using below code to download an .csv file from an ftp. which works if the file is placed in root - but I need to access it on a subfolder. How do I do this?

$source = "myfile.csv";
$target = fopen("myfile.csv", "w");
$conn = ftp_connect("ftp.mysever.com") or die("Could not connect");
ftp_login($conn,"user","pass");
ftp_fget($conn,$target,$source,FTP_ASCII);
echo "file downloaded.\n";

1 Answer 1

1

You can specify full path to folder in which file is.

$source = "path".DIRECTORY_SEPARATOR."to".DIRECTORY_SEPARATOR."subfolder".DIRECTORY_SEPARATOR."myfile.csv";
$target = fopen("myfile.csv", "w");
$conn = ftp_connect("ftp.mysever.com") or die("Could not connect");
ftp_login($conn,"user","pass");
ftp_fget($conn,$target,$source,FTP_ASCII);
echo "file downloaded.\n";
Sign up to request clarification or add additional context in comments.

6 Comments

FYI when using backward-slash you actually escaping t,s and m, same as you would write \t\r\n in double quoted string and converting it to tab, new-line
Hmm - I can´t get that to work. Do i need to use an absolut path or something? Tried $target = fopen("subfolder\myfile.csv", "w");
Edited answer; please check.
Hi @DivyeshPrajapati - cant get it to work - I changed subfolder to the subfolder on root, do I need change the other directory separators as well?
from root; in which folder your file is?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.