Welcome! Log In Create A New Profile

Advanced

PHP 10 - Working with files in PHP

Posted by Gaia77490614 
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
PHP 10 - Working with files in PHP
February 15, 2012 04:07PM
I have completed this section
avatar Re: PHP 10 - Working with files in PHP
February 17, 2012 10:10AM
Section Completed.
Re: PHP 10 - Working with files in PHP
February 22, 2012 08:46PM
I have completed this section.
Re: PHP 10 - Working with files in PHP
February 25, 2012 04:18PM
Completed
avatar Re: PHP 10 - Working with files in PHP
February 25, 2012 10:05PM
Completed. Very cool.

While browsing the php docs online I came across this note:
http://php.net/manual/en/function.list.php

It says that
Quote

it is strongly recommended that you always use the 'b' flag when opening files with fopen().
and
Quote

If you do not specify the 'b' flag when working with binary files, you may experience strange problems with your data, including broken image files and strange problems with \r\n characters.

Is using the 'b' flag considered standard practice when opening files in php?

Thanks smiling smiley
Re: PHP 10 - Working with files in PHP
February 27, 2012 12:12AM
I have completed this section.
avatar
Mac
Re: PHP 10 - Working with files in PHP
February 27, 2012 07:35AM
mm77509382 Wrote:

> Is using the 'b' flag considered standard practice
> when opening files in php?
>
> Thanks smiling smiley

That is just a recoomendation, so it is not standard practivce smiling smiley
Re: PHP 10 - Working with files in PHP
February 29, 2012 11:25AM
I can't seem to get rid of this error:

Notice: Undefined offset: 1 in C:\xampp\htdocs\php_sandbox\Course\workingwithfiles.php on line 27

and my code looks like this:

Language: PHP
<?php $fileArray = fopen("dictionary.txt" , "rb");   while (!feof($fileArray)) { $arrayLine = fgets($fileArray, 1024); $parts = explode(';=';, $arrayLine);   echo $parts[0] . $parts[1] . "<br />"; } fclose($fileArray); ?>

Even if I copy and paste their code exactly I get this error.
Can someone help me please?
Re: PHP 10 - Working with files in PHP
February 29, 2012 07:02PM
I am not responding to the last question but coming up with new one. This is an example from our manual, I can't get this right, when I run this script it's printing something like this without the comas not sure where am going wrong:

Widget1blue£10
Widget2red£12
Widget3green£14
Widget4black£16
Widget5white£18

Language: PHP
<?php $fileArray = fopen("dictionary.txt" , "rb");   while (!feof($fileArray)) { $arrayLine = fgets($fileArray, 1024); $parts = explode(';=';, $arrayLine);   echo $parts[0] . $parts[1] . "<br />"; } fclose($fileArray); ?>
Re: PHP 10 - Working with files in PHP
February 29, 2012 09:57PM
Complet!bt stil practicing using "b" method
avatar
Mac
Re: PHP 10 - Working with files in PHP
March 01, 2012 07:08AM
77471466_Alwyn Wrote:
-------------------------------------------------------
> I can't seem to get rid of this error:
>
> Notice: Undefined offset: 1 in

Mmmm. Nothing wrong with the code. Probably with the dictionary.txt file.
avatar
Mac
Re: PHP 10 - Working with files in PHP
March 01, 2012 07:15AM
Fulu-72924640 Wrote:
-------------------------------------------------------
> I am not responding to the last question but
> coming up with new one. This is an example from
> our manual, I can't get this right, when I run
> this script it's printing something like this
> without the comas not sure where am going wrong:
>

I cannot replicate your problem - if there are commas in dictionary.txt then it must show. You probably saved the .csv as a text file.
Re: PHP 10 - Working with files in PHP
March 01, 2012 09:12AM
mac Wrote:
-------------------------------------------------------
> 77471466_Alwyn Wrote:
> --------------------------------------------------
> -----
> > I can't seem to get rid of this error:
> >
> > Notice: Undefined offset: 1 in
>
> Mmmm. Nothing wrong with the code. Probably with
> the dictionary.txt file.

Thanks Mac
Re: PHP 10 - Working with files in PHP
March 01, 2012 02:17PM
Thanx, I've seen where I went wrong.
avatar
Mac
Re: PHP 10 - Working with files in PHP
March 02, 2012 08:20AM
Care to tell us where you went wrong for future reference?
Re: PHP 10 - Working with files in PHP
March 02, 2012 01:39PM
Done
Re: PHP 10 - Working with files in PHP
March 02, 2012 01:51PM
There is a nice function for reading in your html file if you have a layout
ob_get_contents()
Re: PHP 10 - Working with files in PHP
March 03, 2012 12:09PM
owned this section
Re: PHP 10 - Working with files in PHP
March 16, 2012 11:39AM
Done smiling smiley
Re: PHP 10 - Working with files in PHP
March 16, 2012 12:38PM
mm77509382

I think using b is make sure that your data doesn't get corrupted and to prevent portability issues later.
Re: PHP 10 - Working with files in PHP
March 20, 2012 10:26PM
Completed the section.
Re: PHP 10 - Working with files in PHP
May 07, 2012 10:37PM
Fulu-72924640 Wrote:
-------------------------------------------------------
> I am not responding to the last question but
> coming up with new one. This is an example from
> our manual, I can't get this right, when I run
> this script it's printing something like this
> without the comas not sure where am going wrong:
>
> Widget1blue£10
> Widget2red£12
> Widget3green£14
> Widget4black£16
> Widget5white£18
>
>
Language: PHP
> <?php > $fileArray = fopen("dictionary.txt" , "rb"); > > while (!feof($fileArray)) { > $arrayLine = fgets($fileArray, 1024); > $parts = explode(';=';, $arrayLine); > > echo $parts[0] . $parts[1] . "<br />"; > } > fclose($fileArray); > ?> >

I've noticed by saving both the .csv and .txt files for the dictionary file, there were no commas still afterwards, but I've added it so:

Language: PHP
<?PHP   $file_handle = fopen("widgets.csv", "r");   while (!feof($file_handle) ) {   $line_of_text = fgetcsv($file_handle, 1024);   print $line_of_text[0].", " .$line_of_text[1].", " .$line_of_text[2] . "<BR>";   }   fclose($file_handle);   ?>
Re: PHP 10 - Working with files in PHP
May 07, 2012 10:39PM
completed this sectionsmile
Sorry, you do not have permission to post/reply in this forum.