uploading csv fie to ftp server - file empty - php

I am trying to store a file from my local server on an ftp server.
source file is in my current directory (same as php file)
I run the script with http://www.server.co.za/kisv2/xmltest/export.php
file to upload to ftp is http://www.server.co.za/kisv2/xmltest/exportfile.csv
destination ftp path is: ftp://ftp.ftpserver.co.za/LocExports/exportfile.csv
my ftp login defaults to: ftp://ftp.ftpserver.co.za
so I want to copy file from in current directory exportfile.csv to ftp://ftp.ftpserver.co.za/LocExports/exportfile.csv
My Current syntax is:
$source = 'exportfile.csv'; //this is a file in the same directory as my php file. full path is... http://www.server.co.za/kisv2/xmltest/exportfile.csv
$target = '/LocExports/exportfile.csv'; //full path is... ftp://ftp.ftpserver.co.za/LocExports/exportfile.csv
$conn = ftp_connect("ftp.ftpserver.co.za") or die("Could not connect");
ftp_login($conn, "username", "password");
$upload = ftp_put($conn, $target, $source, FTP_ASCII);
if (!$upload) { echo 'FTP upload failed!'; }
echo "complete";
This gives me error Warning: ftp_put() [function.ftp-put]: Opening ASCII mode data connection. the file does appear on the FTP server but is blank and 0bytes in size.
Any ideas welcome.
Thanks and regards
UPDATE
$source = 'exportfile.csv';
$target = '/LocExports/exportfile.csv';
$conn = ftp_connect("ftp.server.co.za") or die("Could not connect");
ftp_login($conn, "username", "password");
ftp_pasv($conn, true);
$upload = ftp_put($conn, $target, $source, FTP_BINARY);
if (!$upload) { echo 'FTP upload failed!'; }
echo "complete";
this still fails with:
Warning: ftp_put() [function.ftp-put]: Opening BINARY mode data connection.
file is created on ftp but empty.
thanks again

Add ftp_pasv($conn, true); after your ftp_login(...) .
From http://www.php.net/manual/en/function.ftp-put.php
If when using ftp_put you get the one of the following errors:
Warning: ftp_put() [function.ftp-put]: Opening ASCII mode data
connection
Warning: ftp_put() [function.ftp-put]: Opening BINARY mode data
connection
and it creates the file in the correct location but is a 0kb file and
all FTP commands thereafter fail. It is likely that the client is
behind a firewall. To rectify this use:
ftp_pasv($conn, true);
Before executing any put commands. Took me so long to figure this out
I actually cheered when I did :D

I was having the same problem. The file created but size 0 KB. after setting mode to passive, my file successfully transferred to FTP Server.
InFact there are 3 things we have to take care while uploading file on FTP Server.
set file type to BINARY: objFtpClient.setFileType(FTP.BINARY_FILE_TYPE);
set File Transfer Mode to BINARY: objFtpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
set Mode to Passive (i dont know what it does internally. but it works...!): objFtpClient.enterLocalPassiveMode();

Related

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

Unable To Upload File With FTP in PHP

Just now I got an error with my FTP file upload part. I am not able to upload a file via ftp using PHP. the code which I entered is as follows :
<?php
$conn_id = ftp_connect(localhost);
$login_result = ftp_login($conn_id, 'newuser', 'wampp') or die("Could Not Connect To FTP Server");
$image = $_FILES['image']['tmp_name'];
$upload = ftp_put($conn_id, 'sri/image.jpg', $image, FTP_ASCII);
?>
The error that it shows is as follows :
Warning: ftp_put() [function.ftp-put]: Filename invalid in D:\xampp\htdocs\mycloud\edit.php on line 7
Please help me out of this stuff.
I think the process of uploading files via FTP has to be:
Connect to FTP Server
Login to the FTP Server (if applicable)
Change to the right directory - (I believe you need to do this before attempting to upload the file in the sri folder). So you will need to go to the sri folder.
Upload the file (so in your case it should be image.jpg only NOT sri/image.jp )
and then close the connection to the FTP Server.
To change to current directory to the right directory, I think you need to do the following:
if(ftp_chdir($conn_id, "sri"))
{
echo "Current directory is now: " . ftp_pwd($conn_id) ;
}
else
{
echo "Error could not change directory";
}
More info on changing directories

PHP ftp_put warning Warning: ftp_put() [function.ftp-put]: Type set to I. in

When i try to upload files using PHP's ftp_put function, earlier it was erroring:
Warning: ftp_put() [function.ftp-put]: No data connection
Now, i tried to put passive mode on:
ftp_pasv($conn_id, true);
then comes error:
Warning: ftp_put() [function.ftp-put]: Type set to I. in
ftp_login is done properly and it says Successfully.
Now it gives new warning: Warning: ftp_put() [function.ftp-put]: abc.txt: Cannot open or remove a file containing a running program.
Any ideas, why file not tranferring ?
Thanks !
Here is my code snippet:
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) or die("You do not have access to this ftp server!");
if ((!$conn_id) || (!$login_result)) {
// wont ever hit this, b/c of the die call on ftp_login
echo "<span style='color:#FF0000'><h2>FTP connection has failed! <br />";
echo "Attempted to connect to $ftp_server for user $ftp_user_name</h2></span>";
exit;
} else {
//echo "Connected to $ftp_server, for user $ftp_user_name <br />";
}
//turn passive mode on
ftp_pasv($conn_id, true);
$upload = ftp_put($conn_id, $destination_file.$name, $filename, FTP_BINARY);
if (!$upload) {
echo "<span style='color:#FF0000'><h2>FTP upload of $filename has failed!</h2></span> <br />";
} else {
echo 'Uploaded';
}
ftp_close($conn_id);
http://php.net/ftp_pasv
$resource = ftp_connect('ftp.example.com');
ftp_login($resource, 'username', 'password');
# set this to true
ftp_pasv($resource, true);
ftp_get(...);
ftp_put(...);
I was recieving same (not very descriptive) error message E_WARNING ftp_get(): Type set to I..
I found out that it is because server running PHP did not have visible public IP (it is virtual server on my workstation).
Solution was using passive mode. Default setting (active mode) did not have problem on live server, because live server has visible public IP.
The last error you are seeing happens when the FTP daemon is stuck with the uploaded file open and waiting for you to write to it.
Anytime you successfully open a connection over an FTP server, be prepared to close the connection with the following function when the process completes or terminates due to any errors.
ftp_close($conn_id);
It's possible your script is leaving its connections open and the FTP server is getting confused by this. Try adding ftp_close in the appropriate places and see if the script runs more smoothly.
I've tried using the ftp functions in PHP and found it was much easier to use file_put_contents() like the following:
$remote_file = "ftp://username:password#host.com/path/to/file.txt";
file_put_contents($remote_file, $file_contents);
You can still check if it was successful and all that good stuff of course too.
Your ftp setup looks ok, try putting the filename $destination_file.$name in a single variable, dump the variable and make sure this file exists with absolute path if it is not in the same folder as your script. That is the only detail I saw in a quick glance, that could choke your upload.
Make sure your file is not opened in an editor! And if the file is .txt you can use FTP_ASCII although being in binary should not cause a problem.
Good-luck!
I found its solution as below:
I just talked to EUKHOST server support
Main point in this was that the support person now opened a passive port range for FTP on server, and he told us to try the FTP upload now. If you could try it with some testfile and it went through successfully..
Add following lines at the end of
open /etc/vsftpd.conf and add
pasv_promiscuous=YES___ at the end.
In my case, the issue triggering this error was that the file I was trying to upload was too large for the recieving server's configuration.

Categories