Diving into PHP

Diving into PHP

Mar 12th in Screencasts by Jeffrey Way

As some of you might know, I've been running a weekly video series on the ThemeForest Blog that teaches new developers exactly how to work with PHP. Once or twice a week, I release a new "episode" that builds upon the previous days. To expand our viewership, I've decided to release "Day 13" here. Be sure to subscribe to In the Woods to stay up to date on each new release.

Why do it this way? Because people don't always have the time to watch sixty minute videos. By posting short ten minute episodes, beginners can easily digest each lesson, rather than become overwhelmed with more information than their minds are able to consume. If you're new to PHP, and are not familiar with this series, I hope you'll subscribe and become a new viewer.

PG

Author: Jeffrey Way

Hi, I'm Jeff. I'm the editor of Nettuts+, and the Site Manager of Theme Forest. I spend too much time in front of the computer and find myself telling my fiance', "We'll go in 5 minutes!" far too often. I just can't go out to dinner while I'm still producing FireBug errors...drives me crazy. During my free time, I sporadically write articles for my own personal blog. If it will keep you in the good graces of the church, follow us on Twitter.

Day 13: Uploading Files

Don't worry; it's not required that you view the previous days first. Each new episode is a self contained tutorial. However, the concepts build upon what you've already learned!

In this lesson, you'll learn how to upload files, and how to use regular expressions to ensure that inappropriate file types aren't uploaded to your server.

Previous Screencasts

Diving into PHP
  1. Day 1: Installation
  2. Day 2: Variables
  3. Day 3: Passing Values From Page to Page
  4. Day 4: Multiple Variables and the “foreach” Statement
  5. Day 5: Refactoring, Arrays, and Functions - Oh My!
  6. Day 6: Including Files
  7. Day 7: Regular Expressions
  8. Day 8: Strings
  9. Day 9: First-Time Visitors
  10. Day 10: Getting Started With MySql
  11. Day 11: SQL Insert Statements
  12. Day 12: The File System
  • Subscribe to the NETTUTS RSS Feed for more daily web development tuts and articles.


Related Posts

Check out some more great tutorials and articles that you might like

Enjoy this Post?

Your vote will help us grow this site and provide even more awesomeness

Plus Members

Source Files, Bonus Tutorials and
More for $9 a month for all TUTS+
sites in one subscription.

Join Now

User Comments

( ADD YOURS )
  1. PG

    shedh March 12th

    When will the next part of User Registration and login be out?

    can’t wait for that one and i love the videos!

    ( Reply )
    1. PG

      lawrence77 April 4th

      Love this series! ;)

      ( Reply )
  2. PG

    David Singer March 12th

    http://us.php.net/pathinfo will get the file extension as well. Its easier than regular expressions.

    ( Reply )
  3. PG

    David Singer March 12th

    I use this in every project. Its just wraps pre tags around print_r. Saves a lot of time debugging though.

    /**
    * @brief Returns output of print_r enclosed in \c tags.
    * @return string The output of print_r enclosed in \c tags.
    * @param mixed $expression The expression to be printed.
    * @param bool $return[optional] Set to TRUE to return output, FALSE to print.
    */
    function pre_r($expression, $return = false) {
    if ($return) {
    return ” . print_r($expression, true) . ”;
    } else {
    echo ”;
    print_r($expression, false);
    echo ”;
    }
    }

    Now I can just type pre_r($array); and it will look pretty in the browser.

    ( Reply )
    1. PG

      Jorge Mudry March 12th

      Man, you just save me a lot of time in the future with that function =)

      Tks!!

      ( Reply )
      1. PG

        David Singer March 12th

        Your welcome. Here is another stupidly simple function thats a great time saver for debugging.

        /**
        * @brief Returns a HTML break.
        *
        * This function is usefull for quickly outputing breaks for testing.
        *
        * @param int $count[optional] The number of breaks to return.
        * @param bool $return[optional] Set to TRUE to return output, FALSE to print.
        * @return string HTML breaks. Will echo if \$ return is set to FALSE.
        */
        function br($count = 2, $return = false) {
        $count = (int) $count;
        if ($return) {
        return str_repeat(”, $count);
        } else {
        echo str_repeat(”, $count);
        }

        }

        Now you can do:
        echo $var1;
        br();
        echo $var2;

    2. PG

      shane sponagle March 12th

      Nice tips, thanks :)

      ( Reply )
    3. PG

      Anton Agestam March 18th

      I use something similar:

      function preint_r( $array )
      {
      ob_start();
      print_r($array);
      $ob=ob_get_contents();
      ob_end_clean();
      return “\n$ob\n”;
      }

      ( Reply )
      1. PG

        Anton Agestam March 18th

        Unfornataly my pre-tags were deleted, since they don’t use htmlentities() here ….

    4. PG

      kido March 24th

      maybe this is silly but why not use var_dump()?
      this function will warp output with tag, if you have “Syntax Highlighting mode” on in your php.ini, it will do a real magic for ya

      cheers

      ( Reply )
  4. PG

    David Singer March 12th

    I think line 51 (index.php) is wrong. It should be $path = $target . urlencode($file_name); otherwise the code will not correctly handle filenames with wired characters in them.

    ( Reply )
    1. PG

      David Singer March 12th

      One last thing before you call move_uploaded_file() you should replace some/all of the weirder characters with something safe like the underscore. Otherwise it is possible to construct an image name that passes the regex test but injects arbitrary html into the document.

      ( Reply )
  5. PG

    Shaun C. March 12th

    Pleeeease don’t stop the series, Jeff! I know there is so much more to learn in PHP, but these examples are invaluable and (to a person that’s new to PHP) very inspirational. Your screencasts have really opened my eyes to what all can be done, and you explain it so well.

    As long as you continue to have the time to do these, I know I’ll still be watching every episode!

    ( Reply )
  6. PG

    AliG March 12th

    Great tutorial,

    Any chance of some more OOP in PHP.

    ( Reply )
  7. PG

    BroOf March 12th

    Youre great! Looked for something like this! THANKS!

    ( Reply )
  8. PG

    Paul Davis March 12th

    I’m all for the Diving Into PHP Day 64 :P

    I suppose a re-brand could be good, Deep Sea Diving into PHP?

    ( Reply )
  9. PG

    Jeff Adams March 12th

    Yay my fav video series is back! And on Nettuts too – cross promotoin? LOL.

    I love the 10-15 min videos, plus as well, if theres something i dont understand i can always re-watch them!

    For future videos, i love seeing how PHP and MySQL interact, then perhaps throw in some jQuery and it’d be awesome.

    People should check out the jQuery series too, that’s REALLY handy for desingers that don’t program too well.

    Great job Jef – nice work as always!

    ( Reply )
  10. PG

    SX March 12th

    Great tutorial Jeffrey! That was a great introduction to file uploads. Very organized and clean. I would also mention that storing the image outside of the web root and giving the uploaded file a generated name with the uniqid(’photo’) function would also help make it more secure. That way the user will not know the file name. Thanks a lot. I loved the jQuery series and would love to see some more.

    BTW, those mistakes were not your fault. That editor you are using really sucks. It changed your code a couple of times which gave you those parse errors.

    ( Reply )
  11. PG

    Randy March 12th

    I am sure you get tired of doing what seems to be basic php, but I second Shaun that these are invaluable to those of us learning. I have read a few books on php and I can tell you that hearing and seeing you explain this makes it so much clearer.

    The best thing is that you are taking some time to talk about security. I do get tired sometimes of reading tutorials only to find “oh, but never use this on a site” lurking at the end of it. (I also realize you aren’t making any promises of “100%” security, however.)

    @David Thank for those tips. Seems like a good opportunity for you to write a tut here?

    Thanks!

    ( Reply )
  12. PG

    Matt Radel March 12th

    This has been a great series, and just what I needed. Thanks a ton!!!

    ( Reply )
  13. PG

    Kyle March 12th

    Great stuff Jeff! I would love the intermediate series.

    ( Reply )
  14. PG

    Nico March 12th

    Great Stuff dude, PHP is a realy nice Script speak, i love the code :)

    ( Reply )
  15. PG

    crysfel March 12th

    good job!!

    ( Reply )
  16. PG

    Meshach March 12th

    Jeffrey, you’ve got the best series I’ve ever seen.

    Absolutely awesome!!

    ( Reply )
  17. PG

    Jeffrey Way March 12th

    Thanks guys!

    ( Reply )
  18. PG

    Yoosuf March 12th

    ohh jeff

    same old day 12

    ( Reply )
  19. PG

    i386 March 12th

    Guys, could you please have a mirror for these screencasts? In youtube or maybe a direct link to download them? We really appreciate it. Thanks.

    ( Reply )
    1. PG

      Jeffrey Way March 12th

      You can download the videos from iTunes. Just visit the store and search for nettuts.

      ( Reply )
  20. PG

    Yosy March 12th

    Thank You Jeff,Will we have a screencast in this week?

    ( Reply )
  21. PG

    shane sponagle March 12th

    Another great tutorial. This is my favorite TV show :)

    ( Reply )
  22. PG

    Benjamin B. March 12th

    great, what is your editor please ?

    ( Reply )
    1. PG

      Jeffrey Way March 12th

      e text-editor

      ( Reply )
      1. PG

        Benjamin B. March 12th

        thanks.

  23. PG

    Ziggidy March 12th

    Ive been wanting to know how to do this for some time now.
    what a coincidence that it shows up on your site.
    by the way your tutorials are really great help.
    keep it going!

    ( Reply )
  24. PG

    Ziggidy March 12th

    oh yes one more thing.

    keep doing the php tutorials definitely.

    they are invaluable.

    ( Reply )
  25. Doing an awesome job as always Jeff, I think it would be a shame to end it at 15 but the thoughts of an intermediate series is very tempting. Either way thanks for the tutorials :D

    ( Reply )
    1. PG

      Jeffrey Way March 12th

      Well – it would basically transition into the intermediate series.

      ( Reply )
      1. PG

        Ziggidy March 12th

        I say go intermediate.

      2. PG

        Meshach March 13th

        I would too.

        I am seriously looking forward to OOP!

  26. PG

    Esteban March 12th

    I discover your tuts recently…what a happiness! ecerything is easy with you..and the small mistakes you do is also learning tool for me and for a lot of persons I am sure. You troubleshoot fast and you really have talent for providing training. Bref tu es trop bon! I am following your PHP series and JQuery.
    Steff I wanted to ask you somehting if you think is relevant for your series.
    I know it is not always relevant to do Stored procedures but I really learn them. So I will appreciate if ou can show us how to write for a stored procedure in MySQL and call it within a PHP string using mysqli. You have covered already the prepare statements, maybe would you do this extra effot to couple prepare statements and stored procedure.
    Thanks for everything
    Esteban

    ( Reply )
  27. PG

    Rene March 12th

    Hey thanks for the tut, any idea when are you gonna make a tut on password protected pages.

    PS: dont stop making php tuts, they’re great:

    ( Reply )
  28. PG

    Yoosuf March 12th

    Jeff i guess you guys are caching the content, yesterday in the same video i saw the File Handling, but today its about File Unloader

    ( Reply )
    1. PG

      Yoosuf March 12th

      all about file up loader

      ( Reply )
  29. PG

    S.M.Riyaz March 12th

    Hi,
    Thanks a lot for your great screen casts and tutorials. Can you please post the previous screen casts too in net tuts? I always get an 404 error in themeforest blog which I’ve already complained. And I heard people other then me are also having the same problem.
    I appreciate if you could post wordpress, PHP screen casts here in net tuts.

    Thanking you again.

    ( Reply )
  30. PG

    hellboy March 12th

    I really enjoyed these series! thanks

    ( Reply )
  31. PG

    Shane March 13th

    Great series! Many many thanks!

    ( Reply )
  32. PG

    Snorri - Css March 13th

    How awsome is this .. Keep it up :D
    this is just what i needed

    ( Reply )
  33. PG

    Angel March 13th

    your php tutorials are definitely invaluable, please keep them coming.

    ( Reply )
  34. PG

    şişko inek March 13th

    I can see no reason in this context to throw an exeception.

    1.) From a semantic view: Uploading a File is a highly security-related thing, where you should not start experimenting with unhandled behavior

    2.) As far as im know you should use Exeptions in a context if you are not sure about the actual result of some function behavior etc, in this case you have strict rules (i.e. has right filesize, filetype correct etc.)

    ( Reply )
    1. PG

      Tom March 14th

      I think it is much easier to work with try and catch.

      ( Reply )
  35. PG

    Ben Blogged March 13th

    Intermediate Series! – Jeffrey you are the man…

    ( Reply )
  36. PG

    laci March 14th

    Great series, i would love to see some more advanced stuff, also something about using php to send mails and a little bit of OOP.

    ( Reply )
  37. PG

    Tom March 14th

    You are inspiration, you makes me wanna work! Congrats! It is really interesting Jeff!

    I think there is an error, because if file size is more than 300kb, it shouldnt return “true” to isset($status)… but anyway it was, not uploading the file, but not showing the error neither. :/

    ( Reply )
  38. PG

    Felix March 15th

    Hi Jeff,
    did you know that

    if(isset($_POST['submit'])) {
    echo “something”;
    }

    does not work in Internet Explorer?

    ( Reply )
  39. PG

    Geoff March 15th

    Great set of tuts! Thanks for all the effort you have put into these.
    I really like the idea of an intermediate series. I would also like to see
    a series where you tie a variety of other languages together, similar
    to your jquery or back-end admin tuts. Thanks again.

    ( Reply )
  40. PG

    Kayla March 16th

    I’ve been trying to learn PHP for so long, and while some of this was review, I still learned so much. It also helped me think of PHP programming in a new way, and allowed me to be more confident in what I can do with it.

    I’d love if you continued the tutorials after day 15! An intermediate set would be ideal, you explain things so well!

    ( Reply )
  41. PG

    David March 16th

    does anybody know how it works to upload all files, not only jpgs???

    thx

    ( Reply )
  42. PG

    Jirka Fornous March 19th

    I would recommend you to use some of IDE with parse error detection – like Eclipse PDT is. It will save a lot of type-check cycles :)

    ( Reply )
  43. PG

    Ben McRae March 19th

    Hi Jeff, i spoke to you a little while ago via email, you were helping me out with OOP code in the Zip_Archive().

    I think everyone would love to an OOP screencast :) these tutorials are great!

    thanks alot!

    Ben

    ( Reply )
  44. PG

    Steve March 20th

    day 14 is awesome! Im starting to feel like im getting the hang of php :) Keep going past 15 days or or do intermediate level, ether way you cant stop php im hooked now :)

    ( Reply )
    1. PG

      ciprian March 23rd

      where is day 14 ?

      ( Reply )
  45. PG

    Scott Tolinski March 23rd

    Hi I’m trying to upload an mp3 and get it to play in a basic flash player. The audio uploads fine, however when it is clicked to play in the flash player, it just hangs and says buffering forever.

    You can try it out @

    http://www.toraprod.com/test/upload/index2.php

    This is the code for the flash part. The flash player works fine with audio files directly linked to it.

    if(isset($status)) {
    $path = $target . $file_name;
    echo “Congratulations. Hear your file.“;

    echo ”

    “;

    }
    ?>

    Thanks! for anyone who takes time to help. Thanks Jeffery for the shout out during that tut! It’s exactly what I wanted to cover.

    ( Reply )
  46. PG

    Scott Tolinski March 23rd

    /**
    if(isset($status)) {
    $path = $target . $file_name;
    echo “Congratulations. Hear your file.“;

    echo ”

    “;

    }
    ?>
    **/

    ( Reply )
  47. PG

    Ciprian March 23rd

    Where is day 14 STEVE :P ?

    ( Reply )
  48. PG

    Shibs March 23rd

    I think the idea of an intermediate series is great. I’m really anxious for more of these.

    ( Reply )
  49. PG

    Bastiaan March 26th

    Hey Jeff,

    Thanks for the lessons! I learn it way better this way, then from a book. It’s easier to understand this way, and if you understand this way, it’s a lot easier to read a book of you already know a couple of things.

    I was just wondering if you could do a lesson about mail()(or PHPMailer) and/or more mySQL. Especially login. INSERT and SELECT helped me a lot and i have much fun with it, but how can i fix a login page? I really hope you have the time and money to do a lot more episodes, it helps me(and not only me) very very much to understand PHP.

    Thank you very much!!

    Bastiaan(Holland)

    ( Reply )
  50. PG

    Sean Hood March 28th

    Hi Jeff,

    I’m really liking your screen casts in php, I’ve learnt loads so far and hope to learn a lot more, also i really like your jQuery screen casts too.

    Keep it up.

    ( Reply )
  51. PG

    Andrew Miesner March 29th

    Jeff!

    When will we see Day 14? I hope you haven’t died from exhaustion yet :P

    ( Reply )
  52. PG

    Steve March 30th

    Hi Jeff, yes i’m also hoping you do a day 14! This is great stuff.

    ( Reply )
  53. PG

    Bogdan March 30th

    I’m Working on a website with gallery for a friend and i didn’t know how to do the upload part.. this tutorial is very great i’ve read about ten twenty tutorials about file upload but no other tutorial was explained part by part like this one know i know what i have to do.. maybe in the next tutorial or i dunno do onther tutorial for multiple file upload and inserting the data into mysql for pictures.. or other stuff.. would be very nice and a great help for lot’s of ppl.

    ( Reply )
  54. PG

    Rick Bross April 1st

    I wanna see a Intermediate Series named, “A Firm Grip on PHP”.

    ( Reply )
  55. PG

    Keith April 2nd

    Nice! always good to go over php

    ( Reply )
  56. PG

    Josh April 8th

    When will the series continue? or will it?
    I enjoyed this.
    Maybe branch to another level.

    ( Reply )
  57. PG

    Benny April 16th

    This is so great. Thank you very much! And please, make this a 300 days Diving into PHP marathon!

    ( Reply )
  58. PG

    Kenny April 18th

    Is there a day 14? Or does this series continue on under a different name?

    I love your tutorials, and I’ve tried to learn PHP before, and have never understood it, but these tutorials really help me to understand the language better.

    I appreciate all the effort you put into these Jeff, and hope you continue to do them.

    ( Reply )
  59. PG

    samiul April 25th

    great man! keep it up

    ( Reply )
  60. PG

    JR April 29th

    Dude, I’d pay for these. Don’t stop.

    ( Reply )
  61. PG

    Joshua May 25th

    Sob sob sob… is day 13 the end? :( I want to learn more :(

    ( Reply )
  62. PG

    beneblack May 26th

    First of all: I love these tutorials. I’m watching them all, as I watched the awsome serie jquery for beginners.

    There is a little mistake in line 12 of the index.php

    $_FILES['file']['size'] > 300000) does not work. You see it in the tutorial on minute 17, where the file is not uploaded and the message “congrats…” ist thrown out.

    If the file is larger than the size defined in line 42 :
    input type=”hidden” name=”MAX_FILE_SIZE” value=”300000″
    the $_FILES-array-values of ‘error’ and ’size’ are changing. ‘error’ becomes ‘2′ and (that is the key:) ’size’ bekomes ‘0′.

    Something like $_FILES['file']['size'] == ‘0′ && $_FILES['file']['error'] ==’2′ should work.

    @jeff Please go on making these tutorials. I love them all. Didn’t find anything helpful like this in german.

    beneblack

    ( Reply )
  63. PG

    Saad May 26th

    more php tutorials like this please.

    ( Reply )
  64. PG

    nilaf June 30th

    best video tutorial for a beginner like me.Thanks Jeffrey Way.i want to know how to create a user image crop application after upload.waiting for that

    ( Reply )
  65. PG

    Jason July 29th

    Are there not going to be any more of these?

    ( Reply )
  66. PG

    jbcarey August 22nd

    Can’t keep asking it…. please keep this going, either here or “there”

    ( Reply )
  67. PG

    Alex November 1st

    Dont stop! keep going all the way to 100 videos. there is so much more to learn with MySQL etc…

    We love it, keep it coming!

    ( Reply )
  1. Arrow
    Gravatar

    Your Name
    November 1st