How to add an FTP user online with PHP in FreeBSD

Source: Internet
Author: User
Tags exit in ftp mysql administrator password mysql database

Recently, the leader asked me to plan a web design contest and Flash creation contest, required to achieve online registration and upload works. I realized the requirement through FREEBSD+APACHE+PHP+MYSQL+FTP.

The idea of online registration and uploading works is to use Web Forms to collect data from users to be stored in the MySQL database, and to create an FTP upload account with the user's registration name and create a corresponding directory for the user.

FTP Server is the system by default, using the user name and password of the system user, create the system user is equal to create the FTP user, FreeBSD is the operating system belonging to the Unix camp, it does not have like Linux useradd and Groupadd and so on to create the user and group's command, Instead, the PW command is implemented with the corresponding parameters, and the command to create a user as an administrator on FreeBSD is

echo <passwd> | PW Useradd <username> [-g][groupname] [-s][shelldir][-h 0]

Parameter g Specifies the user group, and the parameter s specifies the user's shell.

If the normal user logged in, you must also use the SU command, the method is called

Su root–c ' echo <passwd> | PW Useradd <username> [-g][groupname] [-s][shelldir][-h 0] '

After execution, the system will ask for an administrator password and enter the password to execute the command as an administrator.

The main difficulty in implementing this step is how to invoke the above system commands through PHP to create a user, this example is implemented using the Popen () function in PHP, which executes the instruction to open the file, the syntax is int popen (String command, String mode), its open file can only be one-way, can only read or write, the corresponding "string mode" is "R" or "W", "String command" is the command string, in the operation of the file can use Fgets (), FGETSS () With the fputs () function, this example uses the Fputs () function to enter the administrator password into the file. If an error in the open file will return a value of false, the last function should remember to call Pclose () off.

Here we plan the FTP user group, we first use PW Groupadd ftpuser to create the Ftpuse group, so that the online application users for the group members. For security reasons, we should not give FTP users telnet permission, so we also have to create a shell for them, so that they can not log through Telnet normal system, the following method: First create a file/bin/ftponly

#!/bin/csh

/bin/cat << XX

Can ony use this username to login ftp server!

And you can don't use it to telnet to this system! Xx

Sleep 10

Between xx in this file is displayed to the user who logged in Telnet to see the information. The information will automatically exit in 10 seconds. Finally, do not forget to use chmod +x/bin/ftponly to give this file executable properties.

Then add "/bin/ftponly" to the/bin/shell file, and in the future command we can use the PW-s parameter to assign the shell to the FTP user.

Finally also pay attention to a problem, Su command is only wheel management group of users can use, when the PHP call su command must also be wheel group members, or the system refused to run, and PHP run System command is the identity of the Apache Web server running identity, The initial user name and user group are nobody, so you have to set up a wheel group of users www for Apache use, Then change the user in the Apache profile httpd.conf to Www,group as wheel, restart Apache, and you can run as a new user.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.