I want to upload all files to a specific directory
This script works fine
foreach (glob("*.*") as $filename)
{
ftp_put($ftp_conn, basename($filename), $filename, FTP_BINARY);
}
How to edit it to make it work to a specific directory? I tried this, but it didn't work:
// connect and login to FTP server
$usr = '*****';`enter code here`
$pwd = '******';
$ftp_server = "*******";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $usr, $pwd);
$ftp_path = '/public_html/';
foreach (glob("*.*") as $filename)
{
ftp_put($ftp_conn, $ftp_path, $filename, FTP_BINARY);
}
// check upload status:
print (!$upload) ? 'Cannot upload' : 'Upload complete';
print "\n";
// close connection
ftp_close($ftp_conn);
The second param in ftp_put() should include the path and the filename.
foreach (glob("*.*") as $filename) {
$ftp_path = $ftp_path = '/public_html/'.$filename;
ftp_put($ftp_conn,$ftp_path , $filename, FTP_BINARY);
}
Related
I try to upload a file using ftp_put after submitting a form. However my code does not work as expected:
$ftpHost = '192.168.180.36';
$ftpUsername = 'userdownload';
$ftpPassword = 'Toms!';
$filepath = "C://xampp/htdocs/Helpdesk/fmt/download";
$connId = ftp_connect($ftpHost,21) or die("Couldn't connect to $ftpHost");
$ftpLogin = ftp_login($connId, $ftpUsername, $ftpPassword);
$file_name = $_FILES['file']['name'][$i];
$tmp_name = $_FILES['file']['tmp_name'][$i];
// try to upload file
if(ftp_put($connId, $file_name, $filepath.'/'.$file_name, FTP_BINARY)){
echo "File transfer successful - $file_name";
}else{
echo "There was an error while uploading $file_name";
}
And I get this error message:
There was an error while uploading $file_name
There are two thing i noticed in your code. the first one is the mode of transfer depends upon your file type.Files that are in the ascii/text file extension list are transferred as ascii, all other files are transferred as binary.The second one is may be there is a problem in your file which you received may be it is black first check that. You can try this code it is working for me.
<?php
// connect and login to FTP server
$ftp_server = "ftp.example.com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file = "localfile.txt";
// upload file
if (ftp_put($ftp_conn, "serverfile.txt", $file, FTP_ASCII))
{
echo "Successfully uploaded $file.";
}
else
{
echo "Error uploading $file.";
}
// close connection
ftp_close($ftp_conn);
?>
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 try to upload file to FTP Server but i can not connect & can not upload file to FTP Server. Is there something wrong with my script ?
<?php
// FTP access parameters:
$host = 'mywebhost';
$usr = 'myusername';
$pwd = 'mypassword';
// file to upload:
$local_file = './ramli.doc';
$ftp_path = '/home/ramli/ramli.doc';
// connect to FTP server (port 21)
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
// send access parameters
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
// turn on passive mode transfers (some servers need this)
// ftp_pasv ($conn_id, true);
// perform file upload
$upload = ftp_put($conn_id, $ftp_path, $local_file, FTP_ASCII);
// check upload status:
print (!$upload) ? 'Cannot upload' : 'Upload complete';
print "\n";
/*
** Chmod the file (just as example)
*/
// If you are using PHP4 then you need to use this code:
// (because the "ftp_chmod" command is just available in PHP5+)
if (!function_exists('ftp_chmod')) {
function ftp_chmod($ftp_stream, $mode, $filename){
return ftp_site($ftp_stream, sprintf('CHMOD %o %s', $mode, $filename));
}
}
// try to chmod the new file to 666 (writeable)
if (ftp_chmod($conn_id, 0666, $ftp_path) !== false) {
print $ftp_path . " chmoded successfully to 666\n";
} else {
print "could not chmod $file\n";
}
// close the FTP stream
ftp_close($conn_id);
?>
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 .
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";
}
?>