Connecting to ftp server - php

I am trying to connect to multilple ftp servers using ftp_connect and ftp_login. My code is very simple as given
foreach ($ftp_accounts as $ftp_account) {
$connect_id = $ftp_account['server'];
$user = $ftp_account['user'];
$pass = $ftp_account['password'];
echo 'Ftp Server: '.$connect_id.'<br> Username: '.$user. '<br> Password: '.$pass.'<br>';
if (ftp_login($connect_id, $user, $pass)){
echo "| Connected to server " . $connect_id . " as " . $user . '<br>';
} else {
echo "Couldn't connect to $connect_id as $user \n";
}
}
I have a table in my database with multiple servers and their credentials. but the code i wrote give me the following error (warning) message
ftp_login() expects parameter 1 to be resource, string given
and it goes to the else statement for every server.
Any idea what I am doing wrong ?
Thanks in advance

You need to use ftp_connect() to connect to the server before trying to login. Additionally, you need to check the result of ftp_connect() to ensure it was able to connect before you try to login. If it wasn't then it returns FALSE and you should not proceed to try and login.
foreach ($ftp_accounts as $ftp_account) {
$connect_id = ftp_connect($ftp_account['server']);
if($connect_id !== false)
{
$user = $ftp_account['user'];
$pass = $ftp_account['password'];
echo 'Ftp Server: '.$connect_id.'<br> Username: '.$user. '<br> Password: '.$pass.'<br>';
if (ftp_login($connect_id, $user, $pass)){
echo "| Connected to server " . $connect_id . " as " . $user . '<br>';
} else {
echo "Couldn't connect to $connect_id as $user \n";
}
} else {
echo 'failed to connect to server';
}
}

Replace
<?php
$connect_id = $ftp_account['server'];
?>
with
<?php
$connect_id = ftp_connect($ftp_account['server']);
?>
Hope this helps.

Let me answer my question. First of all the replies I get was all corect options to be checked for such problems I highly appriciate the replies but one thing you also need to check is that wether your server firelwall allows to connect to any outgoing ftp server or not ?
If it does not allow you to connect then even with everything written correct in your code you will not be able to connect to the server. So before testing your code check your server firewall settings.
It worked for me when i allowed all outgoing connections.
Once again Thanks for the replies

You forgot to make connection to server using ftp_connect function, assuming that in variable $ftp_account['server'] you have valid FTP server host address.
foreach ($ftp_accounts as $ftp_account) {
$connect_id = ftp_connect($ftp_account['server']);
if ($connect_id === false) {
echo "Failed to connect to FTP server";
continue;
}
$user = $ftp_account['user'];
$pass = $ftp_account['password'];
echo 'Ftp Server: ' . $ftp_account['server'] . '<br> Username: ' . $user . '<br> Password: ' . $pass . '<br>';
if (ftp_login($connect_id, $user, $pass)){
echo "| Connected to server " . $ftp_account['server'] . " as " . $user . '<br>';
} else {
echo "Couldn't connect to " . $ftp_account['server'] . " as " . $user . "\n";
}
}

Related

phpMyAdmin sql query is always returning 500 Internal Error?

I'm new to PHP.I have been trying to get data from Database and convert them to json . However when I open the chrome console. It says 500 : Internal Server Error.
Here is my PHP Code
<?php
function get_data(){
$link = mysqli_connect("localhost:8889","root2","root","EMPLOYEE");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
$sql = "SELECT * FROM people";
$result = mysqli_query($link,$sql);
$json_array = array();
while($row = mysql_fetch_array($result))
{
$json_array[] = array(
'id' => $row["id"],
'name' => $row["name"]
);
}
$json_array['name'] = "sarath";
$json_array['age'] = 19;
#echo $json_array;
return json_encode($json_array);
}
echo '<pre>';
print_r(get_data());
echo '</pre>';
?>
I get the following output at http://localhost:8888/peo.php. The json_array was not getting printed.
<pre>Success: A proper connection to MySQL was made! The my_db database is great.
Host information: localhost:8889 via TCP/IP
THE DATABASE IMAGE FROM PHPMyAdmin has been linked here
1) You open the connection using mysqli_connect but later use mysql_fetch_array. Change it to mysqli_fetch_array: those are different drivers.
2) Are you sure that you have got json addon installed? You can check it using phpinfo(); or just try to var dump: var_dump(json_encode(['something']));

Connection to database shows blank page

I have a file login.php to try to connect to my database.
I put the file login.ph inside the server folder and started the server.
Then i call the file in the browser and it shows a blank page. It does not respond even if i change the values of the database to an incorrect value.
I don't know if the error is inside the code or if it is another problem.
Thanks.
login.php:
<?php
$username = $_GET['fname'];
$password = $_GET['fpass'];
$con=mysqli_connect("localhost","user","pass","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$qz = "SELECT contact_id FROM contacts" ;
$qz = str_replace("\'","",$qz);
$result = mysqli_query($con,$qz);
while($row = mysqli_fetch_array($result))
{
echo $row['contact_id'];
}
mysqli_close($con);
?>
You must check $con variable that you have set with the result of the connection:
if (!$con) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
Do you have web server (say apache) installed and running on your server? You have to. Then put your file on the web server folder (say /var/www/html) and test in the browser.

Can't connect to MySQL server using PHP

I have MySQL server running on my local computer. I am trying to connect to it
through PHP from my website host. I get the following error:
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2).
Here is the php I am running with the userid and password wiped out for security:
if ($dbc = #mysql_connect('localhost', 'userid', 'password')) {
print '<p>Successfully connected to MySQL!</p>';
// Try to create the database:
if (#mysql_query('CREATE DATABASE myblog',$dbc)) {
print '<p>The database has been created!</p>';
} else { // Could not create it.
print '<p style="color: red;">Could not create the database because:<br />' . mysql_error() . '.</p>';
}
// Try to select the database:
if ($x == #mysql_select_db('myblog,$dbc')) {
print '<p>The database has been selected.</p>';
} else {
print '<p style="color: red;">Could not select the database because:<br />' . mysql_error() . '.</p>';
}
mysql_close(); // Close the connection.
} else {
print '<p style="color: red;">Could not connect to MySQL:<br />' . mysql_error() . '.</p>';
Can someone help me with this connection?
It seems that your code should work unless your MySQL server is not properly configured... but you may try the MySQLi Route.
$servername = "computername";
$username = "username";
$password = "password";
$dbname = "dbname";
// Create connection
$mysqli = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>

add users in microsoft active directory via php ldap_add

I have tried adding users via php ldap Active-Directory for Microsoft Server 2008 R2 datacenter, but I can't. I always get this error :
An error occurred. Error number 64: Naming violation
The code is:
<?php
$ldaprdn = 'administrador#correo.mx';
$ldappass = 'dir378prob#';
$ds = 'correo.mx';
$dn = 'ou=usuarios,dc=correo,dc=mx';
$puertoldap = 389;
$ldapconn = ldap_connect($ds,$puertoldap)or die("ERROR: I Don'n connect to LDAP.");
if ($ldapconn)
{
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION,3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS,0);
$con = ldap_bind($ldapconn, $ldaprdn, $ldappass);
if ($con)
{
$info["cn"] = $_POST['cn'];
$info["sn"] = $_POST['sn'];
$info["mail"] = $_POST['mail'];
$info["objectclass"] = "inetorgperson";
// prepare DN for new entry
$dn_aux = "mail=" . $_POST['mail'] . ",ou=usuarios,dc=correo,dc=mx";
$result = ldap_add($ldapconn, $dn_aux, $info);
if($result)
{
echo "New entry with DN " . $dn . " added to LDAP directory.";
}
// else display error
else
{
echo "An error occurred. Error number " . ldap_errno($conn) . ": " .
ldap_err2str(ldap_errno($conn));
}
}
else
{
echo "LDAP bind error...";
}
}
ldap_close($ldapconn);
?>
I'm taking my first steps in this ldap, so please could you explain in detail.
Not sure of the correct PHP syntax but the following line :
$dn_aux = "mail=" . $_POST['mail'] . ",ou=usuarios,dc=correo,dc=mx";
is not correct concerning an Active-Directory. The explanation is that in such a Directory your are not able to choose the attribute you use for naming an object. For example an 'InetOrgPerson' object MUST use the CN attribute to name it. For more details read carefuly naming attributes in object naming from Microsoft documentation.
try :
dn_aux = "CN=" . $_POST['cn'] . ",ou=usuarios,dc=correo,dc=mx";

Getting the name of an Exchange mailbox to connect to it programmatically

I am writing a PHP script to connect to an Exchange server and read messages from a mailbox. I have it connecting to my inbox just fine. What I'm trying to do now is get PHP to connect to a different mailbox that I have access to (Let's call it "Test Mailbox").
I tried this code:
imap_open( '{mail.domain.com:143}Test Mailbox', 'myusername', 'mypassword' );
But it said the mailbox doesn't exist. How can I get a list of mailboxes or get the path to the mailbox?
You should really think about doing this with Exchange Web Services (EWS). This will get you the data you want via SOAP versus IMAP which isn't going to be able to produce alot of things.
You can get a list of mailboxes with imap_getmailboxes(). As for selecting a mailbox, try without the leading /.
From php.net:
$mbox = imap_open("{mail.domain.com:143}", "username", "password", OP_HALFOPEN)
or die("can't connect: " . imap_last_error());
$list = imap_getmailboxes($mbox, "{mail.domain.com:143}", "*");
if (is_array($list)) {
foreach ($list as $key => $val) {
echo "($key) ";
echo imap_utf7_decode($val->name) . ",";
echo "'" . $val->delimiter . "',";
echo $val->attributes . "<br />\n";
}
} else {
echo "imap_getmailboxes failed: " . imap_last_error() . "\n";
}
imap_close($mbox);

Categories