Find the best tutors and institutes for PHP

Find Best PHP Classes

Please select a Category.

Please select a Locality.

No matching category found.

No matching Locality found.

Outside India?

Search for topics

Be Carefull While Uploading A file To Server Using PHP

Praveen Tyagi
19/07/2017 0 0

With PHP, it is easy to upload files to the server.

However, with ease comes danger, so always be careful when allowing file uploads.

1. 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

2. Create The HTML Form:

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

<!DOCTYPE html>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">


Some rules to follow for the HTML form above:

  • Make sure that the form uses method="post"
  • The form also needs the following attribute: enctype="multipart/form-data". It specifies which content-type to use when submitting the form

Without the requirements above, the file upload will not work.

Other things to notice:

  • The type="file" attribute of the tag shows the input field as a file-select control, with a "Browse" button next to the input control

The form above sends data to a file called "upload.php", which we will create next.

3. Create The Upload File PHP Script:

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

PHP script explained:

  • $target_dir = "uploads/" - specifies the directory where the file is going to be placed.
  • $target_file specifies the path of the file to be uploaded.
  • $uploadOk=1 is not used yet (will be used later).
  • $imageFileType holds the file extension of the file.
  • Next, check if the image file is an actual image or a fake image.
0 Dislike
Follow 0

Please Enter a comment


Other Lessons for You

HTML Tables
Introduction to HTML Tables To use any data in a structured form you'll use table. By including tables in your webpage you can make it more structured and creative. To create tables in HTML you use...

Gaurav Rai | 28 Mar

1 0

What is M.S.Project ?
MICROSOFT PROJECT contains project work and project groups, schedules and finances.Microsoft Project permits its users to line realistic goals for project groups and customers by making schedules, distributing...

What Is Web Designing Or Composition?
What is web Designing or composition? Quickly, web designing implies arranging, creation and refreshing of websites. Web composition likewise includes data engineering, site structure, UI, route ergonomics,...

What is the difference between PHP and PYTHON
Let's start with PHP: - Ideal for standard web development- Easy integration with MySQL- $_GET and $_POST built into the language- C-Esque syntax Primarily used on an Apache server (Routinely, you...

Why Angularjs Is Highly Popular?
AngularJS is a full-fledged JavaScript framework. It has made the life of JavaScript developers very easy. It provides some amazing features which were not present in any of the single JS framework ever....

Ashish Soni | 23/05/2018

1 0

Find Best PHP Classes?

Find Now » is India's largest network of most trusted tutors and institutes. Over 25 lakh students rely on, to fulfill their learning requirements across 1,000+ categories. Using, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 6.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more