I would like to copy a file (eg. MYFILE.csv) from FTP 'SOURCE' to FTP 'TARGET'. What type of command would you recommend for a script in php?
I have tried this but it didn't work.
<?php
$server = 'ftp.TARGET.com' ;//address of ftp server
$user_name = 'USER_TARGET'; // Username
$password = 'PASSWORD_TARGET'; // Password
$source = 'MYFILE.csv';
$dest = '/in/MYFILE.csv';
$mode='FTP_ASCII';
// set up basic connection
$connection = ftp_connect($server) ;
// login with username and password
$login_result = ftp_login($server, $user_name, $password);
// upload a file
if (ftp_put($connection, $dest, $source, $mode)) {
echo "successfully uploaded $source\n";
} else {
echo "There was a problem while uploading $source\n";
}
// close the connection
ftp_close($connection);
?>
=> The php script will be hosted in a folder on FTP A.
Thank you for your guidance.
You can use file_put_contents
http://php.net/manual/en/function.file-put-contents.php
file_put_contents('ftp://user:pass#server/path/to/file.txt', $data);
It will return false on a failure:
if(file_put_contents('ftp://user:pass#server/path/to/file.txt', $data)) {
// ftp upload successful
} else {
// ftp upload failed
}
Why does your current method fail though?
Related
This question already has answers here:
PHP ftp_put fails
(2 answers)
Closed 1 year ago.
I use PHP to upload images from a host into FTP remote server.
I have already stored the names of the files that need to be uploaded to the FTP remote server, stored on the database.
After connecting to DB and a query for select information of images, I passed variables with $_SESSION to another PHP file for upload them on FTP remote server like this:
conn_auth.php for select target images:
<?php
if (file_exists(__DIR__.'/conn_auth/db_conn.php')){
include __DIR__.'/conn_auth/db_conn.php';
}else{
die('Connection is Failed ...!');
}
$conn = OpenConn();
$sql = "SELECT * FROM users where status = 'Active'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$user_id = $row["id"];
//search for user_id in userAthentication Table in DB
$sql = "SELECT * FROM userauthentication where user_id = $user_id";
$result = $conn->query($sql);
$user_auth = $result->fetch_assoc();
$user = array(
'id'=>$row['id'],
'name'=>$row['name'],
'family'=>$row['family'],
'auth_image'=>'',
'file_name'=>$user_auth["image"],
'phone'=>$row["phone"]
);
$url = "/home/root/app_name_1/uploads/".$user["file_name"];
if(!file_exists($url)){
$url = "/home/root/app_name_2/uploads/".$user["file_name"];
}
$user['auth_image'] = $url;
session_start();
$_SESSION["userInfo"] = $user;
$_SESSION["userAuth"] = $user_auth;
if (file_exists(__DIR__.'/conn_auth/get_auth.php')){
include __DIR__.'/conn_auth/get_auth.php';
}else{
die("I can't Connect to the Server");
}
}
} else {
echo "0 results";
}
CloseConn($conn);
On this file, I try to connect to FTP remote server and upload images with the ftp_put() method, but it does not work:
<?php
include_once __DIR__.'../conn_auth.php';
session_start();
$ftp_server="*.***.***.**";
$ftp_user_name="****";
$ftp_user_pass="**********";
$file = $_SESSION["userInfo"]['auth_image']; //to be uploaded
$folder_name = $_SESSION["userInfo"]['phone'];
$file_name = $_SESSION["userInfo"]["file_name"];
$remote_file = "/HDD/pics/$folder_name";
$conn_id = ftp_connect($ftp_server,21);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_mkdir($conn_id,'auth_pics').ftp_chdir($conn_id,'auth_pics');
if (ftp_mkdir($conn_id,"$folder_name")){
ftp_chdir($conn_id,$folder_name);
}else{
echo "I Can't Create Folder For : ".$folder_name;
die();
}
// upload a file
$upload = ftp_put($conn_id, $remote_file, $file, FTP_BINARY);
if ($upload) {
echo "successfully uploaded : $file\n";
} else {
echo "There was a problem while uploading...\n";
}
// close the connection
ftp_close($conn_id);
?>
Mostly your FTP server doesn't support active mode connections, so you may try to switch to the passive mode using the ftp_pasv, but make sure to do this after you logged into the server (after ftp_login).
If you face some troubles while using the native API functions, you may want to try out an FTP client library, like this, that I've built.
Hope you find this answer useful for you.
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 am trying to upload image from one server to another server through FTP by using PHP.But the uploaded data shows.pureftpdupload.5809ed2f.15.7b36.24316ca6 error.
I am using this code.
$connection = 'servername';
$username = 'xxxx';
$password = 'yyyy';
$local_file = 'http://servername/test.jpg';
$remote_file = 'admin/files/company/test.jpg';
$connection = ftp_connect($server);
if (#ftp_login($connection, $username, $password)) {
// successfully connected
//echo 'connected';exit;
} else {
echo 'not connected';
return false;
}
if (ftp_put($connection, $remote_file, $local_file, FTP_BINARY)) {
echo "successfully uploaded\n";
} else {
echo "There was a problem while uploading \n";
}
My guess is that you want to upload a local file from a web server. But by mistake or misunderstanding, you are using file's HTTP URL (http://servername/test.jpg) instead of file's local path.
Try to use a real local path, like /home/user/test.jpg.
This may work as a generic solution: $_SERVER['DOCUMENT_ROOT']."/test.jpg"
I'm trying to download file from ftp, but whatever i tried i couldn't download any file from ftp and it is not giving any error.
My code fetching file names from database and compares these file names with existing ftp file(names). If file name exist on ftp i want to download this file to my local-destination folder.
I can read ftp file names but i can't download them. Hope you can help me.
Here is my code
error_reporting(E_ALL);
$ftp_server = "xx.xx.y12.34"; //server
$bt = ftp_connect($ftp_server); // connect to ftp
$username = "myusername"; $pass = "mypass"; //username and password
$connection = ftp_login($bt, $username, $pass); // login ftp with username and password
ftp_pasv($bt, true);
if ((!$bt) || (!$connection)) {
echo "failed";
exit;
}else {
$path = "/1_Swatch Files/LG Swatches/";
$destination_folder = "/home/faruk/lasenza/media/color_samples/";
// mysql connection codes..
$results = $readConnection->fetchAll($query); // fetch file names from database
$contents_on_server = ftp_nlist($bt, $path);
foreach($results as $result){
$check_file_exist = $result["CLR_CODE"].".gif";
if(in_array($check_file_exist, $contents_on_server)) {
$server_file = $result["CLR_CODE"].".gif";
if (ftp_get($bt, $destination_folder , $server_file, FTP_BINARY)) {
echo 'Succeeded';
}else{
echo "There was a problem";
}
break;
}
}
}
What I'm trying to do is automaticly upload a zip-file through FTP using PHP, and then extract the zip-file as well. However first things first: I'm having trouble with uploading the zip-file using PHP's ftp_put function. This is my script:
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $user, $pass); //<!--same as cPanel account user and pass?
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $user";
exit;
} else {
echo "Connected to $ftp_server, for user $user";
}
// server & file info
$file = 'phpBB3.zip';
$ftp_root = '/public_html/';
$site_root = $_SERVER['DOCUMENT_ROOT'].'/scripts/';
// >>>HERE<<<
// upload the file
// >>>HERE<<<
$upload = ftp_put($conn_id, $ftp_root .$file, $site_root . $file, FTP_BINARY);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $file to $ftp_server as $file";
}
// unzip the uploaded file (from FTP?)
$unzip = shell_exec("unzip {$file}"); //<!-- how to do this through FTP uploaded files?
// close the FTP stream
ftp_close($conn_id);
What's happening here is that it (SOMETIMES) uploads A PART of the file ( always the same amount; like 900 kB ) and sometimes doesn't upload anything at all. It's as if the uploading process is interrupted by the rest of the script being executed while the uploading wasn't finished yet. Though I'm not sure if that's the cause of the problem.
Though, it always gives me this error:
Warning: ftp_put() [function.ftp-put]:
Connecting to port 38694 in
/home/quicksit/public_html/createacct.php
on line 93
Where the output port is always different and always larger than 20.000 ( like 30.000-50.000 ).
Could anyone help me out on this?
Thanks in advanced,
Skyfe.
There is a timeout and upload limit in the configuration file for PHP... you may want to check the setting there.