New Feature: Playlist Center! Pick a topic and let our playlists guide the way—like a learning mixtape.

Start learning with our library of video tutorials taught by experts. Get started

Validating and Processing Forms with JavaScript and PHP
Illustration by

Understanding file uploads


From:

Validating and Processing Forms with JavaScript and PHP

with Ray Villalobos

Video: Understanding file uploads

So let's take a minute to talk about how file uploads work between forms and PHP. In addition to capturing regular text data, a form can also send a file into a folder on your server using PHP. In order for this to work, the form has to use the multipart/form-data enctype. This is part of your form tag. Because get methods are limited to the size that's allowed when sending data through a URL, you should use the post method if you're uploading files from a form. You can include the accept attribute inside the input field to limit the MIME type of the file you're uploading. If you have any questions about this make sure you check out the movie on Limiting Uploads by MIME Types.
Expand all | Collapse all
  1. 3m 36s
    1. Welcome
      1m 9s
    2. What you should know
      1m 2s
    3. Using the exercise files
      1m 25s
  2. 12m 50s
    1. Understanding forms
      2m 2s
    2. Working with form fields
      7m 4s
    3. Using the form tag
      3m 44s
  3. 19m 23s
    1. Using input validation
      2m 10s
    2. Adding required fields and placeholders
      1m 31s
    3. Constraining numeric and date fields
      2m 32s
    4. Accepting multiple entries
      1m 41s
    5. Limiting uploads by MIME types
      2m 35s
    6. Assisting text input with a datalist
      1m 55s
    7. Constraining with regular expression patterns
      6m 59s
  4. 42m 13s
    1. Accessing forms
      3m 57s
    2. Looking up form elements
      3m 35s
    3. Handling focus changes
      2m 47s
    4. Detecting the onchange event
      4m 31s
    5. Using the selectedIndex property
      2m 30s
    6. Dynamic validation with regular expressions
      7m 0s
    7. Creating a generic input validation function
      4m 31s
    8. Validating in older browsers with Modernizr
      7m 32s
    9. Interrupting form submission with onsubmit
      5m 50s
  5. 15m 20s
    1. Understanding jQuery
      3m 47s
    2. Validating on submit with jQuery
      3m 45s
    3. Building interactive jQuery validation
      2m 34s
    4. Using the jQuery Validation plugin
      5m 14s
  6. 32m 57s
    1. Communicating with PHP servers
      2m 27s
    2. Retrieving data from superglobals
      8m 18s
    3. Using server-side validation
      4m 59s
    4. Adding in-page validation
      5m 22s
    5. Mirroring input data back to the user
      7m 46s
    6. Sanitizing form input
      4m 5s
  7. 43m 29s
    1. Mailing form data
      8m 28s
    2. Understanding file uploads
      3m 1s
    3. Uploading files
      9m 20s
    4. Processing form data with AJAX
      8m 14s
    5. Preparing your database
      5m 50s
    6. Pushing data
      8m 36s
  8. 1m 17s
    1. Next steps
      1m 17s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
please wait ...
Validating and Processing Forms with JavaScript and PHP
2h 51m Intermediate Apr 23, 2013

Viewers: in countries Watching now:

Validating web forms is a critical skill for any web developer, ensuring that the data that's submitted is complete, accurate, and nonmalicious before it's sent off to the server. Join author Ray Villalobos in this course as he shows how to validate input from site visitors with HTML5, JavaScript, and jQuery and then process the data with PHP. Plus, learn how to email form data and save it in a MySQL database so that it's ready for other applications.

Topics include:
  • Understanding forms
  • Adding required fields and placeholders
  • Accepting multiple entries
  • Limiting uploads
  • Handling focus changes
  • Validating with regular expressions
  • Working with older browsers
  • Building jQuery validation
  • Using server-side validation
  • Sanitizing form input
  • Uploading files
  • Sending form data to a database
Subjects:
Developer Web Web Design Databases Web Development
Software:
JavaScript PHP
Author:
Ray Villalobos

Understanding file uploads

So let's take a minute to talk about how file uploads work between forms and PHP. In addition to capturing regular text data, a form can also send a file into a folder on your server using PHP. In order for this to work, the form has to use the multipart/form-data enctype. This is part of your form tag. Because get methods are limited to the size that's allowed when sending data through a URL, you should use the post method if you're uploading files from a form. You can include the accept attribute inside the input field to limit the MIME type of the file you're uploading. If you have any questions about this make sure you check out the movie on Limiting Uploads by MIME Types.

When uploading files, you can use an optional hidden field with the name of Max File Size that PHP commands will try to recognize. So for example this input field tells PHP that this file should be no bigger than about 30K. One note, this hidden input field has to be placed before the file input field. Once a form is submitted, PHP stores information about the uploaded file in a special files associative array. You can use this superglobal in your script to get information about the file.

You can read more about this on this page in the PHP documentation. Here's some different variables that you can get and what they do. So let's go over those. You can read the original name of the array using the user file name field. If the browser provides it, you can also read the MIME type of the file by reading the user file type value. The user file size value holds the size in bytes of the uploaded documents. PHP is going to place the uploaded documents in a temporary place on the server.

This variable, user file temp name, has the name of that location. What you'll end up doing, is moving the file to a more permanent location later. If the server is unable to upload the file for any reason, you can check the error types on this page. After you check, validate and sanitize file info using the file's superglobal. The file has to be moved, and you can do so with the move_uploaded_file function. You can read more about this function in this page of the documentation. It's really pretty simple.

The function makes sure that, if the file was uploaded properly, it moves the file to a permanent position on your server. This position must have the right upload permissions, which you have to set manually. If the file upload and movement were successful, the function returns true. So you usually check this function with an if statement. It's really important to give your filenames a unique name, because this function will simply overwrite any files at the destination with the same name, and it doesn't give you a warning. So one of the reasons I love PHP is its ability to make complicated tasks really simple.

Working with files is a good example. It takes care of most of the heavy lifting while providing easy functions to get work done. In the next movie, we'll add this functionality to our form.

There are currently no FAQs about Validating and Processing Forms with JavaScript and PHP.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Validating and Processing Forms with JavaScript and PHP.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Notes cannot be added for locked videos.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.