Welcome! Log In Create A New Profile

Advanced

Section 10 - Working With Files In PHP

Posted by 78183472 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Section 10 - Working With Files In PHP
July 24, 2013 07:30PM
Completed and understood!
It's a lot of fun to play with files and also very useful!! grinning smiley


Just a note of caution:
If you are using the "move on to next part" links at the bottom of the tutorial pages (on the CD), the link underneath page 2 and 3 takes you to section 13 (databases)!
Use the main contents page to go to the correct next section!

Re: Section 10 - Working With Files In PHP
July 24, 2013 08:10PM
Thought so.. can someone help
I cant connect to the database.
can't find the right php.ini file.

please help......
avatar
Mac
Re: Section 10 - Working With Files In PHP
July 25, 2013 09:01AM
Why the php.ini file?
Re: Section 10 - Working With Files In PHP
July 25, 2013 08:00PM
The PHP.ini was for to enabling the magic quotes.

I cannot connect to the database. I get this when I run my code "Database NOT Found"

$user_name = "root";
$password = "";
$database = "addressbook";
$server = "127.0.0.1";

$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {
$SQL = "INSERT INTO tb_address_book (First_Name, Surname, Address) VALUES ('bill', 'gates', 'Microsoft')";
$result = mysql_query($SQL);
mysql_close($db_handle);
print "Records added to the database";
}
else {
print "Database NOT Found ";
mysql_close($db_handle);
}
avatar
Mac
Re: Section 10 - Working With Files In PHP
July 26, 2013 08:17AM
Use the formatted code button on the editor to post PHP code.

See this on magic quotes: http://www.tizag.com/phpT/php-magic-quotes.php

add an or die function like here under

Language: PHP
$result = mysql_query($SQL) or die(mysql_error());

It will provide more information

If it says Unknown database 'addressbook' then perhaps check the spelling of the db.
Re: Section 10 - Working With Files In PHP
July 27, 2013 05:02PM
Hi,

This is a great lesson! Interesting reading many column arrays. Done, but will return to this section
to do further research on working with CSV files in PHP.
Re: Section 10 - Working With Files In PHP
August 01, 2013 10:33AM
Section Complete
Re: Section 10 - Working With Files In PHP
August 06, 2013 04:10PM
Regarding page two of section 10, did anyone else get the number "6143" printed at the end of their page when using readfile()? It's not in dictionary.txt and when I add file_get_contents() it goes away. My intuition tells me it's probably a character count. Anyone know for sure?
Re: Section 10 - Working With Files In PHP
August 07, 2013 03:55PM
@Brad

You are right, it is the character count. The reason for this is that readfile() actually prints out the content AND returns the length.
So as I understand this, in the following code :

Language: PHP
$file_to_read = "dictionary.txt"; print readfile($file_to_read);

the "readfile()" is actually printing out the content of the document,
while "print" is actually printing the content length of the file (that was returned by "readfile()"winking smiley

If you just use
Language: PHP
$file_to_read = "dictionary.txt"; readfile($file_to_read);

you'll see that the contents of the document is still printed!

I hope this makes sense!!!! grinning smiley
Re: Section 10 - Working With Files In PHP
August 08, 2013 12:32PM
@78183472

You've done well, great feedback.
Thanks so much smiling smiley
Re: Section 10 - Working With Files In PHP
August 10, 2013 05:29PM
Completed this section. Noticed that some of the "next page" links were not working correctly on my cd - not a problem.
Was wondering how to get a new text line when writing to the file - found \r\n using google - tested and it worked.
Can't wait to get to the mysql section, going to do the dates quickly and then start that one!
Re: Section 10 - Working With Files In PHP
August 12, 2013 11:19AM
Section 10 - Working With Files In PHP: Completed , looking forward to applying this knowledge. !
Re: Section 10 - Working With Files In PHP
August 13, 2013 02:10PM
Wait a minute... Am I missing something here? @78183472 when I use:
readfile() and also echo readfile() the results are the same to me - the contents of: "dictionary.txt" are printed on the screen. So I don't get the difference you've mentioned.
Re: Section 10 - Working With Files In PHP
August 13, 2013 08:53PM
Done! Thanks to 78183472 for the warning, I was a bit confused there for a minute until I read your comment.
I quite like the CSV files, had a bit of introducing in excel a while back, so it was nice to see how it works together.
Next..
Re: Section 10 - Working With Files In PHP
August 14, 2013 03:43PM
When using the fgetcsv() function; is it a must that one should include file size?
avatar
Mac
Re: Section 10 - Working With Files In PHP
August 14, 2013 04:03PM
Optional. Specifying it increases the speed, but not so that anyone will notice
Re: Section 10 - Working With Files In PHP
August 14, 2013 07:04PM
Re: Section 10 - Working With Files In PHP : Completed and understood.
avatar Re: Section 10 - Working With Files In PHP
August 14, 2013 10:38PM
I have finished chapter 10. My links were a incorrect too, but not a problem.
Re: Section 10 - Working With Files In PHP
August 16, 2013 11:49AM
Section 10 completed.

Useful summary: http://www.w3schools.com/php/php_file.asp

Warren
Re: Section 10 - Working With Files In PHP
August 18, 2013 10:28AM
Completed this section, but had the same issue as Tessa with some of the next page options jumping to Section 13. So completed some of those too smiling smiley I'm glad I thought to look back at the php.html file again to check why I was getting so confused.
Re: Section 10 - Working With Files In PHP
August 19, 2013 10:29PM
Completed section. Found it pretty straight forward.

Also experienced the incorrect page links, for a second there I was so worried! Could not understand how the tutorial can claim we have done steps 1 and 2 and then I can't recall any of it!! smile Anyway, luckily realised the mistake quickly so did not get too confused.

Thank you Warren for the summary on w3schools. Found it helpful and will probably refer back to that if I ever need a reminder.
Re: Section 10 - Working With Files In PHP
August 23, 2013 07:56PM
Section Complete smiling smiley
Re: Section 10 - Working With Files In PHP
August 25, 2013 01:03PM
Completed this section no difficulty experienced.
Sorry, only registered users may post in this forum.

Click here to login