Other

PHP 5 File Upload

Configure The “php.ini” File

First, ensure that PHP is configured to allow file uploads.

In your “php.ini” file, search for the file_uploads directive, and set it to On:

file_uploads = On

Create The HTML Form

Next, create an HTML form that allow users to choose the file they want to upload:

<form action=”upload.php” method=”post” enctype=”multipart/form-data”>
Please choose a file: <input type=”file” name=”uploadFile”><br>
<input type=”submit” value=”Upload File”>
</form>

Create The Upload File PHP Script

The “upload.php” file contains the code for uploading a file:

<?php
$target_dir = “uploads/”;
$target_dir = $target_dir . basename( $_FILES[“uploadFile”][“name”]);
$uploadOk=1;

if (move_uploaded_file($_FILES[“uploadFile”][“tmp_name”], $target_dir)) {
echo “The file “. basename( $_FILES[“uploadFile”][“name”]). ” has been uploaded.”;
} else {
echo “Sorry, there was an error uploading your file.”;
}
?>

Check if File Already Exists

Now we can add some restrictions.

First, we will check if the file already exists in the “uploads” folder. If it does, an error message is displayed, and $uploadOk is set to 0:

if (file_exists($target_dir . $_FILES[“uploadFile”][“name”])) {
echo “Sorry, file already exists.”;
$uploadOk = 0;
}

Limit File Size

The file input field in our HTML form above is named “uploadFile”.

We can now use $uploadFile_size to check the size of the file. If the file is larger than 500kb, an error message is displayed, and $uploadOk is set to 0:

if ($uploadFile_size > 500000) {
echo “Sorry, your file is too large.”;
$uploadOk = 0;
}

Limit File Type

The code below checks to be sure the user is NOT uploading a PHP file to your site. If they do upload a PHP file, an error message is displayed , and $uploadOk is set to 0:

if ($uploadFile_type == “text/php”) {
echo “Sorry, no PHP files allowed.”;
$uploadOk = 0;
}

The code below only allows users to upload .gif files. All other types are given an error before setting $uploadOk to 0:

if (!($uploadFile_type == “image/gif”)) {
echo “Sorry, only GIF files are allowed.”;
$uploadOk = 0;
}

Complete Upload File PHP Script

The complete “upload.php” file now looks like this:

<?php
$target_dir = “uploads/”;
$target_dir = $target_dir . basename( $_FILES[“uploadFile”][“name”]);
$uploadOk=1;

// Check if file already exists
if (file_exists($target_dir . $_FILES[“uploadFile”][“name”])) {
echo “Sorry, file already exists.”;
$uploadOk = 0;
}

// Check file size
if ($uploadFile_size > 500000) {
echo “Sorry, your file is too large.”;
$uploadOk = 0;
}

// Only GIF files allowed 
if (!($uploadFile_type == “image/gif”)) {
echo “Sorry, only GIF files are allowed.”;
$uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo “Sorry, your file was not uploaded.”;
// if everything is ok, try to upload file
} else { 
if (move_uploaded_file($_FILES[“uploadFile”][“tmp_name”], $target_dir)) {
echo “The file “. basename( $_FILES[“uploadFile”][“name”]). ” has been uploaded.”;
} else {
echo “Sorry, there was an error uploading your file.”;
}
}
?>

PHP array_search()

Example

Search an array for the value “red” and return its key:

<?php
$a=array(“a”=>”red”,”b”=>”green”,”c”=>”blue”);
echo array_search(“red”,$a);
?>

Run example »

PHP array_replace()

Example 1

Search an array for the value “red” and return its key:

<?php
$a=array(“a”=>”red”,”b”=>”green”,”c”=>”blue”);
echo array_search(“red”,$a);
?>

Example 2

Search an array for the value 5 and return its key (notice the “”):

<?php
$a=array(“a”=>”5″,”b”=>5,”c”=>”5”);
echo array_search(5,$a,true);
?>

PHP array_combine()

Syntax

array_combine(keys,values);

 

Example

Create an array by using the elements from one “keys” array and one “values” array:

<?php
$fname=array(“Peter”,”Ben”,”Joe”);
$age=array(“35″,”37″,”43”);

$c=array_combine($fname,$age);
print_r($c);
?>

Parameter Description
keys Required. Array of keys
values Required. Array of values

PHP array_change_key_case()

Example

Change all keys in an array to uppercase:

<?php
$age=array(“Peter”=>”35″,”Ben”=>”37″,”Joe”=>”43”);
print_r(array_change_key_case($age,CASE_UPPER));
?>

PHP count_chars() Function

Example

Return a string with all the different characters used in “Hello World!” (mode 3):

<?php
$str = “Hello World!”;
echo count_chars($str,3);
?>

PHP bin2hex() Function

Example

Convert “Hello World!” to hexadecimal values:

<?php 
$str = bin2hex(“Hello World!”);
echo($str); 
?>

PHP addcslashes() Function

Example

Add a backslash in front of the character “W”:

<?php 
$str = addcslashes(“Hello World!”,”W”);
echo($str); 
?>