I am trying to upload my local file to server with the following code copied from stackoverflow. I couldnt map the files right and looking for assistance.
I got two test cases with $localfile.
mapping with relative path using c:, it throws filename prohibited.
mapping with server path. it says connected and written successfully. But in server the file size is 0KB only.
The code:
//$local_file = 'C:\Program Files\EasyPHP-DevServer-14.1VC11\data\localweb\projects\alaks\filestoanotherserver\sample.csv';
//Warning: ftp_put(): Prohibited file name: C:\Program Files\EasyPHP-DevServer-14.1VC11\data\localweb\projects\alaks\filestoanotherserver\sample.csv in C:\Program Files\EasyPHP-DevServer-14.1VC11\data\localweb\projects\alaks\filestoanotherserver\test.php on line 28
// The filename that i got the php code is test.ph
$local_file = 'sample.csv';
//Connected!Successfully written to sample.csv - but not written.
//The file size is showing 0KB.
$server_file = 'http://iseedtechnologies.in/sample.csv';
// set up basic connection
$conn_id = ftp_connect("iseedtechnologies.in");
// login with username and password
$login_result = ftp_login($conn_id, '****', '****');
echo is_array(ftp_nlist($conn_id, ".")) ? 'Connected!' : 'not Connected! :(';
ftp_pasv($conn_id, true);
// try to download $server_file and save to $local_file
if (ftp_put($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);
?>
Working code (Removed the http:// and it worked)
$local_file = 'sample.csv';
$server_file = 'sample.csv';
// set up basic connection
$conn_id = ftp_connect("iseedtechnologies.in");
// login with username and password
$login_result = ftp_login($conn_id, '****', '****');
echo is_array(ftp_nlist($conn_id, ".")) ? 'Connected!' : 'not Connected! :(';
ftp_pasv($conn_id, true);
// try to download $server_file and save to $local_file
if (ftp_put($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);
?>
Related
Can you please assist me. I am trying to copy a file to the folder inside the application. The script is working on my local machine however when I run it on Cpanel server it shows an error "ftp_login() expects parameter 1 to be resource, Boolean given"
Here is the script which I tested
$folder_path = "192.xx.xx.xx\TMS";
$local_file = "CurrentFile\Inbound.xls";
$server_file = "CurrentFile\Inbound.xls";
//-- Connection Settings
$ftp_server = "192.xx.xx.xx"; // Address of FTP server.
$ftp_user_name = "FTP server username"; // Username
$ftp_user_pass = "FTP server Password"; // Password
$target = 'CurrentFile';
if (!file_exists($target))
{
die("Target directory doesn't exist.");
}
else if (!is_writable($target))
{
die("Insufficient privileges to write to the target directory!");
}
// 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";
}
function fileExists($path)
{
return (#fopen($path,"r")==true);
}
ftp_close($conn_id);
I think the script is failing to connect to the FTP server.
If I add this just after I create the connection It returns "connection Failed".
if(!$conn_id) {
die("Connection failed!");
}
I am not used this but I have suggested go to this tutorial ftp_login expects parameter 1 to be a resource and let me know what happen to this
I have searched a lot for the answer and found some results but none of one is working for me. I want to transfer zip file from one server to another server using ftp functions. File went transferred but without data. Here is my code as follows:
<?php
$file = 'LICENSE.zip';
$fp = 'LICENSE.zip';
$ftp_server = "ftpserver name";
$ftp_user_name = "ftp username";
$ftp_user_pass = "ftp password";
// set up basic connection
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// login with username and password
// upload a file
ftp_chdir($conn_id,'/dev');
if (ftp_put($conn_id, $file, $fp, FTP_BINARY)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
fclose($fp);
?>
Even I am unable to transfer a simple .php or .txt file with their data. In this case only files without data is moving.
I have two similar websites.Some of the content are same in both sites.I have some files in specific folder in one website say A.I want to copy some specific files from Website A to website B.
I have tried ftp functions in php but not working.
<?php
// define some variable
$local_file = 'eg.html';
$server_file = 'http://example.com/horoscope/M12459TAM_03092009_123239.html';
// set up basic connection
$conn_id = ftp_connect("example.com");
// login with username and password
$login_result = ftp_login($conn_id, 'username', 'password');
echo is_array(ftp_nlist($conn_id, ".")) ? 'Connected!' : 'not Connected! :(';
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);
?>
I get connected message but display "There was a problem".Pls can anyone try this..
Regards,
Rekha
change last part where ftp_get to ftp_put
if (ftp_put($conn_id, $local_file, $server_file, FTP_BINARY)) {
echo "Successfully written to $local_file\n";
} else {
echo "There was a problem\n";
}
$connection = ssh2_connect('IP address', Port_No);
ssh2_auth_password($connection, 'server_username', 'server_password');
ssh2_scp_send($connection,'File_Path','Destination_Path', 0644);
<?php
$local_file = "/home/zzh/Desktop/1.txt";
$server_file = "/var/www/helpdesk/1.txt";
$ftpserver = "xxx.xxx.xxx.xx";
$port = xx;
$conn_id = ftp_connect($ftpserver,$port) or die('can\'t connect to ftp');
$ftpname="xxx";
$ftppass = "xxx";
ftp_login($conn_id, $ftpname, $ftppass);
if (ftp_get($conn_id, $local_file, $server_file, FTP_ASCII)) {
echo "Successfully written to $local_file\n";
} else {
echo "There was a problem\n";
}
// close the connection
ftp_close($conn_id);
?>
i want use this php to login the ftp server download 1.txt to replace the 1.txt in the desktop, but it seems doesn't work, i am using ubuntu. needs help please.
ps. i can connect to the ftp successfully.
It should replace it if IIRC.
If not, why not do
if ( file_exists($local_file) ) unlink($local_file)
This would go before your ftp_get procedure.
I was working on with a FTP upload from my php page.
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
flush();
$ftp_server = "myserver";
$ftp_user_name="myuser";
$ftp_user_pass="mypass";
$remote_file="myfile.txt";
$file="myfile.txt";
// set up a connection or die
$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);
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
?>
It gives me a Warning in browser like
Warning: ftp_put(): Access is denied. in /var/www/html/ftpcheck.php on line 17. There was a problem while uploading myfile.txt.
I checked the access permissions on the file. But its accessible. Can any one tell me why this happens?
Most probably this is a permission issue. When you are uploading a file via FTP, you also need to check the directory's permission. When you're saying it's accessible, it doesn't mean it's writable.
You don't check the result of the login operation:
if (ftp_login($conn_id, $ftp_user_name, $ftp_user_pass)) {
echo "Connected as $ftp_user_name#$ftp_server\n";
} else {
echo "Couldn't connect as $ftp_user_name\n";
}
You should also try a manual FTP operation from the PHP host to the FTP host to ensure that you can log-on with these credentials and put a file. This will help you establish whether it's your code at fault or the FTP credentials.
<?php
class Ftp {
function upload()
{
$ftp_server="50.56.113.39";
$ftp_user_name="******";
$ftp_user_pass="***";
$file = "form.pdf";//tobe uploaded
$remote_file = "uploads1/test.pdf";
// 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);
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY))
{
echo "successfully uploaded $file\n";
//exit;
}
else
{
echo "There was a problem while uploading $file\n";
//exit;
}
// close the connection
ftp_close($conn_id);
}
}
?>