UrbanPro
true

Learn PHP from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

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>
<html>
<body>

<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">
</form>

</body>
</html>
 

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

Submit

Other Lessons for You

A Guide To Become A Full-Stack Developer
Full-Stake Web Development, according to the Stack Overflow 2016 Developer Survey is the most popular developer occupation today. It’s no wonder then that there are dozens of online and in-person...

How to create your website with PHP?
PHP is a server-side programming language used to create web pages that are more interactive than their HTML counterparts. PHP also optimizes the speed of web pages since processing occurs on the server...

Core PHP v/s PHP Frameworks
PHP Frameworks: Framework is a piece of software by which one can develop large enterprise, robust & secure software applications. Frameworks provide straight forward flow and strict guidelines by...

Read Your Gmail Emails Using PHP and IMAP
Hello Friends ,Recently I have worked on drip email campaign project for that I need to read emails using PHP From the gmail inbox. Here is the simple example for Read emails from the gmail: To start...

Git Branching: Basic Branching And Merging
Basic Branching and Merging: Let’s go through a simple example of branching and merging with a workflow that you might use in the real world. You’ll follow these steps: Do work on...
X

Looking for PHP Classes?

The best tutors for PHP Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn PHP with the Best Tutors

The best Tutors for PHP Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. 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