PHP advanced file upload form

PHP file upload script

Freelance Web Designer East Sussex

PHP file upload

<< Creating form | Restricting file types >>

Get notified of the new tutorials

 

Renaming file
As you have already noticed, our form has also a text field which we will be using to type in the full name of whoever would use this form. If you were using this form with the forum or any other application where you´d like to allow your users to upload their aviatars – it would be a good idea to give their image some unique name. You would most probably use their unique id number rather than their name, but for the purpose of this tutorial we´ll use the first and last name for the new name of the file.

We will need to slightly change our code at the top of the page.
First we need to find out what extension our uploaded file has by typing the following after the line which defines the constant with the path to the uploads folder:

//Find the extension
$flext = pathinfo($_FILES['frmfile']['name']);

We have created a variable called $flext, assigned the function pathinfo() to it and passed the original name of the file as a parameter $_FILES['frmfile']['name'].
This function returns information about the path of the file in the form of array.
The array will include the following:

  • dirname – the directory name
  • basename – name of the original file
  • extension – extension of the file
  • filename – name of the file without extension

All we need to do now is to retrieve the extension of the file from the array, assign it to the variable and then concatenate it with our new file name (which we´ll create in just a minute).
Type in the following on the next line:

$ext = $flext['extension'];

Now the variable called $ext stores the extension of the file.
Some of the files come with the extension in capital letters – so it may be a good idea to change the case of the extension to lower case.
To do this apply the strtolower() function to our last line of code so it reads:

$ext = strtolower($flext['extension']);

Now we need to populate the value from the text field and make it our file name plus add the extension to it - which we have already retrieved.
We need to make sure that our new file name is free of any spaces and we also may want it to be in the lower case.
To do this we will use the str_replace() and strtolower() functions.
We´ll pass three parameters to the str_replace() function:

  1. what we want to replace
  2. what the replacement will be
  3. and what should we apply the replacement to

On the next line type:

$file = str_replace(' ', '_', $_POST['frmname'].'.'.$ext);

We have created a new variable called $file and assigned the str_replace() function to it which replaces all empty spaces in the value populated from the frmname text field with the underscores. It also adds the dot (.) and the file extension to it using the $ext variable.

Now all we need to do is to change the case of the characters to lower.
To do it, type the following on the next line:

$file = strtolower($file);

We also need to change the line which reads:

move_uploaded_file($_FILES['frmfile']['tmp_name'],UPL_FLD.$_FILES['frmfile']['name']);

by replacing the UPL_FLD.$_FILES['frmfile']['name'] with UPL_FLD.$file.
The whole line should now read:

move_uploaded_file($_FILES['frmfile']['tmp_name'],UPL_FLD.$file);

You can now test the page in the browser – make sure you type in the full name in the text field.
Once you have uploaded file you should find the new file in the uploads folder with the name you typed into the text field – in lower case and with underscores (Fig. 03).

image illustrating a folder with the uploaded file
Fig. 03

In the next section I´m explaining how to allow only certain file types to be uploaded via our form.

<< Creating form | Restricting file types >>

 

Website Design Company | Small Business Web Site Design | Ecommerce Web Site Design | Web Design Tutorials
UK Content Management System | Database Design | Flash Animation | Web Designer Resources | Independent Web Hosting Review
© Freelance Web Designer - Sebastian Sulinski | Valid CSS | Valid XHTML