PHP 101 (part 5): Rank and File

July 21, 2004

Tutorials

Back to School
Handle With Care
•  Open the file and assign it a file handle
•  Interact with the file via its handle and extract its contents into a PHP variable
•  Close the file
Different Strokes

When Laziness is a Virtue
Writing to Ma
Information is Power
Breaking Eggs



Back to School

When you first started reading this series, I promised you that
you’d have a whole lot of fun. If you’re the cynical type, you may be
feeling that I didn’t keep my promise. After all, how much fun have you
really had so far? All you’ve done is learn a bunch of
theoretical rules, added and subtracted numbers from each other, learnt
primitive decision-making and gone round and round in the circular
funhouse of loops. Heck, if this wasn’t a PHP tutorial, it would be
kindergarten…

I hear you.

In this segment of our ongoing saga, I’m going to teach you how to
do something that’s definitely not for kids. It involves getting down
and dirty with files on the disk: meeting them (shock!), reading their
contents (shriek!) and (horror of horrors!) writing data to them. All
of these exciting activities will take place under the aegis of PHP’s
very cool file manipulation API, which allows you to view and modify
file attributes, read and list directory contents, alter file
permissions, retrieve file contents into a variety of native data
structures, and search for files based on specific patterns.

Let’s get started!


Handle With Care

I’ll begin with something simple: opening a file and reading its
contents. Let’s assume that somewhere on your disk, hidden under
/usr/local/stuff/that/should/be/elsewhere/recipes/, you have a text
file containing the recipe for the perfect Spanish omelette. You now wish
to read the contents of this file into a PHP script.

In order to do this, there are three distinct steps to be
followed:

  • Open the file and assign it a file handle.
  • Interact with the file, via its handle, and extract its contents
    into a PHP variable.
  • Close the file.

Here’s a PHP script that does just that:



<?php

// set file to read
$file = '/usr/local/stuff/that/should/be/elsewhere/recipes/omelette.txt' or die('Could not open file!');

// open file

$fh = fopen($file, 'r') or die('Could not open file!');

// read file contents

$data = fread($fh, filesize($file)) or die('Could not read file!');

// close file

fclose($fh);

// print file contents

echo $data;

?>



Run this script through your Web browser, and PHP should return the
contents of the file.

Now let me explain each of the three steps above in detail:


Open the file and assign it a file handle

PHP needs a file handle to read data from a file. This file handle
can be created with the fopen() function, which accepts two arguments:
the name and path to the file, and a string indicating the “mode” in
which the file is to be opened ('r' for read).

Three different modes are available for use with the fopen()
function. Here’s the list:

'r' – opens a file in read mode

'w' – opens a file in write mode, destroying existing file
contents

'a' – opens a file in append mode, preserving existing file
contents


Interact with the file via its handle and extract its contents
into a PHP variable

If the fopen() function is successful, it returns a file handle,
$fh, which can be used for further interaction with the file. This file
handle is used by the fread() function, which reads the file and places
its contents into a variable.

The second argument to fread() is the number of bytes to be read.
You can usually obtain this information through the filesize()

function, which – who’d have guessed it?!- returns the size of the
file in bytes.


Close the file

This last step is not strictly necessary as PHP closes the file
automatically once it reaches the end of the script, but it’s a good
habit to develop. Explicitly closing the file with fclose() has two
advantages: it ties up loose ends in your script, and it wins you lots
of good karma from the PHP community.

You probably haven’t see the die() function before, either. This
function is mostly used as a primitive error-handling mechanism. In the
event of a fatal error, such as the file path being invalid or the file
permissions being such that PHP cannot read it, die() terminates script
processing and optionally displays a user-specified error message
indicating why it committed suicide.


Different Strokes

An alternative method of reading data from a file is the very cool
file() function, which reads the entire file into an array (remember
them?) with one line of code. Each element of the array then contains
one line from the file. To display the contents of the file, simply
iterate over the array in a foreach() loop and print each element.

The following example demonstrates:

<?php

// set file to read
$file = '/usr/local/stuff/that/should/be/elsewhere/recipes/omelette.txt' or die('Could not read file!');

// read file into array

$data = file($file) or die('Could not read file!');

// loop through array and print each line

foreach ($data as $line) {

     echo
$line;

}

?>



In this example, the file() command opens the file, reads it into an
array and closes the file – all in one, single, elegant movement. Each
element of the array now corresponds to a line from the file. It’s easy
to print the file’s contents now – just reach for that mainstay of
array processing, the foreach() loop.

Don’t want the data in an array? Try the file_get_contents()
function, new in PHP 4.3.0 and PHP 5.0, which reads the entire file
into a string:



<?php

// set file to read
$file = '/usr/local/stuff/that/should/be/elsewhere/recipes/omelette.txt' ;

// read file into string

$data = file_get_contents($file) or die('Could not read file!');

// print contents

echo $data;

?>



Who am I kidding? I always use the one-line functions noted above
instead of the three-line sequence of fopen(), fread() and fclose().
Laziness conquers all.


When Laziness is a Virtue

PHP also offers two very useful functions to import files into a PHP
script: the include() and require()functions. These functions can be
used to suck external files lock, stock and barrel into a PHP script,
which is very handy if, for example, you have a modular application
which has its code broken down across files in separate locations.

The best way to understand the utility of the include() and
require() functions is with an example. Assume that on your Web site
you have a standard menu bar at the top of every page, and a standard
copyright notice in the bottom. Instead of copying and pasting the
header and footer code on each individual page, PHP gurus simply create
separate files for the header and footer, and import them at the top
and bottom of each script. This also makes a change to the site design
easier to implement: instead of manually editing a gazillion files, you
simply edit two, and the changes are reflected across your entire site
instantaneously.

Let’s see a real live example of this in action. Create the header
in one file, called header.php:



<html>

<head>

<title><?php echo $page['title'];?></title>

</head>

<body>

<!-- top menu bar -->

<table width="90%" border="0" cellspacing="5" cellpadding="5">

<tr>

<td><a href="#">Home</a></td>

<td><a href="#">Site Map</a></td>

<td><a href="#">Search</a></td>

<td><a href="#">Help</a></td>

</tr>

</table>

<!-- header ends -->


Next, create the footer with the copyright notice in a second file,
footer.php:



<!-- footer begins -->

<br />
<center>Your usage of this site is subject to its published <a href="tac.html">terms and conditions</a>. Data is copyright Big Company Inc, 1995-<?php echo date("Y", mktime()); ?></center>

</body>

</html>


Finally, create a script to display the main content of your site,
and include() the header and footer at appropriate places:



<?php

// create an array to set page-level variables
$page = array();

$page['title'] = 'Product Catalog';

/* once the file is imported, the variables set above will become available to it */

// include the page header

include('header.php');

?>



<!-- HTML content here -->

<?php

// include the page footer

include('footer.php');

?>


Now, when you run the script above, PHP will automatically read in
the header and footer files, merge them with the HTML content, and
display the complete page to you. Simple, isn’t it?

Notice that you can even write PHP code inside the files being
imported. When the file is first read in, the parser will look for
<?php...?> tags, and automatically execute the code inside it.
(If you’re familiar with JavaScript, you can use this feature to
replicate functionality similar to that of the onLoad() page event
handler in JavaScript.)

PHP also offers the require_once() and include_once()functions,
which ensure that a file which has already been read is not read again.
This can come in handy if you have a situation in which you want to
eliminate multiple reads of the same include file, either for
performance reasons or to avoid corruption of the variable space.

A quick note on the difference between the include() and
require()functions: the require()function returns a fatal error if the
named file cannot be found and halts script processing, while the
include() function returns a warning but allows script processing to
continue.


Writing to Ma

After everything you’ve just read, you’ve probably realized that
reading a file is not exactly brain surgery. So let’s proceed to
something slightly more difficult – writing to a file.

The steps involved in writing data to a file are almost identical to
those involved in reading it: open the file and obtain a file handle,
use the file handle to write data to it, and close the file. There are
two differences: first, you must fopen() the file in write mode ('w'
for write), and second, instead of using the fread() function to read
from the file handle, use the fwrite() function to write to it. Take a
look:



<?php

// set file to write
$file = '/tmp/dump.txt';

// open file

$fh = fopen($file, 'w') or die('Could not open file!');

// write to file

fwrite($fh, "Look, Ma, I wrote a file!
"
) or die('Could not write to file');

// close file

fclose($fh);

?>



When you run this script, it should create a file named dump.txt in
/tmp, and write a line of text to it, with a carriage return at the
end. Notice that double quotes are needed to convert
into a carriage
return.

The fopen(), fwrite() and fread() functions are all binary-safe,
which means you can use them on binary files without worrying about
damage to the file contents. Read more about many of the issues related
to binary-safe file manipulation on different platforms at "http://www.php.net/manual/en/function.fopen.php" target="_blank" target='blank'>http://www.php.net/manual/en/function.fopen.php.

If I’ve spoiled you by showing you the one-line shortcut functions
for file reads, let me damage you further by introducing you to the
file_put_contents() function, new in PHP 5.0, which takes a string and
writes it to a file in a single line of code.



<?php

// set file to write
$filename = '/tmp/dump.txt';

// write to file

file_put_contents($filename, "Look, Ma, I wrote a file!
"
) or die('Could not write to file');

?>



Bear in mind that the directory in which you’re trying to create the
file must exist before you can write to it. Forgetting this important
step is a common cause of script errors.


Information is Power

PHP also comes with a bunch of functions that allow you to test the
status of a file – for example to find out whether it exists, whether
it’s empty, whether it’s readable or writable, and whether it’s a
binary or text file. Of these, the most commonly used operator is the
file_exists() function, which is used to test for the existence of a
specific file.

Here’s an example which asks the user to enter the path to a file in
a Web form, and then returns a message displaying whether or not the
file exists:


<html>

<head>

</head>

<body>

<?php

// if form has not yet been submitted

// display input box

if (!isset($_POST['file'])) {

?>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

    Enter file path <input type="text" name="file">

    </form>

<?php

}

// else process form input

else {

    
// check if file exists

    // display appropriate message

    
if (file_exists($_POST['file'])) {

        echo 'File exists!';

        }

    else {

        echo
'File does not exist!';

    }

}

?>

</body>

</html>


There are many more such functions. Here’s a brief list, followed by
an example that builds on the previous one to provide more information
on the file specified by the user.

  • is_dir() – returns a Boolean indicating whether the specified path
    is a directory
  • is_file() – returns a Boolean indicating whether the specified file
    is a regular file
  • is_link() – returns a Boolean indicating whether the specified file
    is a symbolic link
  • is_executable() – returns a Boolean indicating whether the
    specified file is executable
  • is_readable()- returns a Boolean indicating whether the specified
    file is readable
  • is_writable()- returns a Boolean indicating whether the specified
    file is writable
  • filesize() – gets size of file
  • filemtime() – gets last modification time of file
  • filamtime() – gets last access time of file
  • fileowner() – gets file owner
  • filegroup() – gets file group
  • fileperms() – gets file permissions
  • filetype() – gets file type

This script asks for a file name as input and uses the functions
above to return information on it.


<html>

<head>

</head>

<body>

<?php

/* if form has not yet been submitted, display input box */

if (!isset($_POST['file'])) {

?>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

    Enter file path <input type="text" name="file">

    </form>

<?php

}

// else process form input

else {

    echo
'File name: <b>'.$_POST['file']  .'</b><br />';

    /* check if file exists and display appropriate message */

    
if (file_exists($_POST['file'])) {

        
// print file size

        
echo 'File size: '.filesize($_POST['file']).' bytes<br />';

        // print file owner

        
echo 'File owner: '.fileowner($_POST['file']).'<br />';

        
// print file group

        
echo 'File group: '.filegroup($_POST['file']).'<br />';

        // print file permissions

        
echo 'File permissions: '.fileperms($_POST['file']).'<br />';

        
// print file type

        
echo 'File type: '.filetype($_POST['file']).'<br />';

        // print file last access time

        
echo 'File last accessed on: '.date('Y-m-d', fileatime($_POST['file'])).'<br />';

        // print file last modification time

        
echo 'File last modified on: '.date('Y-m-d', filemtime($_POST['file'])).'<br />';

        // is it a directory?

        
if (is_dir($_POST['file'])) {

            echo
'File is a directory <br />';

        }

        
// is it a file?

        
if (is_file($_POST['file'])) {

            echo 'File is a regular file <br />';

        }

        
// is it a link?

        
if (is_link($_POST['file'])) {

            echo
'File is a symbolic link <br />';

        }

        // is it executable?

        
if (is_executable($_POST['file'])) {

            echo
'File is executable <br />';

        }

        
// is it readable?

        
if (is_readable($_POST['file'])) {

            echo 'File is readable <br />';

        }

        
// is it writable?

        
if (is_writable($_POST['file'])) {

            echo
'File is writable <br />';

        }

    }

    else {

        echo 'File does not exist! <br />';

    }

}

?>

</body>

</html>

And here’s what the output might look like:

File name: /usr/local/apache/logs/error_log
File size: 53898 bytes
File owner: 0
File group: 0
File permissions: 33188
File type: file
File last accessed on: 2004-05-26
File last modified on: 2004-06-20
File is a regular file
File is readable


Breaking Eggs

So now you know how to read a file, write to it, and test its
status. Let’s look at some examples of what you can do with this
new-found power.

Let’s go back to my Spanish omelette recipe. Let’s suppose I’m feeling
generous, and I decide that I’d like to hear what people really

think about my culinary skills. Since I have a bunch of recipes that
I’d like to share with people, and since they all look something like
this:

SPANISH OMELETTE
INGREDIENTS:
- 1 chopped onion
- 1 chopped tomato
- 1/2 chopped green pepper
- 4 beaten eggs
- Salt and pepper to taste
METHOD:
1. Fry onions in a pan
2. Pour beaten eggs over onions and fry gently
3. Add tomatoes, green pepper, salt and pepper to taste
4. Serve with toast or bread

I need a quick way to convert them all into HTML so that they look
presentable on my Web site. We’ve already established that I’m lazy, so
fuggedaboutme re-creating the recipes in HTML. Instead, I’ll have PHP
do the heavy lifting for me:


<html>

<head></head>
<body>

<?php

// read recipe file into array

$data = file('/usr/local/stuff/that/should/be/elsewhere/omelette.txt') or die('Could not read file!');

/* first line contains title: read it into variable */

$title = $data[0];

// remove first line from array

array_shift($data);

?>

<h2><?php echo $title; ?></h2>

<?php

/* iterate over content and print it */

foreach ($data as $line) {

    echo nl2br($line);

}

?>

</body>

</html>


I’ve used the file() function to read the recipe into an array, and
assign the first line (the title) to a variable. That title is then
printed at the top of the page. Since the rest of the data is fairly
presentable as is, I can simply print the lines to the screen one after
the other. Line breaks are automatically handled for me by the
extremely cool nl2br() function, which converts regular text linebreaks
into the HTML equivalent, the <br /> tag. The end result: an
HTML-ized version of my recipe that the world can marvel at. Take a look:

<html>
<head></head><body>

<h2>SPANISH OMELETTE
</h2>
INGREDIENTS:<br />
- 1 chopped onion<br />
- 1 chopped tomato<br />
- 1/2 chopped green pepper<br />
- 4 beaten eggs<br />

- Salt and pepper to taste<br />
METHOD:<br />
1. Fry onions in a pan<br />
2. Pour beaten eggs over onions and fry gently<br />
3. Add tomatoes, green pepper, salt and pepper to taste<br />
4. Serve with toast or bread<br />

</body>
</html>

If the elegance and creative simplicity of my Spanish omelette recipe
has left you speechless, I’m not surprised – many people feel that
way. Until you get your voice back: Ciao… and make sure you come back
to work through Part Six of PHP 101,
which discusses creating your own reusable functions.


Copyright Melonfire, 2004 (http://www.melonfire.com).
All rights reserved.

,

41 Responses to “PHP 101 (part 5): Rank and File”

  1. bondjimbond Says:

    I thought that at first, too – all those IFs in a row. But it makes sense because when the file matches more than one of the cases, you want to make sure it reports all of them.

  2. j_smith1981 Says:

    Just to show an example of what I am talking about for anyone wanting to see an example here you go:

    <html>
    <head>
    <title></title>
    </head>

    <body>
    <?php
    if(!isset($_POST['submit'])) {
    $message = ‘Find out about files/directories here: ‘;
    ?>

    <?php echo (isset($message)) ? "<p>$message</p>" : ”; ?>

    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
    <label for="file_directory">Enter a file or directory name: </label><input type="text" id="file_directory" name="file_directory" />
    <input type="submit" id="submit" name="submit" value="Find Info" />
    </form>

    <?php

    } else {

    // do processing for into of file or directory:

    trim($_POST['file_directory']);

    if(strlen($_POST['file_directory']) > 0) {

    $file = ‘/html/projects-work/php/zend_101/05_Rank_and_File/file/’.$_POST['file_directory']; // kind of chrooting the user into a directory only allowed for global users!

    switch ($file) {

    case file_exists($file):

    // occures or is executed if file exists:
    echo ‘File found!’;
    echo ‘<p>File/Directory name is: ‘.$_POST['file_directory']."<br>\n";
    echo ‘File size: ‘.(filesize($file) / 1024)." bytes big <br>\n";
    echo ‘File owner: ‘.fileowner($file)."<br> \n";
    echo ‘File group: ‘.filegroup($file)."<br> \n";
    echo ‘File permissions: ‘.fileperms($file)."<br> \n";
    echo ‘File type: ‘.filetype($file)."<br> \n";
    echo ‘Last accessed on: ‘.date(‘d/m/Y’, fileatime($file))."<br>\n";
    echo ‘Last modified on: ‘.date(‘d/m/Y’, filemtime($file))."<br>\n</p>";

    break;

    default:
    // does not exist, just the default action using like an else statement:
    echo ‘<p>File/Directory does not exist, please go back <a href="06_Details_About_Files.php">here</a></p>’;
    break;
    }

    } else {
    echo ‘<p>You did not enter a value in the field required, please try again <a href="06_Details_About_Files.php">here</a></p>’;
    }
    }
    ?>
    </body>
    </html>

  3. j_smith1981 Says:

    I would use the if of a switch statement(s) to do all those if’s

    Makes a whole lot more sense, if they are or not as it where.

    Just makes it a whole lot more logical being a web developer myself.

    Maybe this is touched on a bit later like so:

    switch ($file) {
    case ‘is_directory’:

    // action to take if it is a directory:
    break; // use this so it doesnt recursively go through all the other case statements in the logic!

    }// end of switch!

  4. j_smith1981 Says:

    filesize() only reads how many bits (not bytes) the file holds.

    To make a good calculation to turn the size of the file in bytes one would have to use the following on an output:

    (filesize($file) / 1024) // where file is the opened file in the PHP script.

    Just thought I would give a heads up in this respect.

  5. fdrouillard Says:

    Change the following:

    // read recipe file into array
    $data = file(‘/usr/local/stuff/that/should/be/elsewhere/omelette.txt’) or die(‘Could not read file!’);

    to:

    // set file to read
    $file = ‘/usr/local/stuff/that/should/be/elsewhere/omelette.txt’;

    // read recipe file into array
    $data = file($file) or die(‘Could not read file!’);

    Hope that helps!

  6. jenycz Says:

    I forgot to add the ../ before my filename to include my url and root stuff…

    Fixed!

  7. jenycz Says:

    Hey -

    First off I’d like to thank you for the wonderful tutorial, it’s been an easy read and you treat us like we have no idea whats going one – which is perfect, cause prior to this, I didn’t!

    Here is the one and only problem I’m having – I can’t seem to get my file to write…

    <?php

    // set file to write
    $filename = ‘/dump.txt’;
    // write to file
    file_put_contents($filename, "I wrote a file!") or die(‘Could not write to file’);

    ?>

    I’ve made it as simple as possible buy letting it right the dump.txt directly into my root folder so I could rule out it being a filename error on my part.

    Here’s the error I keep getting.

    Warning: file_put_contents(/dump.txt) [function.file-put-contents]: failed to open stream: Permission denied in D:\Hosting\4506153\html\html\test.php on line 14
    Could not write to file

    I know for a fact that all my files have read/write/execute access (I set them for this tutorial!)

    Any help is greatly appreciated, and thanks again mate!

    - Jack

  8. mahalie Says:

    Not sure about WAMP environments, on Windows but all those on Linux servers and hosted space should be able to complete the examples keeping in mind you’ll need to make sure file permissions are set on the directory in question so that you have read + write permission. Uh, not you, the script.

    For example, I’ve been working in an ‘exercises’ directory in my webserver. I needed to CHMOD the directory to 747 for all the examples to work. (747 = read/write/execute for both owner and others/all). You can this using an FTP client like Filezilla (right-click the directory) or by command line on the server if you are familiar with CLI.

  9. _____anonymous_____ Says:

    <html>
    <head></head>
    <body>

    <p>
    <h2>SPANISH OMELETTE ala Pablo_Sanz (with a twist!)
    </h2>

    INGREDIENTS:<br />
    - 1 chopped onion<br />
    - 1 sliced potato (circular, thin, irregular, chips)<br />
    - 1 chopped tomato<br />
    - 1/2 chopped green pepper<br />
    - 4 beaten eggs<br />
    - Salt and pepper to taste<br />
    METHOD:<br />
    0. Place potato chips in pan with Olive Oil<br />
    1. Place chopped onions on top of potato chips<br />
    2. Cover pan and fry using medium-low heat until potato-chips are golden brown<br />
    3. Place browned potatoes-chip and onion mixture in a cullender to drain<br />
    4. Beat eggs while mixture is draining<br />
    5. Place drained mixture in bowl with beaten eggsand allow mixture to saturate<br />
    6. Place saturated mixture back into pan with several drops of drained Olive Oil (from step 4 above)<br />
    7. Add tomatoes, green peppers, salt, and pepper (to taste)<br />
    8. Cook on medium heat<br />
    9. After omelette browns (bottom side) place a plate over pan and flip pan over<br />
    A. Place pan back over heat<br />
    B. Add a few more drops of Olive Oil to pan<br />
    C. Slide omelette back into pan and cook for a few more minutes<br />
    D. Repeat steps 9, A, and C, to satifaction<br />
    E. Eat the omelette. Buen provecho!!<br />
    F. Wash and dry the dishes

    </p>

    </body>
    </html>

  10. pablo_sanz Says:

    The tutorial is great, but, sorry to say this, the recipe for the Spanish omelette (tortilla) has little to do with the real thing.

    It’s true that there are Spanish people that add vegetables to the Spanish omelette (as sort of variations), but the original recipe is as simple as: Olive oil, eggs, potatoes, onion is optional (if you like it), and salt.

    - Put chopped potatos (sort of irregular chips, more like circular, thin chips) in an pan with enough olive oil.
    - If you want onion (for me it’s a must), put the chopped onion on top of the potatoes and put a lid over the pan.
    - Let it fry slowly mixing onion and potatoes until the potatoes are slightly brown.
    - Take the pan out of the fire and put the resulting mass in a colander so that the excess of oil drifts away.
    - Beat the eggs
    - Put the oil-drained mass in a bowl with the beaten eggs and leave for the potatoes (the mass) to absorb the beaten egg. (This is important. Too much egg bad and too little wrong)
    - Put a pan on the fire with a few drops of the used olive oil and add the mass (chips, onions and eggs). Medium fire.
    - After a few minutes, and using a dish as a sort of lid, turn the omelette on the dish and put it back in the pan (on the uncooked side of the omelette).
    - Keep doing this until both sides of the omelette are brownish.

    Buen provecho!!

  11. _____anonymous_____ Says:

    These tutorials are excellent, just the header and footer information just made my day, will make future web development so easy, oh thank you so much for such a great resource :)

  12. _____anonymous_____ Says:

    To rickyric (dude before me)

    When you get the file path:

    $file = ‘/Users/Ricky/Sites/test1.txt’ or die( ‘Could not create file!’);

    you should remove the first ‘/’ (slash)

    and keep in mind that the folder "Users"

    must be in your root folder, mine is C:/wamp/www

    so in my case it would be: C:/wamp/www/Users…

    if it is not in the root folder you need to put the full path.

    yes starting from C:/… (and ending in) …filename.txt

  13. rickyric Says:

    Hi, I’m unable to open a file in write mode following is the code and the warning :

    <?php

    // set the file to read.
    $file = ‘/Users/Ricky/Sites/test1.txt’ or die( ‘Could not create file!’);

    $fh = fopen($file, ‘w’) or die( ‘Could not open!!’);

    fwrite($fh, "hi") or die( ‘could not write to file!’ );

    fclose($fh);

    echo $data;

    ?>

    Warning: fopen(/Users/Ricky/Sites/test1.txt) [function.fopen]: failed to open stream: Permission denied in /Users/Ricky/Sites/index.php on line 7
    Could not open!!

    suggestions please !!!

  14. mrmyth Says:

    What if I want to print an array to a file. Is that easy too?

  15. billium Says:

    PHP operates on a ‘server’ which on your computer is at

    C:\Inetpub\wwwroot\

    it cannot access files from the rest of your computer so all the files you want to access should be on the server. You need to copy the file from ‘documents and settings’ to this folder

  16. ShyAnn44 Says:

    I am reallllly new at this. Love these lessons. My questions does the file you are trying to read have to be in the root file. Can it not be anywhere on your computer?

    <?php

    // set file to read
    $file = ‘c:\Documents and

    Settings\Annette\My

    Documents\Phone

    numbers\andrewsletter.txt ‘ or

    die(‘Could not read file!’);
    // read file into array
    $data = file($file) or die(‘Could

    not read file!’);
    // loop through array and print

    each line
    foreach ($data as $line) {
    echo $line;
    }

    ?>

    I am getting…

    Warning: file(c:\Documents and Settings\Annette\My Documents\Phone numbers\andrewsletter.txt ) [function.file]: failed to open stream: Permission denied in C:\Inetpub\wwwroot\readfile.php on line 6
    Could not read file!

  17. ShyAnn44 Says:

    I am reallllly new at this. Love these lessons. My questions does the file you are trying to read have to be in the root file. Can it not be anywhere on your computer?

    <?php

    // set file to read
    $file = ‘c:\Documents and

    Settings\Annette\My

    Documents\Phone

    numbers\andrewsletter.txt ‘ or

    die(‘Could not read file!’);
    // read file into array
    $data = file($file) or die(‘Could

    not read file!’);
    // loop through array and print

    each line
    foreach ($data as $line) {
    echo $line;
    }

    ?>

    I am getting…

    <?php

    // set file to read
    $file = ‘c:\Documents and

    Settings\Annette\My

    Documents\Phone

    numbers\andrewsletter.txt ‘ or

    die(‘Could not read file!’);
    // read file into array
    $data = file($file) or die(‘Could

    not read file!’);
    // loop through array and print

    each line
    foreach ($data as $line) {
    echo $line;
    }

    ?>

  18. _____anonymous_____ Says:

    What if I want to open a file that resides on a different server. Everytime I put in the path of the file, it doesnt seem to work. Please help!

  19. lab69 Says:

    I have been reading this tutorial since the first part and it has been so good that I am here writing this post.

    Thanks for this and see you in the next…..

  20. mikeyeli Says:

    The recipe is 100% eadible!

    by the way, great tutorial.

  21. _____anonymous_____ Says:

    I keep getting "could not write to file" any ideas? Thanks!

    if(isset($_POST['newssubmit'])) {

    $title = $_POST['title'];
    $date = $_POST['date'];
    $time = $_POST['time'];
    $poster = $_POST['poster'];
    $news = $_POST['message'];

    foreach($_POST['check'] as $value) {
    $check_type .= "Checked: $value\n";
    }

    // set file to write
    $file = "http://hypboard.com/thehyp/generalnews.txt&quot;;
    // open file
    $fh = fopen($file, ‘w’) or die(‘Could not open file!’);
    // write to file
    fwrite($fh, "hi ") or die(‘Could not write to file’);
    // close file
    fclose($fh);

  22. another-newbie Says:

    When I use file_put_contents I first got a fatal error undefined function. Realizing that the server didn’t have php 5, I tried out the function on a server that uses PHP Version 5.2.5. Even after copying and pasting the code, I still got Could not write to file.

    I’m using Dreamweaver 2004 MX and have set up my preferences for different colors to easily ID certain code, for functions etc. DW doesn’t seem to recognize it as a function-just plain text, though I don’t think the color of my code really has anything to do with how it is interpreted by PHP…unless PHP color preference are covered later ;-)

    Any ideas? or do I have to write to files the "long" way as that worked.

    Here’s what I did and I even removed the directory so that both the file and txt file would be in the same place.

    <?php
    // set file to write
    $filename = ‘dump.txt’;
    // write to file
    file_put_contents($filename, "Look, Ma, I wrote a file! ") or die(‘Could not write to file’);

    ?>

  23. _____anonymous_____ Says:

    I found that I was able to refer to the files in this way:

    "C:/wamp/www/Tutorial_Examples/stuff/omlette.txt"

    I had to refer to the file using its full address.

  24. _____anonymous_____ Says:

    How do I refer to files in my local computer?
    I am using you example of checking for an existing file, but don’t know how to refer to my disk c:.

    Thanks for the great tutorial.
    Rony

  25. _____anonymous_____ Says:

    2 ALL tech books AUTHORS
    Worst thing you can do for us foreign learners is making jokes trough text. It is quite confusing, usually.

  26. _____anonymous_____ Says:

    Tnx for help and tips.
    I have to say that it’s sometimes hard to get some things and not to get rush to ask somebody immediately about, when you are learning something in your second language. It’s especially tricky (hard), when you don’t live in English speaking country.

  27. phileasphogg Says:

    Dear Glesinho and anyone else – noobs like me, primarily! – who might have difficulty in finding a folder path to create files in, as in this section, Part 5, of this great – I said G – R – E – A – T ! – tutorial, here is a suggestion:

    Create a "tmp" folder (or give it any name you like, just make sure that it is referenced correctly in the PHP code itself) as a sub-folder to the folder where you have the "executionary" file that will be using the files in the tmp folder. In my case, this set of wonderful tutorials are located under c\wamp\www\Tutorials\ (with Part1, Part2, etc., as sub-folders to the Tutorials folder), so with the tmp folder as a sub-folder to my Part5 folder, all I have to do to designate the path of the "dump.txt" file and the "Spanish_Omlette.txt" file (yes, I just copied Vikram’s presumably fictional recipe over into my Notetab Light text editor without any HTML tags and saved it in the tmp folder with the .txt name in question) is to use this path: ‘tmp/dump.txt’ (or ‘tmp/Spanish_Omlette.txt’) wherever such a reference is relevant, such as the ones listed here in Part 5.

    A user back in 2006 asked how he/she could avoid generating an error message when running this script (since the file didn’t exist in his/her case), and another user gave him/her the trick of placing an "@" in front of the ‘include()’ string. What a shame! The whole point of the tutorials is to do the exercises – this is where all the fun is!! In fact, I end up doing some add-on stuff mentioned here but not specifically part of the routine, just to try it out. It is very pleasing when one can take a step or two away from the beaten path, even if one can only manage being one or two steps from that path at this stage.

    I wouldn’t dream of opting for shortcuts rather than doing the exercises, however many wrinkles it puts in my brow (and believe me, it does sometimes get tricky, especially when files created in my text editor will not even appear in the localhost files! (I can access them under WampServer’s ‘www directory’ option, but they do not get executed there), but I have managed to figure out how to get around that, though I am at a loss to explain why WampServer balked in the first place, but one day, sooner or later, it will dawn on me).

    If I get completely stumped, then I ask for help in a forum – but it is help in how to do the exercises, not in how to bypass them, that I ask for in such cases!

    Yours,
    phileasphogg

  28. Glesinho Says:

    In examples shown above, for all these file paths, like $file = ‘/example/dir/file.txt’, i can’t use path relative to root path (to my virtual host address where my index.php is located – /phplearning/). I have to use path relative to /www/phplearning/example/… root directory for virtual hosts.

    Anyone have idea why is it so?

    Tnx in advance

  29. _____anonymous_____ Says:

    I’ve never programmed. I’m just a designer (html, css, photoshop–the end).

    Up to this lesson I was able to slap together a comment script (after finding FILE_APPEND elsewhere). I had no idea I’d make something I could actually use in just 2 days.

    Can’t wait to really use a database instead of a text file to store information. Thanks so much!

    Check it out (my first script ever, and I’m a designer, so excuse all the style definitions :P) :

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    Enter your name: <br />
    <input name="firstname" /><br /><br />
    Enter your comments:<br />
    <textarea rows="10" cols="20" name="comments"></textarea>
    <br />
    <input type="submit" name="submit" value="Submit">
    </form>

    <?php
    if (isset($_POST['submit'])) {
    $firstname = $_POST['firstname'];
    $comments = $_POST['comments'];

    $commentLog = "comments.txt";

    file_put_contents ($commentLog,’<div style="width:400px;"><p class="name" style="border-bottom: solid 1px; background:#76b900; color:#fff; padding:2px;"><span class="date" style="font-size:10px; float:right;">’.date(‘m-d-Y’).’</span> ‘.$firstname.’</p><p class="comment" style="color: #7498bf; border-left: solid 2px; margin-left: 10px; font-size:14px; margin-top:-10px; padding-left:4px;">’.$comments.’</p></div>’,FILE_APPEND);

    }
    ?>

    <h2>Comments</h2>
    <?php

    $commentLogDisplay = ‘comments.txt’ ;
    $commentDisplay = file_get_contents($commentLogDisplay) or die (‘Be the first to comment!’);

    echo nl2br($commentDisplay);

    ?>

  30. _____anonymous_____ Says:

    >> fwrite($fh, ".") or die(‘Could not append CR to file!’);
    Instead of using the ".", you have to use "backslash n"(UNIX), "backslash r"(MAC), or "backslash r backslash n"(WINDOWS).

  31. _____anonymous_____ Says:

    I am not able to create carriage returns for some reason in text files that I am writing in PHP. I am writing items to a file that a user inputs and then trying to separate each item by a comma. I then would like to end each line (3 items per line) in a carriage return. I tried that with double quotes, but that didn’t work. Hopefully someone can tell me what I am doing wrong.

    <?php

    // set file to write
    $file = ‘c:phplearn est.txt’;

    // open file
    $fh = fopen($file, ‘a’) or die(‘Could not open file!’);

    // append to file
    fwrite($fh, $Exercise) or die(‘Could not append Exercise to file!’);
    fwrite($fh, ‘,’) or die(‘Could not append comma to file!’);
    fwrite($fh, $Time) or die(‘Could not append Time to file!’);
    fwrite($fh, ‘,’) or die(‘Could not append comma to file!’);
    fwrite($fh, $Day) or die(‘Could not append Day to file!’);
    fwrite($fh, ".") or die(‘Could not append CR to file!’);

    // close file
    fclose($fh);

    ?>

  32. _____anonymous_____ Says:

    I am not able to create carriage returns for some reason in text files that I am writing in PHP. I am writing items to a file that a user inputs and then trying to separate each item by a comma. I then would like to end each line (3 items per line) in a carriage return. I tried that with double quotes, but that didn’t work. Hopefully someone can tell me what I am doing wrong.

    <?php

    // set file to write
    $file = ‘c:phplearn est.txt’;

    // open file
    $fh = fopen($file, ‘a’) or die(‘Could not open file!’);

    // append to file
    fwrite($fh, $Exercise) or die(‘Could not append Exercise to file!’);
    fwrite($fh, ‘,’) or die(‘Could not append comma to file!’);
    fwrite($fh, $Time) or die(‘Could not append Time to file!’);
    fwrite($fh, ‘,’) or die(‘Could not append comma to file!’);
    fwrite($fh, $Day) or die(‘Could not append Day to file!’);
    fwrite($fh, ".") or die(‘Could not append CR to file!’);

    // close file
    fclose($fh);

    ?>

  33. _____anonymous_____ Says:

    That first die function shouldn’t be there. All that line does is assign the filepath as a string to the $file variable. The filepath is not checked at that point. Since the assignment doesn’t fail, the die() function is not invoked. The following file operation is where we discover that the filepath is actually invalid, so the second die() function is called.

    Cheers,
    Paul.

  34. _____anonymous_____ Says:

    I’ve tried using this, but I can’t seem to get it to work! I changed the die message to try to figure out what was wrong:

    <?php

    $file = ‘/test/test.txt’ or die (‘Could not open file’);

    $fh = fopen($file, ‘r’) or die(‘Could not open the file!’);

    $data = fread($fh, filesize($file)) or die(‘Could not read that file’);

    fclose($fh);

    echo $data;

    ?>

    I’m getting this output:

    Could not open the file!

    so I’m assuming it can find the file, but for some reason can’t open it. This isn’t a good start for me :p

  35. admin Says:

    Thanks, I’ve made the correction.

    =C=

  36. _____anonymous_____ Says:

    Look at the next two lines:

    filemtime() – gets last modification time of file
    filemtime() – gets last access time of file

    The first line is correct, but the second one should read:

    fileatime() – gets last access time of file

    Just thought you ought to know…

    –Red

  37. _____anonymous_____ Says:

    Hello,

    On your first example on this page you maybe want to enhance the script combining things used in early scripts, like this:

    <?php

    // set file to read
    $file = ‘/home/ivan/open_sesame.txt’;
    // open file
    $fh = fopen($file, ‘r’) or die(‘Could not open file!’);
    // read file contents
    $data = fread($fh, filesize($file)) or die(‘Could not read file!’);
    // close file
    fclose($fh);
    // print file contents
    $data_ord = explode("\n", $data);
    print_r($data_ord);

    ?>

    And for the second example someone maybe want to try this one a little bit modified:

    <?php

    // set file to read
    $file = ‘/home/ivan/open_sesame.txt’;

    // read file into array
    $data = file($file) or die(‘Could not read file!’);

    // loop through array and print each line
    foreach ($data as $line) {
    echo $line."<br />";
    }

    ?>

    Good material and also is fun to read it.

    Thanks.

  38. _____anonymous_____ Says:

    If you don’t want to turn all error-messages off (that’s what heymcd’s proposition would do) you could instead set a ‘@’ in front of the include function.

    This would look like this:

    <?php

    @include ("header.php");

    ?>

  39. heymcd Says:

    > Does anyone know how to shut off the error message if it cant find the file?

    error_reporting in php.ini

    > And is there a good way to get it to look at the start of the domain or a specific folder each time without having to put this http://www.somedomain.com part? (‘http://www.somedomain.com/header.php’);

    /header.php will find the file in root of your site /includes/header.php will find the file in the /includes directory of your site

  40. _____anonymous_____ Says:

    // include the page header
    include(‘header.php’);

    I have two quick questions on this one little thing up there.. if anyone answers, thanks!

    Does anyone know how to shut off the error message if it cant find the file?
    And is there a good way to get it to look at the start of the domain or a specific folder each time without having to put this http://www.somedomain.com part? (‘http://www.somedomain.com/header.php’);

  41. _____anonymous_____ Says:

    Interesting, I’ve seen some people put the egg in last.