Welcome! Log In Create A New Profile

Advanced

Section 10 - Working with Files

Posted by 77695976gillon 
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
July 08, 2012 10:31PM
I have worked through this, it is a good idea to re look at while loops and explode() function before doing the section. Will make it easier to understand. Also if you are having problems writing to the text file check folder permissions, id not set to read/write you will get an error smiling smiley
Re: Section 10 - Working with Files
July 10, 2012 10:07PM
Completed. Found some of the navigation links are wrong. (php10p2 & php10p3 navigation links send you to section 13).
Anonymous User
Re: Section 10 - Working with Files
July 23, 2012 07:20PM
Done!
avatar Re: Section 10 - Working with Files
July 25, 2012 10:38PM
Language: PHP
<?php   $fhandle = fopen("testFile.txt", "a"); $fcontents = "did it change?";   file_put_contents($fhandle, $fcontents); fclose($fhandle); print "file created and written to";   ?>

so this is my script, the file is already created. If I use fwrite() it works fine, if I run the script above it gives me the error:
"Warning: file_put_contents() expects parameter 1 to be a valid path, resource given in C:\Program Files\EasyPHP-12.0\www\excersises\TestwriteToFile.php on line 6"

google doesn't seem to have an answer. I think its something to do with the handle but I don't know what exactly, anyone know what I'm doing wrong?
avatar
Mac
Re: Section 10 - Working with Files
July 26, 2012 06:54AM
The error says it - invalid path of the handle, therefore is testFile.txt in the same directory as the TestWriteToFile.php?
avatar Re: Section 10 - Working with Files
July 26, 2012 02:58PM
Yes it is Mac. What would the error be if I didnt have php5? probably a syntax error? I've reloaded everything onto the testing server again but still does the same.
avatar Re: Section 10 - Working with Files
July 27, 2012 08:01PM
Alrighty, I dont know if anyone else had the same issue. I finaly found the answer, which also worked with my script. in the previous script the $fhandle had a value of fopen(...), apparently the file_put_content(param1,param2,optional param) must have param1 specify the file itself and not the handle. So I changed my script to:
Language: PHP
<?php   $fhandle = "testFile.txt"; //file to be edited $fcontents = "Lets hope it works"; //contents to be added   $path = realpath("testFile.txt"); //test text file path echo $path."<br>";   $file = realpath("TestwriteToFile.php"); //test php page path echo $file."<br>";   file_put_contents($fhandle, $fcontents); echo "file created and written to";   ?>

and it worked. param3 then should specify FILE_USE_INCLUDE_PATH, FILE_APPEND or LOCK_EX. if param3 is not specified then everything is erased and only the new content is placed in the file.

Although it worked, is this correct Mac?
avatar
Mac
Re: Section 10 - Working with Files
July 30, 2012 07:17AM
No, that is not it. It would make the script not very useful, because you cannot always "hard code" the file name in, e.g. if you have a form that uploads the file, you assign it a handle to manage the name of the file.

Difficult to say because each server set-up is different e.g if xampp installed under program files on win 7 (it should be under C: ) then there can be permission problems. Not that this is your issue. Your first code runs fine with me.
Re: Section 10 - Working with Files
August 06, 2012 05:32PM
I tried to open a pdf file, well it did open the file but the contents were converted into numbers and some funny characters...is there a function to handle pdf files?
Re: Section 10 - Working with Files
August 07, 2012 02:57AM
Some pretty cool stuff, saves one time, considering how easy it is to create arrays using the explode function and printing it out on separate lines. Also the ability to open and create documents on the fly.
avatar
Mac
Re: Section 10 - Working with Files
August 07, 2012 07:24AM
77698428Sandra Wrote:
-------------------------------------------------------
> I tried to open a pdf file, well it did open the
> file but the contents were converted into numbers
> and some funny characters...is there a function to
> handle pdf files?

Yes http://www.google.co.za/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CFkQFjAA&url=http%3A%2F%2Fphp.net%2Fmanual%2Fen%2Fbook.pdf.php&ei=uKQgUNOKE9OzhAeR2oDIBg&usg=AFQjCNGa4NfKDtD5Tfdio653g-_aSbjhuA
Re: Section 10 - Working with Files
August 07, 2012 07:17PM
Some PHP Filesystem Functions that you might need are available here File System Functions
Re: Section 10 - Working with Files
August 08, 2012 11:28AM
Done with PHP 10 & 11.
Thanks Mac, really helped a lot...i can be dumb eye rolling smiley at times i mean the internet has all the answers we looking for, should've simply googled the functions...
Re: Section 10 - Working with Files
September 29, 2012 04:05PM
Completed PHP Ten. Was handy to re look at while loops and explode() function before doing the section.
Sorry, only registered users may post in this forum.

Click here to login