Diving into PHP
videos

Diving into PHP

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.

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.


Tags: Videos
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://www.shedh.com shedh

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

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

    • http://laranzjoe.blogspot.com lawrence77

      Love this series! ;)

  • http://ramaboo.com/ David Singer

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

  • http://ramaboo.com/ David Singer

    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.

    • Jorge Mudry

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

      Tks!!

      • http://ramaboo.com/ David Singer

        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;

    • http://modxdeveloper.com shane sponagle

      Nice tips, thanks :)

    • http://www.antonagestam.se/ Anton Agestam

      I use something similar:

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

      • http://www.antonagestam.se/ Anton Agestam

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

    • http://koniec.name kido

      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

  • http://ramaboo.com/ David Singer

    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.

    • http://ramaboo.com/ David Singer

      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.

  • http://www.blizzarddesign.com/ Shaun C.

    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!

  • AliG

    Great tutorial,

    Any chance of some more OOP in PHP.

  • http://www.broof.de BroOf

    Youre great! Looked for something like this! THANKS!

  • http://iamoneman.com Paul Davis

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

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

  • http://www.jeffadams.co.uk Jeff Adams

    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!

  • SX

    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.

  • http://www.prop-14.com Randy

    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!

  • http://matt-radel.com Matt Radel

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

  • Kyle

    Great stuff Jeff! I would love the intermediate series.

  • http://www.cofilew.de Nico

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

  • http://www.quizzpot.com crysfel

    good job!!

  • http://zencarttuts.com Meshach

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

    Absolutely awesome!!

  • http://themeforest.net/user/JeffreyWay Jeffrey Way
    Author

    Thanks guys!

  • http://eyoosuf.blogspot.com/ Yoosuf

    ohh jeff

    same old day 12

  • Pingback: In the Woods - Diving into PHP: Day 13

  • i386

    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.

    • http://www.nettuts.com Jeffrey Way

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

  • Yosy

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

  • http://modxdeveloper.com shane sponagle

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

  • Benjamin B.

    great, what is your editor please ?

    • http://www.nettuts.com Jeffrey Way

      e text-editor

      • Benjamin B.

        thanks.

  • Ziggidy

    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!

  • Ziggidy

    oh yes one more thing.

    keep doing the php tutorials definitely.

    they are invaluable.

  • http://www.websheffield.com Ed Baxter (Web Sheffield)

    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

    • http://themeforest.net/user/JeffreyWay Jeffrey Way
      Author

      Well – it would basically transition into the intermediate series.

      • Ziggidy

        I say go intermediate.

      • http://zencarttuts.com Meshach

        I would too.

        I am seriously looking forward to OOP!

      • http://rickbrossgraphics.com/ Rick Bross

        intermediate please! Id love to learn some log-in/user stuff.

  • Esteban

    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

  • http://scyberspace.com Rene

    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:

  • http://eyoosuf.blogspot.com/ Yoosuf

    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

    • http://eyoosuf.blogspot.com/ Yoosuf

      all about file up loader

  • http://www.smriyaz.com S.M.Riyaz

    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.

  • hellboy

    I really enjoyed these series! thanks

  • http://www.freshclickmedia.com Shane

    Great series! Many many thanks!

  • Snorri – Css

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

  • Pingback: JeremiahTolbert.com » Blog Archive » links for 2009-03-13

  • http://www.dataviking.com Angel

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

  • Pingback: Best of the Links #2

  • şişko inek

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

    • http://www.tomasroggero.com.ar Tom

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

  • http://www.benblogged.com Ben Blogged

    Intermediate Series! – Jeffrey you are the man…

  • http://www.lacisoft.com/blog laci

    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.

  • http://www.tomasroggero.com.ar Tom

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

  • Felix

    Hi Jeff,
    did you know that

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

    does not work in Internet Explorer?

  • Geoff

    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.

  • Pingback: NETTUTS.com: Diving into PHP | pcsourcenetwork.com

  • http://webitect.net Kayla

    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!

  • http://www.davidtoran.at David

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

    thx

  • Jirka Fornous

    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 :)

  • Ben McRae

    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

  • Pingback: Diving into PHP: Day 13 | CgBaran Tuts

  • Steve

    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 :)

    • http://www.homeamrket.ro ciprian

      where is day 14 ?

  • http://ScottTolinski.com Scott Tolinski

    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.