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

Basics about the PHP
Basics: Father of PHP Rasmus Lerdorf. Developed in 1994. Existing in 1999 PHP stands for: PHP: Hypertext Preprocessor. PHP is an Open Source Software (OSS) free to download and Use. PHP is...

Hi,
To learn online is really convenient for both the sides but still trust is risk factor until and unless you don't know the teaching style of your tutor.so I will give you small free session to decide my...

HTML5 Style Guide and Coding Conventions Part 1
HTML Coding Conventions Web developers are often uncertain about the coding style and syntax to use in HTML. Between 2000 and 2010, many web developers converted from HTML to XHTML. With XHTML, developers...

What is Type Juggle in PHP ?
Type Juggling means dealing with a variable type. PHP does not require explicit type definition in variable declaration. A variable's type is determined by the context in which the variable is used. Example,...

User Authenication In PHP: Some Advanced echniques
User Authentication in CodeIgniter: Goals Security: We want our login system to be secure, we want to do everything we can to stop people’s accounts being hacked into. Tracking: We’d like...
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