Showing posts with label PHP FTP. Show all posts
Showing posts with label PHP FTP. Show all posts

Monday, November 11, 2013

CentOS 6.x make visible .htaccess file on ftp / vsftpd

Server OS: Centos 5.x / VSFTPD

Client: Windows 7 /Filezilla

You have uploaded an .htaccess file to your FTP space. You click refresh and the file disappears. Do not worry. The file is there – it is simply not displaying as it is classed as a hidden file. If you have shell access then SSH to your server and run:

 ls -a

You should see the file listed.
In this scenario, to see the hidden files via your FTP client edit the vsftpd.conf file:

 vi /etc/vsftpd/vsftpd.conf

And add the following line force_dot_files=YES
Dont forget to restart the FTP service! /sbin/service vsftpd restart



Sunday, November 10, 2013

CentOS 6.4 VSFTP Problem - 553 Could not create file

Do you have SELinux enabled?

To check: sestatus

If SELinuxstatus = on & Current mode = enforcing, it's all enabled... so lets turn it to permissive mode.

Permissive mode: setenforce 0

Check sestatus again, and it should be Current mode = permissive. This basically leaves SELinux on, but in a log only manner.

With selinux in permissive mode, try your FTP things again, and see if the problems have magically gone away. If they have, you have issues with SELinux. Check /var/log/audit for reasons. Best thing to do is clear the log files, do your FTP thing, then check the logs as they'll then only contain what's related to your FTP issue.

If SELinux is to blame, there's a few SELinux booleans worth looking at.

Best start is: getsebool -a | grep ftp

You'll see something like

allow_ftpd_anon_write --> off
allow_ftpd_full_access --> on
allow_ftpd_use_cifs --> off
allow_ftpd_use_nfs --> off
ftp_home_dir --> on
ftpd_connect_db --> off
httpd_enable_ftp_server --> off
tftp_anon_write --> off

Not all of these are relevant to vsftpd, for example the httpd_enable_ftp_server is for Apache running FTP, and tftp_anon_write is for tftpd. I'd start with:

setsebool -P allow_ftpd_full_access on
setsebool -P ftp_home_dir on

The first one of allow_ftpd_anon_write is needed if you want every joe blow out there to be able to upload files to the anonymous folder... seriously discourage you doing that, unless it's internal only. If this is publicly accessibly, it will be found and it will be abused.

Next you'll need to make sure that the home folders have the right settings enabled.

Check policy bits: ls -alZ /home

Chances are if you just created the home folders they have the wrong settings, so you can just reset them as follows:

First run it to see what it'll change, but without it changing anything: restorecon -nvvr /home
-n = don't do anything, report only
-vv = very verbose
-r = recursive (do /home and everything under it)

You might want to run this on /home/udo, /home/ftp-docs and /home/ftpuser instead of all of /home

If you're happy with what it wants to do: restorecon -vvr /home
Same command with no -n, so it'll do it's actions.

Next stick SELinux back into enforcing mode:
setenforce 1

Check it is with sestatus (current mode = enforcing).

Try FTP again, and if it's broken once more check the audit logs to see what else is going on. Might be missing some settings, which you can sort out using chcon. Use -Z on various commands to work out what the current selinux policies in play are (ps -Z, ls -Z, and so on).

There's a few tools out there to turn the audit log into something more human readable, together with suggestions on what to do to fix it. Never used any of them so I can't speak much about them in anyway.

Monday, May 28, 2012

PHP ftp upload images


To upload a file to ftp you need a html form where you can insert the ftp details, like the ones described above:
server – is the server on which he wanna upload the file
username – is the user for which he makes the connection to ftp server
password - is the user password for ftp username
path to server – is the path on the ftp server which he wanna upload his file.
user file – is the file of the you wanna upload to ftp server
Hint: If you want to offer to your website members a form where he will upload his file, you should take the ftp server details and path, from a config file. Here we made a complete example how to upload through ftp putting all the details.
We will have a POST form and an enctype of multipart/form-data because we have a file on our form.
The user form that the user will insert info is like that:
<form action="upload.php" method="POST" enctype="multipart/form-data">
<table align="center">
<tr>
<td align="right">
Server:
</td>
<td>
<input size="50" type="text" name="server" value="">
</td>
</tr>
<tr>
<td align="right">
Username:
</td>
<td>
<input size="50" type="text" name="user"  value="">
</td>
</tr>
<tr>
<td align="right">
Password:
</td>
<td>
<input size="50" type="text" name="password" value="" >
</td>
</tr>
<tr>
<td align="right">
Path on the server:
</td>
<td>
<input size="50" type="text" name="pathserver" >
</td>
</tr>
<tr>
<td align="right">
Select your file to upload:
</td>
<td>
<input name="userfile" type="file" size="50">
</td>
</tr>
</table>
<table align="center">
<tr>
<td align="center">
<input type="submit" name="submit" value="Upload image" />
</td>
</tr>
 
</table>
</form>
Now, when the user submits the form, we should get all the details that he inputed and then upload the file to the ftp server.
// the file name that should be uploaded
$filep=$_FILES['userfile']['tmp_name']; 
// ftp server
$ftp_server=$_POST['server'];
//ftp user name
$ftp_user_name=$_POST['user'];
//ftp username password
$ftp_user_pass=$_POST['password'];
//path to the folder on which you wanna upload the file
$paths=$_POST['pathserver'];
//the name of the file on the server after you upload the file
$name=$_FILES['userfile']['name'];
To upload the file , first we should establish a connection to the ftp server using ftp_connect and specifing as a parameter the ftp servver. This function return a connection id.
$con_id=ftp_connect($ftp_server);
After connecting to server we should login using the function ftp_login passing three parameters: connection id, ftp user, ftp user password and checking if the login was sucessfull
// 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".".....";
   }
After loging in we can upload the file to the server and after that check if the file was uploaded correctly:
// upload the file
$upload = ftp_put($conn_id, 'public_html/'.$paths.'/'.$name, $filep, FTP_BINARY);
 
// check upload status
if (!$upload) {
       echo "FTP upload has failed!";
   } else {
       echo "Uploaded $name to $ftp_server ";
   }
In the end we should close the ftp connection using ftp_close passing the connection id:
ftp_close($conn_id);
For uploading big files you should set the time limit of the server in order not to finish the script while it’s uploading:
set_time_limit(300);
Here is the complete code:
<?
if(!isset($_POST["submit"])){?>
 
<form action="upload.php" method="POST" enctype="multipart/form-data">
<table align="center">
<tr>
<td align="right">
Server:
</td>
<td>
<input size="50" type="text" name="server" value="">
</td>
</tr>
<tr>
<td align="right">
Username:
</td>
<td>
<input size="50" type="text" name="user"  value="">
</td>
</tr>
<tr>
<td align="right">
Password:
</td>
<td>
<input size="50" type="text" name="password" value="" >
</td>
</tr>
<tr>
<td align="right">
Path on the server:
</td>
<td>
<input size="50" type="text" name="pathserver" >
</td>
</tr>
<tr>
<td align="right">
Select your file to upload:
</td>
<td>
<input name="userfile" type="file" size="50">
</td>
</tr>
</table>
<table align="center">
<tr>
<td align="center">
<input type="submit" name="submit" value="Upload image" />
</td>
</tr>
 
</table>
</form>
<?}
else 
{
 
 set_time_limit(300);//for setting 
 
$paths=$_POST['pathserver'];
 
$filep=$_FILES['userfile']['tmp_name'];
 
$ftp_server=$_POST['server'];
 
$ftp_user_name=$_POST['user'];
 
$ftp_user_pass=$_POST['password'];
 
$name=$_FILES['userfile']['name'];
 
 
 
// set up a connection to ftp server
$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 and login result
if ((!$conn_id) || (!$login_result)) {
       echo "FTP connection has encountered an error!";
       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 to the path specified
$upload = ftp_put($conn_id, $paths.'/'.$name, $filep, FTP_BINARY);
 
// check the upload status
if (!$upload) {
       echo "FTP upload has encountered an error!";
   } else {
       echo "Uploaded file with name $name to $ftp_server ";
   }
 
// close the FTP connection
ftp_close($conn_id); 
 
}
?>