I'm trying to open a folder from a distant server. I wrote :
if ($folderHandle = opendir($folder))
where $folder = "ftp://xxx:xxx#xxx.net:21"
I get the weird error Warning: opendir(ftp://...:21): failed to open dir: operation failed in ... on line 38
Any ideas as to where I should go from here ? Is it a problem with the FTP credentials ?
You could use PHP's FTP Capabilities to remotely connect to the server and get a directory listing:
// set up basic connection
$conn_id = ftp_connect('otherserver.example.com');
// login with username and password
$login_result = ftp_login($conn_id, 'username', 'password');
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
exit;
}
// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
// Retrieve directory listing
$files = ftp_nlist($conn_id, '/remote_dir');
// close the FTP stream
ftp_close($conn_id);
Related
This is a very basic question since I do not properly understand the underlying concepts of the different protocols. Please be indulgent.
I can upload files to my site with FTP using Filezilla. Now I'm developing a user interface for sending data to update a database and files (photos) to be saved in a directory. I send this data with POST (see here). I get the file, but cannot save it to the destination directory performing
move_uploaded_file($_FILES['PhotoToUpload']['tmp_name'],"../../Photos/".$file_name);
I think is because of permissions (currently 755). Since I can currently upload with Filezilla FTP (so I understand I have the permissions to do that),
my question is:
Is bad practice (or conceptually wrong) to take the temp file created after the POST and load it with a FTP code like the following (taken from here)?
<?php
$ftp_server = "myftp.co.uk";
$ftp_user_name = "myusername";
$ftp_user_pass = "mypass";
$destination_file = "../../Photos/";
$source_file = $_FILES['PhotoToUpload']['tmp_name'];
// set up basic connection
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true);
// 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";
}
// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
// close the FTP stream
ftp_close($conn_id);
?>
I am trying to upload my local file to server with the following code copied from stackoverflow. I couldnt map the files right and looking for assistance.
I got two test cases with $localfile.
mapping with relative path using c:, it throws filename prohibited.
mapping with server path. it says connected and written successfully. But in server the file size is 0KB only.
The code:
//$local_file = 'C:\Program Files\EasyPHP-DevServer-14.1VC11\data\localweb\projects\alaks\filestoanotherserver\sample.csv';
//Warning: ftp_put(): Prohibited file name: C:\Program Files\EasyPHP-DevServer-14.1VC11\data\localweb\projects\alaks\filestoanotherserver\sample.csv in C:\Program Files\EasyPHP-DevServer-14.1VC11\data\localweb\projects\alaks\filestoanotherserver\test.php on line 28
// The filename that i got the php code is test.ph
$local_file = 'sample.csv';
//Connected!Successfully written to sample.csv - but not written.
//The file size is showing 0KB.
$server_file = 'http://iseedtechnologies.in/sample.csv';
// set up basic connection
$conn_id = ftp_connect("iseedtechnologies.in");
// login with username and password
$login_result = ftp_login($conn_id, '****', '****');
echo is_array(ftp_nlist($conn_id, ".")) ? 'Connected!' : 'not Connected! :(';
ftp_pasv($conn_id, true);
// try to download $server_file and save to $local_file
if (ftp_put($conn_id, $local_file, $server_file, FTP_BINARY)) {
echo "Successfully written to $local_file\n";
} else {
echo "There was a problem\n";
}
// close the connection
ftp_close($conn_id);
?>
Working code (Removed the http:// and it worked)
$local_file = 'sample.csv';
$server_file = 'sample.csv';
// set up basic connection
$conn_id = ftp_connect("iseedtechnologies.in");
// login with username and password
$login_result = ftp_login($conn_id, '****', '****');
echo is_array(ftp_nlist($conn_id, ".")) ? 'Connected!' : 'not Connected! :(';
ftp_pasv($conn_id, true);
// try to download $server_file and save to $local_file
if (ftp_put($conn_id, $local_file, $server_file, FTP_BINARY)) {
echo "Successfully written to $local_file\n";
} else {
echo "There was a problem\n";
}
// close the connection
ftp_close($conn_id);
?>
I am trying to connect to a server which doesn't have any domain name like 'abc.com'. Rather I have to access it through its IP address.
I tried using ftp here, but the ftp_connect() doesn't support IP address.
Following is the code that I have tried..
<?php
echo $ftp_server="182.8.87.89";
$ftp_user_name="";
$ftp_user_pass="";
$file = "new.txt";//tobe uploaded
$remote_file = "test/heelo.txt";
// set up basic connection
$conn_id = ftp_connect($ftp_server);
//echo 21;
// login with username and password
echo $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) or die();
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
exit;
} else {
echo "There was a problem while uploading $file\n";
exit;
}
// close the connection
ftp_close($conn_id);
The error that I get isWarning: ftp_login() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\te\seperateupload.php on line 26.
I was working on with a FTP upload from my php page.
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
flush();
$ftp_server = "myserver";
$ftp_user_name="myuser";
$ftp_user_pass="mypass";
$remote_file="myfile.txt";
$file="myfile.txt";
// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
?>
It gives me a Warning in browser like
Warning: ftp_put(): Access is denied. in /var/www/html/ftpcheck.php on line 17. There was a problem while uploading myfile.txt.
I checked the access permissions on the file. But its accessible. Can any one tell me why this happens?
Most probably this is a permission issue. When you are uploading a file via FTP, you also need to check the directory's permission. When you're saying it's accessible, it doesn't mean it's writable.
You don't check the result of the login operation:
if (ftp_login($conn_id, $ftp_user_name, $ftp_user_pass)) {
echo "Connected as $ftp_user_name#$ftp_server\n";
} else {
echo "Couldn't connect as $ftp_user_name\n";
}
You should also try a manual FTP operation from the PHP host to the FTP host to ensure that you can log-on with these credentials and put a file. This will help you establish whether it's your code at fault or the FTP credentials.
<?php
class Ftp {
function upload()
{
$ftp_server="50.56.113.39";
$ftp_user_name="******";
$ftp_user_pass="***";
$file = "form.pdf";//tobe uploaded
$remote_file = "uploads1/test.pdf";
// 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);
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY))
{
echo "successfully uploaded $file\n";
//exit;
}
else
{
echo "There was a problem while uploading $file\n";
//exit;
}
// close the connection
ftp_close($conn_id);
}
}
?>
I am trying to write a small php function that will upload files to an FTP server and I keep getting the same error but I cannot find any fix by googling the problem, I am hoping you guys can help me here...
The error I get is: Warning: ftp_put() [function.ftp-put]: Unable to build data connection: No route to host in .
The file was created at the FTP server but it is zero bytes.
Here is the code:
<?php
$file = "test.dat";
$ftp_server="ftp.server.com";
$ftp_user = "myname";
$ftp_pass = "mypass";
$destination_file = "test.dat";
$cid=ftp_connect($ftp_server);
if(!$cid) {
exit("Could not connect to server: $ftp_server\n");
}
$login_result = ftp_login($cid, $ftp_user, $ftp_pass);
if (!$login_result) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user";
}
$upload = ftp_put($cid, $destination_file, $file, FTP_BINARY);
if (!$upload) {
echo "Failed upload for $source_file to $ftp_server as $destination_file<br>";
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
ftp_close($cid);
?>
I forgot to put FTP in passive mode using:
ftp_pasv($cid, true);