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

What Is PHP?
PHP stands for PHP: Hypertext Preprocessor PHP is a widely-used, open source scripting language PHP scripts are executed on the server PHP is free to download and use PHP files can contain text,...

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

Migration from other languages to Salesforce
Anyone can easily migrate from other language to Salesforce. People must have skills to understand business logic.

Hiding .php extension Using HTACESS File
If you wish to hide.php extension from your website, then create a .htaccess file in your project root and paste the following code : RewriteEngine On RewriteCond %{THE_REQUEST} ^{3,}\s/(.+)\.php*...

HTML History by Years & Version
Since the early days of the World Wide Web, there have been many versions of HTML:Year Version1989 Tim Berners-Lee invented www1991 Tim Berners-Lee invented HTML1993 Dave Raggett drafted...
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