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);
?>
Related
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 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);
}
I am doing a system of upload the archives and extract for ftp but as php dont have function "ftp_unzip", I must do an archive to extract inside host.
<?php
//Upload the image file to the server
$ftp_server = 'luis.habboproject.net';
$ftp_user_name= 'login';
$ftp_user_pass = 'senha';
echo $_FILES['userfile']['name'];
$source_file= "archive.zip";
$destination_file="/public_html/archive.zip";
$source_file2= "oi.php";
$destination_file2="/public_html/oi.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);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
// turn passive mode on
ftp_pasv($conn_id, true);
// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
$upload2 = ftp_put($conn_id, $destination_file2, $source_file2, FTP_BINARY);
// check upload status
if (!$upload) {
} else{
// Code to Acting a page http://luis.habboproject.net/oi.php to extract the archive.zip in a host
}
// close the FTP stream
ftp_close($conn_id);
?>
I want to know if there is some function in php or javascript, that have same result of a "submit button", but without click. In other words, I want to execute the archive to active the extractor.
I'm curious how to upload file through FTP using PHP. Let's say I have SQL function and it will return a filename (dynamic name) and upload file using the return of filename. how can i do this?
i have try before and try in command prompt. but the error is "PHP Warning: ftp_put : failed to open stream: no such file or directory in ...."
my code :
<?php
$db = pg_connect("host=localhost port=5432 dbname=automationReporting user=postgres password=admin") or die("gagal konek.");
/$query = pg_query($db, "select lookup_cell();");
$arr = pg_fetch_array($query, 0, PGSQL_NUM);
$file = $arr[0].'.csv';
$remote_file = '/nury/'; // <-- my directory on server
$ftp_server = '192.168.1.128';
$ftp_user_name = 'polban';
$ftp_user_pass = 'polban2014';
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// Passive mode
// ftp_pasv ($conn_id, true);
$ftp = ftp_put($conn_id, $remote_file, $file, FTP_ASCII);
//var_dump($ftp); die();
// upload a file
if ($ftp) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
?>
Remove the comment for Passive mode and then try once it will work.
<?php
$db = pg_connect("host=localhost port=5432 dbname=automationReporting user=postgres password=admin") or die("gagal konek.");
/$query = pg_query($db, "select lookup_cell();");
$arr = pg_fetch_array($query, 0, PGSQL_NUM);
$file = $arr[0].'.csv';
$remote_file = '/nury/'; // <-- my directory on server
$ftp_server = '192.168.1.128';
$ftp_user_name = 'polban';
$ftp_user_pass = 'polban2014';
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// Passive mode
ftp_pasv ($conn_id, true);
$ftp = ftp_put($conn_id, $remote_file, $file, FTP_ASCII);
//var_dump($ftp); die();
// upload a file
if ($ftp) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
?>
i understand that the ftp_put method uploads a file from the local server computer to the ftp server but i have problems using it where when i try to execute a simple script like this:
<?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);
?>
the operation is successfully done except for that the file uploaded to the my ftp server is always with zero byte size!
also i tried to enable passive mode but it still uploads an empty file.
Try enabling track_errors and access $php_errormsg
ini_set('track_errors', 1);
// put operation
// if error
var_dump($php_errormsg);
I had the same issue. When I change "FTP_ASCII" to "FTP_BINARY" it solved my problem and files uploaded as expected.
I had ran into this as well. Bit late but thought I'd post my solution:
$file_name = "localfile.txt";
Get content from your existing file
$content = file_get_contents('http://www.somewhere.com/'.$file_name);
...or make temp file content
$content = "This is content";
upload file
// connect
$conn_id = ftp_connect($host);
$login = ftp_login($conn_id, $username, $password);
ftp_pasv($conn_id, true);
// create
$tmp = fopen(tempnam(sys_get_temp_dir(), $file), "w+");
fwrite($tmp, $content);
rewind($tmp);
// upload
$upload = ftp_fput($conn_id, "serverfile.txt", $tmp, FTP_ASCII);
// close
ftp_close($conn_id);