PHP FTP download failed with "failed to open stream: Error downloading" - php

I am trying to download from FTP server (using FileZilla server) to my local computer, but I keep getting error
This is my code
<?php
// connect and login to FTP server
$ftp_server = "127.0.0.1";
$ftp_username = "myusername";
$ftp_userpass = "mypassword";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
echo "success connect to FTP";
$local_file = "C:\Users\PROSIA\Desktop\test.docx";
$server_file = "C:\Users\PROSIA\Documents\dummyfile.docx";
// download server file
if (ftp_get($ftp_conn, $local_file, $server_file, FTP_ASCII))
{
echo "Successfully written to $local_file.";
}
else
{
echo "Error downloading $server_file.";
}
ftp_close($ftp_conn);
This is the error I get:
Warning: ftp_get(C:\Users\PROSIA\Desktop est.docx): failed to open stream:
Error downloading C:\Users\PROSIA\Documents\dummyfile.docx.
My logic for the code is $local_file is the path to save the downloaded file to my local computer and $server_file is the path from FTP server to download the file
So I am a bit confused with the first warning, "failed to open stream" while the file is not exist yet and its seem got blank space (it should be \Desktop\test.docx rather than Desktop est.docx)
And additional question, can I just read, without download?

You cannot use absolute paths to remote files in FTP protocol.
The FileZilla FTP servers has mapping in its configuration that projects the remote file system into virtual FTP file tree. You have to use paths to the virtual file tree.
E.g. The C:\Users\PROSIA can be mapped to something like /users/PROSIA. In that case you use:
$server_file = "/users/PROSIA/dummyfile.docx";
Chances are that you have actually not configured the mapping at all. So you cannot really access the file, until you do the mapping.
Start by connecting to the FTP server with some GUI FTP client, and try to locate the file. The client will show you the correct path to use in your code.
The next problem you have, is that you need to enable the FTP passive mode. The default active mode will hardly work, if you are behind a firewall on NAT. See my article on network configuration needed for active and passive FTP modes.
To switch to the passive mode, use the ftp_pasv function.
ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
ftp_pasv($ftp_conn, true);
And yes, you can read without downloading.
See
PHP: How do I read a .txt file from FTP server into a variable? or
Stream FTP download to output

Related

Send file to ftp-server

I need to transfer an file created but I keep getting the following error:
Warning: ftp_put(): Can't open data connection for transfer of "/server.txt"
I can access the ftp via an ftp application and I can create and transfer files via the application but when I try to do the same via PHP it doesn't work.
This is my code.
$serverFile = "server.txt";
$localfile = "/var/sites/store/publish/store-201908-1323-rev9286/www/text.txt";
$localfile = "text.txt";
//Connect to ftp
$ftp_server = "111.111.111.11";
$ftp_user_name = "username";
$ftp_user_pass = "password";
/*set up basic ssl connection*/
$ftp = ftp_ssl_connect($ftp_server);
// login with username and password
$login_result = ftp_login($ftp, $ftp_user_name, $ftp_user_pass);
ftp_pasv($ftp, true);
if($login_result){
if (ftp_put($ftp, $serverFile, $localfile, FTP_ASCII))
{
Mage::log("successfully uploaded \n",null,"ftp.log");
}
}
ftp_close($ftp);
Am I missing something here, I do not have access to change anything on the ftp-server but I am aware that we have remotely placed files in the server previously.
Here is also a screenshot of the Ftp-connection I am using in WinSCP. I've tried adding the port. I also tried creating a file on the server and did a ftp_get but got the same result.
I checked your code, and server.txt file will be root of path.
In general, ftp user do not have write permission on root.
Please create new directory with 777 permission and try to upload.

ftp_put(): Can't open that file: No such file or directory

I search a lot on internet but i don't find a solution.
I need upload a file to an FTP server through PHP.
I tested various script PHP like this below but I always receive the same problem (I tries with absolute, normal and other path):
connected
Warning: ftp_put(): Can't open that file: No such file or directory in /web/htdocs/www.stanem.it/home/csv/importinnovacsv.php on line 20
There was a problem while uploading /web/htdocs/www.stanem.it/home/csv/test.csv
What I have to do?
<?php
$ftp_server="ftp.xxxx.it";
$ftp_user_name="user";
$ftp_user_pass="psw";
// connect and login to FTP server
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_user_name, $ftp_user_pass);
if($login) {
echo 'connected<br>';
// Where I need to put file
$local_file = '/web/htdocs/www.stanem.it/home/csv/test.csv';
// Where I copy the file
$server_dir = 'ftp://15886:XDBbcFYQUs#ftp.innovaspa.it';
// upload a file
if (ftp_put($ftp_conn, $server_dir, $local_file, FTP_ASCII)) {
echo "successfully uploaded $local_file\n";
exit;
} else {
echo "There was a problem while uploading $local_file\n";
exit;
}
}
The $remote_file argument of ftp_put is a path to the file on the FTP server.
You are passing a URL (and it even misses any path or file name).
It should be like:
$remote_file = "/remote/path/test.csv";
Once you resolve the path problem, you will also likely face data connection problems, as your are using the default active mode. See PHP ftp_put fails.

download file from ftp not working

i have following code for download file from ftp client is.
<?php
// connect and login to FTP server
$ftp_server = "IP";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, "USER", "PASSWORD");
$local_file = "files/syslog.txt";
$server_file = "syslog/syslog.txt";
// download server file
if (ftp_get($ftp_conn, $local_file, $server_file, FTP_ASCII))
{
echo "Successfully written to $local_file.";
}
else
{
echo "Error downloading $server_file.";
}
// close connection
ftp_close($ftp_conn);
?>
but when i run this program i get following error.
Warning: ftp_get(files/syslog.txt): failed to open stream: Permission denied in C:\wamp\www\demo\schedule_readtxt.php on line 11
Warning: ftp_get(): Error opening files/syslog.txt in C:\wamp\www\demo\schedule_readtxt.php on line 11
Error downloading syslog/syslog.txt.
what am i doing wrong here?
I think you've a permission issue.
It seems to me that you are trying to write a file "syslog.txt" in a folder that's not existing: "files" or your script can't access.
Does the folder "files" exists beside your php script?
And if it does, does apache (or the user that runs the php script) has the right to write and access this folder?
change variable $local_file to full path "C:\path\to\file\syslog.txt" also make Apache have write permission to that folder

PHP ftp_get failed to open stream when trying to download file

I'm trying to make the browser download a file from an FTP server, but whatever I try, I'm getting this error:
Warning: ftp_get(taak4.docx) [function.ftp-get]: failed to open stream: Permission denied in /home/jamesmr117/domains/notepark.be/public_html/classes/taak.php on line 231
Warning: ftp_get() [function.ftp-get]: Error opening taak4.docx in /home/jamesmr117/domains/notepark.be/public_html/classes/taak.php on line 231
I am however 100% sure my FTP server is working fine, as uploading files works correctly. I also set every folder to chmod 777. Does anyone know what the problem might be?
My php code:
$local_file="taak4.dockx";
$server_file="taak4.dockx";
ftp_get($FTPClient->connectionId, $local_file, $server_file, FTP_BINARY);
Thanks in advance !
you must specify the full path to the file. For example:
/var/home/victor/files/taak4.dockx
Use $_SERVER['DOCUMENT_ROOT'] for get document root dir path.
You need to have write permission on the $local_file path. Make it a full path. Example: chmod 777 /test and make $local_file be like /test/taak4.docx.
I was also suffering from this issue even after changing the file permissions on my remote server i was not able to download it on my local server:
Warning: ftp_get(): Can't open Capture.PNG: No such file or directory in C:\MAMP\htdocs\ftp.php on line 25
SOLUTION:
One must include '/' before writing any path in the $server_file variable so the whole example that works just perfect is here:
// This is the path and new file name on my server (name can be different from the remote server file )
// if i want to save it just where my current php file is running ,no need to enter any path just file name
$local_file = 'capture.png';
// This is the path and file name on my Remote server from which i am downloading from
// this should start with '/' and write 'public_html' or 'htdocs' afterwards
$server_file = '/public_html/Capture.PNG';
// ftp details
$ftp_server="example.host.com";
$ftp_user_name="username";
$ftp_user_pass="password";
// 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);
// This is to so that we do not time out
ftp_pasv($conn_id, true);
// 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);

File won't upload to ftp server

I'm trying to upload a file from my webserver to my game server through a script. The problem is that it can't find the directory.
The full directory is /174.34.132.106 port 27015/tf/addons/sourcemod/configs/tf2items.weapons.txt
This path didn't work so I asked the hosting about it and they insisted that /tf/addons/sourcemod/configs/tf2items.weapons.txt is the correct path but this doesn't work either. The game server is running on a windows server and i'm pretty sure the web server is running on linux. Is my code wrong, do I have to replace the spaces in the directory with %20. Thanks in advance!
$ftp_server="174.34.132.106";
$ftp_user_name="Username";
$ftp_user_pass="Password";
$remote_file = "tf2items.weapons.txt";
$file = "weapons/tf2items.weapons.txt";//tobe uploaded
if(!file_exists($file)) echo "The local file does not exist";
$conn_id = ftp_connect($ftp_server) or die('Unable to create the connection');
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_chdir($conn_id, "174.34.132.106 port 27015/tf/addons/sourcemod/configs/");
echo ftp_pwd($conn_id);
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
exit;
} else {
echo "There was a problem while uploading $file\n";
exit;
}
// close the connection
ftp_close($conn_id);
I noticed that the FTP server you're connecting to is using a non-standard port, so it's probably not making the connection. You need to specify the port in the ftp_connect, like so:
$ftp_server="174.34.132.106";
$ftp_port = "27015";
$ftp_user_name="username";
$ftp_user_pass="password";
$remote_file = "tf/addons/sourcemod/configs/tf2items.weapons.txt";
$file = "weapons/tf2items.weapons.txt";//tobe uploaded
// set up basic connection
$conn_id = ftp_connect($ftp_server,$ftp_port) or die('Unable to create the connection');
The die() will stop the script if it's unable to make the connection. You can add the same after your ftp_login line to make sure that it's actually logging in.
Edit To make sure your file exists, try this above the ftp_put line.
if(!file_exists($file)) echo "The local file does not exist";
Edit 2 After reading down on ftp_put, it says that the remote_file does not support directories. You'll need to use ftp_chdir first.
ftp_chdir($conn_id, "tf/addons/sourcemod/configs/");
Then for remote_file, use just tf2items.weapons.txt. You can still use filepaths for the local file.
If that's a Linux server ensure that you use correct case for directory names.
"tf/addons/sourcemod/configs" is not the same as "TF/addons/sourcemod/configs";

Categories