Security of PHP scripts

Security of PHP scripts: Part III

This column will round off our three-page series on security PHP scripting by discussing three important issues: the safety of file uploads, issues surrounding error reporting, and PHP safe mode.

File uploads

PHP allows you to handle file uploads from users easily. The following script implements a basic upload form.

if(!isset($file)) {
<form method=post enctype="multipart/form-data" action="<? echo $PHP_SELF; ?>"?>
Upload a file: <input type=file name=file /> <br />
<input type=submit value="Upload" />
} else {
if(is_uploaded_file($file)) {
if($file_size > 1000000) {
echo "The uploaded file is too large";
} else {
move_uploaded_file($userfile, "/usr/local/apache/uploads/");
echo "Your file was successfully uploaded";
} else {
echo "Invalid upload. Try again with a valid file upload";

The first part of the script tests to see if the file, $file, was uploaded. If not, it presents the user with the upload form. Notice that the <form> element has an attribute 'enctype' set to 'multipart/form-data'. This is necessary when allowing file uploads.

The example script tests for an important security threat: whether or not the file named in $file is an uploaded file. When PHP accepts a file upload, it saves the uploaded file in a temporary directory and provides the script with its filename in the variable set in the script (in our example, $file). A malicious user could manipulate this situation. Consider an upload script that contained the code:

if(isset($file)) {
/* ... */

What if the user did not actually upload a file but rather passed a filename as follows: The script would effectively run: move("../bin/httpd","/usr/local/apache/uploads").Given that most people install their scripts in /usr/local/apache/htdocs, this would mean that PHP would move the location of the Apache binary (httpd) to another directory. If the server was to be stopped and restarted, the restart would fail since the Apache binary would not exist where expected. This would cause a complete denial of service.

To safeguard against this, you can also access upload file variables, such as $file, through the global array $HTTP_POST_FILES["file"].

The script tests for another security problem: the size of the file, $file_size. If this test did not take place, a user could upload files of any size and intentionally exhaust the server's disk space.

Security of error reporting

When developing a script, PHP's error warnings can be very useful in diagnosing bugs. However, once you publicly deploy your script it serves no purpose, and could expose details of the internals of your script to users. As such, it is always best to redirect errors to a log file. This can easily be done by setting the `error_log' directive in the PHP configuration file, php.ini.

Safe mode

Safe mode supplements the operating system's permission system by further isolating PHP from the rest of the file system. To turn on safe mode, set the 'safe_mode' directive in php.ini to 'On'.

In safe mode, PHP can only access files owned by the Web server user - usually called 'nobody'. This means a malicious user would not be able to manipulate a script to read system configuration and other important files.

Further still, scripts can be prohibited from using risky functions - such as those involved in file system manipulation, execution of external programs or any other kind of function - by explicitly disabling such functions using the 'disable_functions' directive. A comma-separated list of function names will ensure that they can never be used. An error such as the following will be generated: "Warning: system() has been disabled for security reasons. in safe.php on line 401".

Finally, whenever you deploy PHP on a public system always be sure to keep up with new releases of PHP to ensure that you are protected against any security problems with older releases.

Join the PC World newsletter!

Error: Please check your email address.

Our Back to Business guide highlights the best products for you to boost your productivity at home, on the road, at the office, or in the classroom.

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments


Microsoft L5V-00027 Sculpt Ergonomic Keyboard Desktop

Learn more >

Lexar® JumpDrive® S57 USB 3.0 flash drive

Learn more >


Lexar® JumpDrive® S45 USB 3.0 flash drive 

Learn more >


Lexar® Professional 1800x microSDHC™/microSDXC™ UHS-II cards 

Learn more >

Audio-Technica ATH-ANC70 Noise Cancelling Headphones

Learn more >

HD Pan/Tilt Wi-Fi Camera with Night Vision NC450

Learn more >

Lexar® JumpDrive® C20c USB Type-C flash drive 

Learn more >


Back To Business Guide

Click for more ›

Most Popular Reviews

Latest News Articles


GGG Evaluation Team

Kathy Cassidy


First impression on unpacking the Q702 test unit was the solid feel and clean, minimalist styling.

Anthony Grifoni


For work use, Microsoft Word and Excel programs pre-installed on the device are adequate for preparing short documents.

Steph Mundell


The Fujitsu LifeBook UH574 allowed for great mobility without being obnoxiously heavy or clunky. Its twelve hours of battery life did not disappoint.

Andrew Mitsi


The screen was particularly good. It is bright and visible from most angles, however heat is an issue, particularly around the Windows button on the front, and on the back where the battery housing is located.

Simon Harriott


My first impression after unboxing the Q702 is that it is a nice looking unit. Styling is somewhat minimalist but very effective. The tablet part, once detached, has a nice weight, and no buttons or switches are located in awkward or intrusive positions.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?