[ad_1]
Facebook –
GitHub –
Google+ –
LinkedIn –
reddit –
Support –
thenewboston –
Twitter –
Beginner PHP Tutorial – 78 – File Handling: Reading a File
by
Tags:
Comments
43 responses to “Beginner PHP Tutorial – 78 – File Handling: Reading a File”
-
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 ", ";
}
}
}
} -
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
-
If you not understand it first watch 79 before this
-
Who else went to 79 and came back here?
-
All the pain he took to remove a trailing comma!
Programming is indeed funny! xD -
<?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> -
and if you want to add a dot at the end:
}elseif($count==$readin_count){
echo '. ';
} -
nice explanation alex sir !
-
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…
-
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.
-
can
this be used for comment purpose in the website? -
GUYS NO NEED TO USE FOREACH THERE IS A SINGLE LINE FUNCTION TO OUTPUT THE FILE CONTENTS :-
ECHO FILE_GET_CONTENTS( ' YOURFILENAME . TXT' ); -
THIS VIDEO IS LIL MESSED UP
-
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
-
cool tutorial bt my trim seems not to be working
{
echo trim($fname);
} -
clear concept .
-
Shouldn't you use httpentities?
-
well good job SIR !!!!
-
Watch 79 first..BUT HOLD ON A SECOND try make it yourself first, its piece of cake 🙂 here is mine!
***********************************************************************
<?phpIF(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> -
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.
-
/*
$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; -
OMG, maybe count++; must be before inner if.
-
<?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>
-
if($count != $fileCount)
{
echo ', ';
}That original code was perfectly fine, all he needed to do was change count to a 1
-
Thanks so much!
-
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"?
-
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> -
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? 🙂 🙂 🙂
-
for those people finding this video confusing…its an uploading error..first of all watch tutorial 79 and come back..Makes sense
-
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!
-
Haha love it!! Off by one error 😀
Great vid -
remove count++ and just do this
if(($count += 1) != $readin_count)
// add comma -
Very difficult to understand!
-
4:41 josh sup man … LOL!
-
are these out of whack? in the last video no such form was created.
-
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…
-
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
-
Before u watch this watch 79.. Videos just got mixed.. Watch 79 then 78.
-
7:37 "oh dear.." xD
-
nice tutorial , you've lean a lot if you stick in his lesson
-
Wha!? What the heck did I miss!? Oh. Videos are just mixed up.
-
I have no idea what just happened
-
that isnt good to say that
Leave a Reply