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

Kick start your IT career with User Interface development
If you are planning to start your career into IT, you can begin as User Interface developer. No previous IT background or programming experience needed. Below are some of the responsibilties of User...

Allow your brain to absorb new stuff
Are you interested in finding ways to learn new things faster? Do you want to become a more effective and efficient learner? If you are like many students, your time is limited so it is important to get...

Software Development Training In Jaipur
Satyam Web Solution provides website designing &development and software designing &development training in Jaipur for various stream’s students. MCA 6 month Industrial Training/Internship B....

Tips Web Developers and Designers
Get Educated Build Up Your Font Library Use Dummy Tools Learn Online Pimp Out Photoshop Quickly Generate Complex CSS Code Visually with a CSS Generator Download Free...

PHP
It is the base for many of the ecommerce websites. Many of the CMS develpoped in PHP platform.If programming cant done inititally try to think for logics and develop writing algorithms. Any doubts divyaravindiran@gmail.com.
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