Beginner PHP Tutorial – 78 – File Handling: Reading a File

[ad_1]
Facebook –
GitHub –
Google+ –
LinkedIn –
reddit –
Support –
thenewboston –
Twitter –


Posted

in

by

Tags:

Comments

43 responses to “Beginner PHP Tutorial – 78 – File Handling: Reading a File”

  1. DrHanna Avatar

    we could use end() function alot easier and fast 🙂 rather than using count etc…

    if (isset($_POST['name'])) {
    $name = $_POST['name'];
    if (empty($name)) {
    echo 'make a name';
    } else {
    $fileHandle = fopen('test.txt', 'a');
    fwrite($fileHandle, $name . "n");
    fclose($fileHandle);
    $fileArr = file('test.txt');
    foreach ($fileArr as $file) {
    echo trim($file);
    if ($file !== end($fileArr)) {
    echo ", ";
    }
    }
    }
    }

  2. Loo Gim Liang Avatar

    i face a problem where when i direct till the last folder ,it does not show the file i have in the folder but open the file straight

  3. Harsath Mark Zuckerberg Avatar

    If you not understand it first watch 79 before this

  4. VisualQuizLIVE Avatar

    Who else went to 79 and came back here?

  5. Anuj Jain Avatar

    All the pain he took to remove a trailing comma!
    Programming is indeed funny! xD

  6. Abdul Moiz Avatar

    <?php
    if (isset($_POST['names'])){
    $names= $_POST['names'];
    if (!empty($names)){
    $handle = fopen('namelist.txt', 'a');
    fwrite($handle, $names);
    fclose($handle);
    $handle= fopen('namelist.txt', 'r');
    $filesize = filesize('namelist.txt');
    $contents = fread($handle, $filesize);
    echo "FILE SIZE IS: $filesize";
    echo "<pre>$contents</pre>";
    fclose($handle);
    }

    }
    ?>
    <form action="test1.php" method="post">
    Name:
    <br><input type="text" name="names"><br><br>
    <input type="submit" value="submit">
    </form>

  7. Nebbo .o Avatar

    and if you want to add a dot at the end:

    }elseif($count==$readin_count){
    echo '. ';
    }

  8. Deep Gagan Avatar

    nice explanation alex sir !

  9. MIKAL PALMANTIER Avatar

    What does your form say on the bottom? This tutorial does not work for me because I cannot see all of your code to understand how to do what you're doing…

  10. Richard Genck Avatar

    These are GREAT!! I'm sharing this with my friends, you might want to rename this and the next video though, this one should probably be 79, and 79 should be 78.

  11. melan shrestha Avatar

    can
    this be used for comment purpose in the website?

  12. Kalyani ‘s Kitchen Avatar

    GUYS NO NEED TO USE FOREACH THERE IS A SINGLE LINE FUNCTION TO OUTPUT THE FILE CONTENTS :-
    ECHO FILE_GET_CONTENTS( ' YOURFILENAME . TXT' );

  13. sr4u Avatar

    THIS VIDEO IS LIL MESSED UP

  14. Jasbir Singh Avatar

    Your videos are great man. I did php in the college But my professor taught nothing. Only because of you I am good with PHP

  15. moses njeri Avatar

    cool tutorial bt my trim seems not to be working
    {
    echo trim($fname);
    }

  16. Gabriel Armendariz Villarreal Avatar

    Shouldn't you use httpentities?

  17. LuqMaN AFridi Avatar

    well good job SIR !!!!

  18. Ankaboot Avatar

    Watch 79 first..BUT HOLD ON A SECOND try make it yourself first, its piece of cake 🙂 here is mine!

    ***********************************************************************
    <?php

    IF(isset($_POST['name'])&&isset($_POST['email'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    IF(!empty($name)&&!empty($email)){
    $handle = fopen('form.txt','w');
    fwrite($handle,'Your name is '.$name.' and your email is '.$email);
    echo '<b>Your name and email stored in our database';
    }else{
    echo '<i>Please fill all the boxes</i>';
    }
    }

    ?>

    <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
    Name:<br>
    <input type="text" name="name" value="" /><br />
    Email:<br />
    <input type="email" name="email" value="" />
    <br />
    <input type="submit" value="submit" />
    </form>

  19. McGavel1 Avatar

    Awesome man, thanks! I'm trying to make an online poker hand reviewer from a downloaded hand history text file and this is just what I needed. Gonna check out some other videos too. Peace and good luck.

  20. Andy unknown Avatar

    /*
    $readin_count=count($readin);
    $count=1;
    foreach($readin as $fname){
    echo trim($fname);
    if ($count<$readin_count){
    echo ', ';
    }
    $count++;
    }
    */
    //better:
    $string=implode(', ',$readin);
    echo $string;

  21. Pucvirok Avatar

    OMG, maybe count++; must be before inner if.

  22. Jeremy Chen Avatar

    <?php

    if (isset($_POST['name'])) {
        $name = $_POST['name'];
        if (!empty($name)) {

            $handle = fopen('names.txt', 'a');
            fwrite($handle, $name . PHP_EOL);
            fclose($handle);

            echo "Current names in file: ";

            $count = 1;
            $readin = file('names.txt');
            $readin_count = count($readin);
            foreach ($readin as $fname) {
                echo trim($fname);
                if($count < $readin_count){
                    echo ", ";
                }
                $count++;
            }

        } else {
            echo "Please type a name";
        }
    }
    ?>

    <form action="file.php" method="POST">

        Name:<br>
        <input type="text" name="name"><br><br>
        <input type="submit" value="Submit">

    </form>

  23. Ciaran Whyte Avatar

    if($count != $fileCount)
    {
            echo ', ';
    }

    That original code was perfectly fine, all he needed to do was change count to a 1

  24. Brax1982 Avatar

    How about not counting at all and instead using a string variable "s" on which you concatenate everything in the loop, then once the loop is done, remove the excess comma and maybe whitespace, then echo "s"?

  25. V. Peters Avatar

    watch 79 first.

    <?php

    if (isset($_POST['name'])) {
        $name = $_POST['name'];
        if (!empty($name)) {
            
            $handle = fopen('names.txt', 'a');
            fwrite($handle, $name."n");
            
            fclose($handle);
            
            echo 'Current names in file: ';
            
            $count = 0;
            $readin = file('names.txt');
            $readin_count = count($reading);
            
            foreach($readin as $fname) {
                echo trim(fname);
                if ($count<$readin_count) {
                    echo ', ';
                }
                $count++;
            }
        } else {
            echo 'Please type a name.';
        }
    }

    ?>
    <form action="les78.php" method="POST">
    Name: <br>
    <input type="text" name="name"><br><br>
    <input type="submit" value="Submit">
    </form>

  26. V. Peters Avatar

    whuhahaha i tried to scroll the scrollbar of your video to look at your code below… am i the only one trying that by accident or do you people recognise this? 🙂 🙂 🙂

  27. Bill Gates Avatar

    for those people finding this video confusing…its an uploading error..first of all watch tutorial 79 and come back..Makes sense

  28. Anthony Rosamilia Avatar

    I find one of the main issues in learning multiple languages is knowing when a standard function creates an array of data or another form of data. It makes you have to memorize so much!

  29. Jesse Lisser Avatar

    Haha love it!! Off by one error 😀
    Great vid

  30. Vu L. Avatar

    remove count++ and just do this

    if(($count += 1) != $readin_count)
       // add comma

  31. Tuan Anh Ha Avatar

    Very difficult to understand!

  32. Gabriel Badiola Avatar

    4:41 josh sup man … LOL!

  33. Rob Hawkins Avatar

    are these out of whack? in the last video no such form was created.

  34. Anthony Vipond Avatar

    Keep in mind, file() does not work for large files as it reads it all into memory. Imagine using file() on 1 GB log files…

  35. Balaclavaz Avatar

    When I echo out I get the word "Array" before the first name, which is replaced by a "1" when I do $count = 1.  I don't know what I'm doing wrong to get that.  The $count business seems a long winded way of removing a final comma

  36. John Jacob Avatar

    Before u watch this watch 79.. Videos just got mixed.. Watch 79 then 78.

  37. Tirador Philippinez Avatar

    nice tutorial , you've lean a lot if you stick in his lesson

  38. Cj Vaans Avatar

    Wha!? What the heck did I miss!? Oh. Videos are just mixed up.

  39. Blackbeltwhitehat Avatar

    I have no idea what just happened

  40. Lauris Skraucis Avatar

    that isnt good to say that

Leave a Reply

Your email address will not be published. Required fields are marked *