PHP ftp_pwd returns empty - php

I'm writing a php script that gets called via ajax in javascript. I'm tyring to connect to an ftp server and list all the files in the "public_html" folder.
I also wanted to print the current ftp dir but when when I try, it prints as empty in the console.
I can connect to the ftp server, but I can't tell what dir is the current one.
//Get ftp user/pass
$ftp_server = "ftp." . $_POST['hostname'];
$ftp_username = $_POST['user'];
$ftp_userpass = $_POST['pass'];
echo "console.log('attempting to connect to ftp host: $ftp_server, user: $ftp_username, password: $ftp_userpass');\n";
$ftp_conn = ftp_connect($ftp_server) or die("alert('Could not connect to $ftp_server');\n");
// turn passive mode on
//ftp_pasv($ftp_conn, true);
ftp_chdir($ftp_conn, 'public_html');
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
// then do something...
echo "console.log('Connected to FTP');\n";
// get contents of the current directory
$curdir = ftp_pwd($ftp_conn);
echo "console.log('Current dir: $curdir');\n";
$contents = ftp_nlist($ftp_conn, ".");
// output $contents
echo "var files = [];";
foreach ($contents as $file){
echo "var tempFile = '$file';\n
files.push(tempFile);\n";
}
echo "console.log(files);\n";
// close connection
ftp_close($ftp_conn);
?>

swap these two lines from:
ftp_chdir($ftp_conn, 'public_html');
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
to:
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
ftp_chdir($ftp_conn, 'public_html');

I have some problems, when using ftp_pasv();
Try to comment this line:
ftp_pasv($ftp_conn, true);

Related

how to get file from ftp server and copy on own server

I want to get file from client server and copy them on my server , I have successfully connected to client server, my code is below.
// connect and login to FTP server
$ftp_server = "xx.xxx.xxx.xxx";
$ftp_username = 'xxxxxxxxxxxxxxx';
$ftp_userpass = 'xxxxxxxxxxxxxxxx';
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
echo "<pre>";
print_r($login);
echo "</pre>";
// get the file list for /
$filelist = ftp_rawlist($ftp_conn, "/");
// close connection
ftp_close($ftp_conn);
echo "<pre>";
print_r($filelist);
echo "</pre>";
// output $filelist
var_dump($filelist);
May anyone please advise how can I achieve this?
You can use the ftp_fget function specified here: http://php.net/manual/en/function.ftp-fget.php
(ftp_fget() retrieves remote_file from the FTP server, and writes it to the given file pointer.)
Here an example provided by the documentation:
<?php
// path to remote file
$remote_file = 'somefile.txt';
$local_file = 'localfile.txt';
// open some file to write to
$handle = fopen($local_file, 'w');
// 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 $remote_file and save it to $handle
if (ftp_fget($conn_id, $handle, $remote_file, FTP_ASCII, 0)) {
echo "successfully written to $local_file\n";
} else {
echo "There was a problem while downloading $remote_file to $local_file\n";
}
// close the connection and the file handler
ftp_close($conn_id);
fclose($handle);
?>
This is how I resolved this now all files will copy on your server. use ftp_ssl_connect if its secure
$ftp_server = "xx.xxx.xxx.xxx";
$ftp_username = 'xxxxxxxxxxxxxx';
$ftp_userpass = 'xxxxxxxxxxxxxxxxxxx';
$ftp_conn = ftp_ssl_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
ftp_pasv($ftp_conn,pasv);
$output_directory="files1/ftpgetfiles/137/";
// get the file list for /
$filelist = ftp_nlist($ftp_conn, "/");
foreach ($filelist as $key => $value) {
$fp = fopen($output_directory.$value,"w");
if(ftp_fget($ftp_conn, $fp, $value, FTP_BINARY))
{
fclose($fp);
}
}
ftp_close($ftp_conn);

Getting multiple files over FTP with ftp_get and ftp_nlist

I'm attempting to get multiple files from another domain using ftp_get and ftp_nlist. ftp_nlist expects a resource and a string, but the below returns
ftp_nlist() expects parameter 1 to be resource, null given in
and
Invalid argument supplied for foreach()
<?php
// Connect and login to FTP server
$ftp_server = "hostname";
$ftp_username ="username";
$ftp_userpass = "password";
$includes = "/directory/";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
// Get file list
$contents = ftp_nlist($conn_id, $includes);
// Loop through for file 1
foreach ($contents as $file) {
$local_file = '/path/to/file.php';
$server_file = '/path/to/file.php';
ftp_get($conn_id, $local_file, $server_file, FTP_BINARY);
}
// Loop through for file 2
foreach ($contents as $file) {
$local_file = '/path/to/file.php';
$server_file = '/path/to/file.php';
ftp_get($conn_id, $local_file, $server_file, FTP_BINARY);
}
// close connection
ftp_close($ftp_conn);
?>
Variable $conn_id that you pass to ftp_nlist() is not defined. You need to pass $ftp_conn in all ftp_* functions instead. (ftp_get() in your case)
Check ftp_close() as well, to be sure you did not forget to close connection.
I'd recommend to use a wrapper for ftp_ functions, like https://github.com/dg/ftp-php, to make debugging easier. You will be able to use Exceptions and catch them like this:
try {
$ftp = new Ftp;
$ftp->connect($ftp_server);
$ftp->login($ftp_username, $ftp_userpass);
$ftp->nlist($includes);
} catch (FtpException $e) {
echo 'Error: ', $e->getMessage();
}

PHP ftp_put fails

I upload XML file through FTP:
$ftp = "ftp";
$username = "username";
$pwd = "password";
$filename = $_FILES[$xyz][$abc];
$tmp = $_FILES['file']['tmp_name'];
$destination = "/Content/EnquiryXML ";
$connect = ftp_connect($ftp)or die("Unable to connect to host");
ftp_login($connect,$username,$pwd)or die("Authorization Failed");
echo "Connected!<br/>";
if(!$filename)
{
echo"Please select a file";
}
else
{
ftp_put($connect,$destination.'/'.$filename,$tmp,FTP_ASCII)or die("Unable to upload");
echo"File successfully uploaded to FTP";
}
I want to send the XML file created using DOMDocument to a FTP server but I am not able.
The ftp_put returns false.
Most typical cause of problems with ftp_put (or any other transfer command like ftp_get, ftp_nlist, ftp_rawlist, ftp_mlsd) is that PHP defaults to the active mode. And in 99% cases, one has to switch to the passive mode, to make the transfer working. Use the ftp_pasv function.
$connect = ftp_connect($ftp) or die("Unable to connect to host");
ftp_login($connect, $username, $pwd) or die("Authorization failed");
// turn passive mode on
ftp_pasv($connect, true) or die("Unable switch to passive mode");
The ftp_pasv must be called after ftp_login. Calling it before has no effect.
See also:
PHP ftp_put fails with "Warning: ftp_put (): PORT command successful"
my article on the active and passive FTP connection modes.
Further, if your FTP server is reporting an incorrect IP address in the response to the PASV command (what is quite common, if the server is behind firewall/NAT), you might need to workaround it by using:
ftp_set_option($connect, FTP_USEPASVADDRESS, false);
See PHP FTP + Passive FTP Server Behind NAT.
Though the right solution in this case, is to get the server fixed.
This worked:
// connect and login to FTP server
$ftp_server = "host";
$ftp_username = "username";
$ftp_userpass = "password";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file ="$abc";
// upload file
if (ftp_put($ftp_conn, "/$abc" , $file, FTP_ASCII)){
echo "Successfully uploaded $file.";
} else {
echo "Error uploading $file";
}
// close connection
ftp_close($ftp_conn);

PHP ftp_nlist() returning bool(false) even with passive mode and different users

I'm currently making a script that will backup files that I have on my server but am having a major issue. About 30 minutes ago I was able to connect using the same script, list my files, and download them. But now when I try to connect, it connects and that is it, I can't ftp_nlist the files because when I do it just returns bool(false). I read on some other posts that I should try setting passive mode to true with ftp_pasv but it did not change anything. I've also tried other users which do the same exact thing. I tried using the credentials on filezilla and it connected and allowed me to download with no issues. Any help would be greatly appreciated, I'll post the code below.
PHP:
Connect to FTP
$ftp_server = "Hidden for security";
$ftp_user = "Hidden for security";
$ftp_pass = "Hidden for security";
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
ftp_pasv($conn_id, true);
Get filenames on FTP
$contents = ftp_nlist($conn_id, ".");
$rm = array(0, 1, 2, 3,);
foreach($rm as $value) {
unset($contents[$value]);
}
$contents = array_values($contents);
Get filenames on localhost
$dir = scandir("images/");
$rmval = array(0, 1, 2);
foreach ($rmval as $val) {
unset($dir[$val]);
}
$dir = array_values($dir);
Try to login
if (ftp_login($conn_id, $ftp_user, $ftp_pass)) {
//echo "Connected";
$counter = 0;
var_dump($contents);
/*foreach ($ftp_content as $file) {
ftp_get($conn_id, "images/" . $ftp_content[$counter], $ftp_content[$counter], FTP_BINARY);
$counter++;
}*/
} else {
echo "Couldn't Connect";
}
Close FTP connection
ftp_close($conn_id);
The issue was that I was trying to list my files before establishing a connection to the FTP server. Adding $login_result = ftp_login($conn_id, $ftp_user, $ftp_pass) after $conn_id fixed my issue. Now I'm able to evaluate if it's true or false and run different code depending on the result.

how to set file permission 0555 using ftp_chmod in php

How to set file permission 0555 using ftp_chmod?
<?php
$ftp_server = "example.com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, "user", "password");
$file = "path/index.php";
ftp_chmod($ftp_conn, 0555, $file)
?>
using this script i can not change file permission to 0555!! using this script i can change 0666,0777 but i cant not change to 0555
output of above script is 0655!!

Categories