FTP file upload using php - php

I need to upload a file automatically from a local computer to a remote server. I have found the following code on here:
<?php
require_once('ftp.php');
// 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_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);
?>
ftp.php is my file with the ftp authentication information. The connection works but I am getting the following error:
There was a problem while uploading C:/xampp/htdocs/testbcos/accounting/checkslastmonth.csv
EDIT: I amnot sure if this makes a difference or not, but here are my $remote_file and my $file:
$file = "C:/xampp/htdocs/testbcos/accounting/checkslastmonth.csv";//tobe uploaded
$remote_file = "/home/bookcell/public_html/testbcos/accounting/checkslastmonth3.csv";
What am I doing wrong here? Also, is it possible to do this if the file is on a mapped drive on my local server?
Thanks.

First thing: Try to set passive mode. You need it if you're sitting behind a firewall. (What probably is the case)
ftp_pasv($conn_id, true); // after ftp_login
Second, you have to change to dir first:
ftp_chdir($conn_id, '/home/bookcell/public_html/testbcos/accounting/');
ftp_put($conn_id, 'checkslastmonth3.csv', $file, FTP_ASCII);
If you want to know what's really going on, try to get error message with error_get_last() or
$php_errormsg

Related

php downloading a file from ftp doesn't work locally using XAMPP works only with older files on ftp

When I run the code below the local file gets deleted, I cannot work out why:
$local_file = 'test.csv';
$server_file = 'test_directory/test2.csv';
$ftp_server="servername";
$ftp_user="test";
$ftp_pass="pass";
$conn_id = ftp_connect($ftp_server);
// login with username and password`
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);
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);
So I get an error on the console showing cannot open no file or directory.
Edit:
Renaming the file to something else other than I am getting from ftp does not delete the file but still doesn't work.
I am running this in XAMMP htdocs location.
Edit:
Problem is if I add a new file to the ftp server, it works with existing ones even though the permission is the same.
Problem is a corrupt file on the ftp server. I tried with a different file and it works.

Storing files in a subdomain/custom folder using the Storage function in Laravel

I'm trying at the moment to upload files onto a subdomain, hosted on the same server, while the Laravel application is hosted on the main domain. Is there any way to use the Storage functions with the subdomain? Or anything similar?
You can use FTP to transfer file from your server to another server.
First, the receiver must have ftp account.
Second, you can use this below code to transfer your file [source].
<?php
$file = 'somefile.txt';
$remote_file = 'readme.txt';
// 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_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
?>

Send files from One server to another.

I have large files (total ~250mb) on my web server. I want to send these files quickly to my 2nd server without using an ftp client. I know the ftp password, username etc of my 2nd server, but how to do it without an ftp client?
$file = 'somehugefile.big';
$remote_file = '/uploads/somehugefile.big';
// 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";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
This example is adapted from the official docs -> http://www.php.net/manual/en/function.ftp-put.php
You will have to change 'upload_max_filesize' and 'post_max_size' in PHP.ini to a higher value as well.
ini_set('upload_max_filesize', '250M');
ini_set('max_execution_time', '999');
ini_set('memory_limit', '500M');
ini_set('post_max_size', '250M');

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

enable debug mode in PHP, Window Dos

in windows command line.
how to enable debug in ftp commands,
second questions
in PHP how to enable the debug mode,
below is the simple ftp script,
How to get step by step script execution trace for the FTP.
i guess there is something debug mode in php, please advise that mode,
<?php
$file = 'somefile.txt';
$remote_file = 'readme.txt';
// 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_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
?>
run your php script in the terminal to get step by step script execution trace, use:
/usr/local/php/bin/php filename.php
=)

Categories