I need to use php to upload to an ftp server 4 files. I have the following example code, that I am working from. How could this code be changed to upload multiple files that were already on the server (not uploaded at the time of the ftp transfer).
Lets say I have 4 files in a subfolder relative to the php file that does the upload, lets call the subfolder “/fileshere/” with the following 4 files in it:
file1.zip
file2.zip
file3.zip
file4.zip
I need the script to upload each of the files, then give a done message.
Below is the starting code I am using and trying to adapt. Any help would be much appreciated:
$ftp_server = "ftp.yourserver.com";
$ftp_user_name = "ftpuser";
$ftp_user_pass = "ftppassword";
$remote_dir = "/target/folder/on/ftp/server";
// 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);
//default values
$file_url = "";
if($login_result) {
//set passive mode enabled
ftp_pasv($conn_id, true);
//check if directory exists and if not then create it
if(!#ftp_chdir($conn_id, $remote_dir)) {
//create diectory
ftp_mkdir($conn_id, $remote_dir);
//change directory
ftp_chdir($conn_id, $remote_dir);
}
$file = $_FILES["file"]["tmp_name"];
$remote_file = $_FILES["file"]["name"];
$ret = ftp_nb_put($conn_id, $remote_file, $file, FTP_BINARY, FTP_AUTORESUME);
while(FTP_MOREDATA == $ret) {
$ret = ftp_nb_continue($conn_id);
}
if($ret == FTP_FINISHED) {
echo "File '" . $remote_file . "' uploaded successfully.";
} else {
echo "Failed uploading file '" . $remote_file . "'.";
}
} else {
echo "Cannot connect to FTP server at " . $ftp_server;
}
Try this code
<?php
// connect and login data
$web = 'www.website.com';
$user = 'admin';
$pass = 'password';
// file location
$server_file = '/public_html/example.txt';
$local_file = 'example.txt';
//connect
$conn_id = ftp_connect($web);
$login_result = ftp_login($conn_id,$user,$pass);
//uploading
if (ftp_put($conn_id, $server_file, $local_file, FTP_BINARY))
{echo "Success";}
else {echo "Failed";}
?>
if you want to upload multiple files just put your files names into array then put whole code into for loop .
Related
<?php
$local_file = 'filename';
$remote_file = '/folder name/filename';
$ftp_server ='IP';
$ftp_user_name = 'NAME';
$ftp_user_pass = 'PW';
// 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);
// turn passive mode on
ftp_pasv($conn_id, true);
// upload a file
if (ftp_get($conn_id, $remote_file, $local_file, FTP_BINARY)) {
echo "successfully uploaded $local_file\n";
} else {
echo "There was a problem while uploading $local_file\n";
}
// close the connection
ftp_close($conn_id);
?>
I'm using the above script successfully on a different server with file located in the root folder. But on this one I need to fetch a file that's in a sub-directory (let's say 'folder name' for now). I've tried different things but all throw an error "failed to open stream: No such file or directory".
I've tried:
$remote_file = '/folder name/filename.csv';
$remote_file = './folder name/filename.csv';
$remote_file = '../folder name/filename.csv';
$remote_file = '/../folder name/filename.csv';
It's my first time dealing with ftp_get command. Can someone please help I'm badly stuck... don't know what I'm doing wrong here
For those who might run into the same issue, I've sorted it out by added the following lines of code (using the function ftp_chdir) and it worked fine:
// turn passive mode on
ftp_pasv($conn_id, true);
echo "Current directory: " . ftp_pwd($conn_id) . "\n";
// try to change the directory to somedir
if (ftp_chdir($conn_id, "Live Stock")) {
echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
} else {
echo "Couldn't change directory\n";
}
// upload a file
if (ftp_get($conn_id, $remote_file, $local_file, FTP_BINARY)) {
echo "successfully uploaded $local_file\n";
} else {
echo "There was a problem while uploading $local_file\n";
}
I have these lines of code which uploads a file via FTP. After uploading, I need to unzip the file. Problem encountered was, the file is successfully uploaded but I cannot unzip it. Can anyone help me with this?
if(isset($_POST['submit'])){
$file = $_FILES['uploaded_file']['name'];
$remote_file = $_FILES['uploaded_file']['name'];
$ftp_server = "1xx.xx.xx.xx";
$ftp_user_name = "xxuser";
$ftp_user_pass = "xx2016";
$toform2 = "FormType/Upload/";
$tounzip2 = "Unzip/";
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_pasv($conn_id, true);
// upload the file
$ftype = substr ($file,0,2);
if ($ftype == "F2") {
ftp_chdir ($conn_id,$toform2);
$upload = ftp_put($conn_id,$remote_file,$file,FTP_ASCII);
$file_path = $toform2;
// check upload status
if($upload){
// Unzip file
$zip = new ZipArchive();
$x = $zip->open($toform2);
if ($x === true) {
ftp_chdir ($conn_id,$tounzip2);
$zip->extractTo($tounzip2);
$zip->close();
echo "success"."</br>";
}else{
echo "fail";
}
//echo "Uploaded $source_file to $ftp_server as $destination_file" ;
}else{
//echo "FTP upload has failed!" ;
}
}
If the script has SSH access to the remote server, you can ssh2-exec a remote script to unzip your file.
SSH2-Connect:
http://php.net/manual/en/function.ssh2-connect.php
SSH2-Exec:
http://php.net/manual/en/function.ssh2-exec.php
First upload your file using FTP. (SFTP would be better)
Open an SSH connection and execute the unzip command remotely.
I want to upload the directory content which includes only text files to a ftp server and it should be automated through windows task schedular. Right now I'm using this but its only uploading one file, My requirement is basically a user can copy its files to a specific folder and it should be uploaded on ftp server asap.
this is the code I'm using right now:
$host = 'ftp.xyz.com';
$usr = 'abc123';
$pwd = 'l2345';
date_default_timezone_set('Asia/Kuala_Lumpur');
$date = date('d-m-Y_hi');
$name = $date.".txt";
$local_file = '/Applications/XAMPP/xamppfiles/htdocs/Conversion/Upload/xyz/abc.txt';
$ftp_path = '/public_html/xyz/abc.txt';
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
$upload = ftp_put($conn_id, $ftp_path, $local_file, FTP_BINARY);
echo (!$upload) ? 'Cannot upload' : 'Upload complete';
echo "\n";
if (!function_exists('ftp_chmod')) {
function ftp_chmod($ftp_stream, $mode, $filename){
return ftp_site($ftp_stream, sprintf('CHMOD %o %s', $mode, $filename));
}
}
if (ftp_chmod($conn_id, 0666, $ftp_path) !== false) {
echo $ftp_path . " chmoded successfully to 666\n";
} else {
echo "could not chmod $file\n";
}
ftp_close($conn_id);
To upload multiple files, you'll want to either issue a series of FTP (connections and) transfers with a loop in your code...or use a wildcard in FTP's interactive mode.
I am having a php enabled server1. I have a php code for file upload. But i need the file to be saved on server2.
I have FTP access to server2.
While searching i found this code,
<?php
$ftp_server = "199.53.23.1";
$ftp_user_name = "xxxx";
$ftp_user_pass = "**********";
$remote_dir = "http://server2/Images/";
// 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);
//default values
$file_url = "";
if($login_result) {
//set passive mode enabled
ftp_pasv($conn_id, true);
$file = $_FILES["uploadedfile"]["tmp_name"];
$remote_file = $_FILES["uploadedfile"]["name"];
$ret = ftp_nb_put($conn_id, $remote_file, $file, FTP_BINARY, FTP_AUTORESUME);
while(FTP_MOREDATA == $ret) {
$ret = ftp_nb_continue($conn_id);
}
if($ret == FTP_FINISHED) {
echo "File '" . $remote_file . "' uploaded successfully.";
} else {
echo "Failed uploading file '" . $remote_file . "'.";
}
} else {
echo "Cannot connect to FTP server at " . $ftp_server;
}
?>
It said unable to connect to Server.
Anyone have an idea on this type of requirement?
Please help.
UPDATE
Server2 doesn't support PHP
Here your line
$login_result = #ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
Remove #
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
For SFTP
$conn_id = ssh2_connect($ftp_server, 22);
ssh2_auth_password($conn_id, $ftp_user_name, $ftp_user_pass);
$sftp = ssh2_sftp($conn_id);
Uses example: $stream = fopen("ssh2.sftp://$sftp/path/to/file", 'r');
*send a file
ssh2_scp_send($conn_id, '/local/filename', '/remote/filename', 0644);
*fetch file
ssh2_scp_recv($conn_id, '/remote/filename', '/local/filename');
*Create a new folder
ssh2_sftp_mkdir($sftp, '/home/username/newdir');
*Rename the folder
ssh2_sftp_rename($sftp, '/home/username/newdir', '/home/username/newnamedir');
*Remove the new folder
ssh2_sftp_rmdir($sftp, '/home/username/newnamedir');
*Create a symbolic link
ssh2_sftp_symlink($sftp, '/home/username/myfile', '/var/www/myfile');
*Remove a file
ssh2_sftp_unlink($sftp, '/home/username/myfile');
How can I save an image over FTP? Here's my code:
$conn_id = ftp_connect('***');
$login_result = ftp_login($conn_id, '***','***');
if( $login_result) echo 'connected';
$save = "FTP://temp/". $FileName;
imagepng($this->Picture, $save);
/*if (ftp_put($conn_id,$save,$save, FTP_ASCII))
echo "successfully uploaded \n";
else
echo "There was a problem while uploading \n";
*/
You could do:
ob_start();
imagepng($this->Picture);
$image = ob_get_clean();
$stream = stream_context_create(array('ftp' => array('overwrite' => true)));
file_put_contents("ftp://user:pass#host/folder/".$FileName, $image, 0, $stream);
Hope this helps you
<?
//Configuration
$ftp_server = "ftpaddress";
$ftp_user = "username";
$ftp_pass = "password";
// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
//trying to connect with login details
if (#ftp_login($conn_id, $ftp_user, $ftp_pass)) {
echo "Connected! ";
} else {
echo "Couldn't connect!";
}
//You can change it with the file name, this is for if you're upload using form.
$myName = $_POST['name']; //This will copy the text into a variable
$myFile = $_FILES['file_name']; // This will make an array out of the file information that was stored.
?>
<?PHP
$destination_path = "dest/path/";
//your desired path for uploading)
$destination_file = $destination_path."img.jpg";
//This will create a full path with the file on the end for you to use, I like splitting the variables like this in case I need to use on on their own or if I'm dynamically creating new folders.
$file = $myFile['tmp_name'];
//Converts the array into a new string containing the path name on the server where your file is.
$upload = ftp_put($conn_id, $destination_file, $file, FTP_BINARY);// upload the file
if (!$upload) {// check upload status
echo "FTP upload of $destination_file has failed!";
} else {
echo "Uploaded $file to $conn_id as $destination_file";
}
?>