Showing source code for /jcd/useful/webcon/2005/filestore.php

Show normal

More about showing source

 

<?php
include "header.php";
include 
"navbar.php";
?>


<h4>Write to a file</h4>

<ol>

<li><p>For the next exercise, create a new file in your text editor with
the following content:</p>

<?php $tmp = <<<END
<html>
<body>

<?php 

\$filename = "data/file1.txt";

\$file_handle = fopen( \$filename, "w" );

if( ! fwrite( 
\$file_handle, "Hello world!\\n" ) ){
        exit( "Cannot write to file, 
\$filename" );
}else{
    echo "Successfully wrote <a href=\"
\$filename\">\$filename</a>\\n";
}

fclose( 
\$file_handle );

?>

</body>
</html>
END;
print_code($tmp);
?>

<li><p>Save it as <i class=link>file_writer.php</i> on your site.  View
the page online - you should see a few errors.  This is
intentional.</p>

<li><p>What needs to happen next is you must first create the folder
called <i class=link>data</i>.  Try it again.</p>

<li><p>Now you need to make the folder writable by the PHP server.</p>

    <ul>

    <li><p><?=$psuicon?>Follow the directions on <a
    href="http://php.scripts.psu.edu/php_set_permissions.html">
    http://php.scripts.psu.edu/php_set_permissions.html</a> to grant
    the group <i class=link>php.scripts.psu.edu</i> Read / Write permission
    on your <i
    class=link>data</i> folder.</p>

    </ul>

<li><p>If everything worked correctly, you should be able to see the
text "Successfully wrote..." and follow the link to the file.</p>

</ol>

<h4>Read from a file</h4>

<ol>

<li><p>Now try to read the file back into a script; create a new file
with the following content:</p>

<?php $tmp = <<<END
<html>
<body>
<style type="text/css">
<!--
    p.border 
{ border: solid 1px; }
-->
</style>

<?php

\$filename = "data/file1.txt";

\$file_handle = fopen( \$filename, "r" );

\$file_data = fread( \$file_handle, filesize(\$filename) );

echo "<p>Contents of 
\$filename:</p>\\n";
echo "<p class=border>
\$file_data</p>\\n";

fclose( 
\$file_handle );

?>

</body>
</html>
END;
print_code($tmp);
?>

<li><p>Save the file in your space as <i class=link>file_reader.php</i> and view it in
a browser.</p>

<li><p>Now try changing the content written to your datafile as you
have set in <i class=link>file_writer.php</i>; see if it changes in
<i class=link>file_reader.php</i>.</p>

</ol>

<h4>Using Comma Separated Values (CSV) files</h4>

<p>Files can be formatted in various ways, many of which are useful
to aid in reading back into a script for automatic parsing.  One such
format is Comma Separated Values (CSV).  CSV files are plain text files that
store tables of data.  Each line is a successive row of the table, and each 
field in a row is separated from the others by a comma.  You can use other
characters such as semicolon as the delimiter.  CSV are popular formats
for data as they are easy to read and understand.  Many popular programs
such as Microsoft Excel are able to import/export in CSV.</p>

<p>PHP includes functions such as <a
href="http://php.net/manual/en/function.fgetcsv.php">fgetcsv()</a> and
<a href="http://php.net/manual/en/function.fputcsv.php">fputcsv()</a>
that help you work with CSV files.</p>

<ol>

<li><p>Create a new file in your text editor with the following content:</p>

<?php $tmp = <<<END
<html>
<body>

<?php

\$filename = "data/file2.csv";

\$file_handle = fopen( \$filename, "w" );

\$table = array(
    array("Name","PSU-ID","HW1","HW2","HW3","HW4","MT1","HW5","HW6","HW7","HW8","MT2","HW9","HW10","FN"),
    array("John Doe","900000001","36","29","39","40","189","38","26","32","35","191","36","34","200"),
    array("Jane Dough","900000002","26","18","29","30","159","29","27","33","37","180","37","32","178"),
    array("Jeff Felps","900000003","32","27","38","28","175","35","26","31","32","170","35","39","171"),
    array("Mike Mork","900000004","34","24","33","40","189","38","26","32","35","191","36","34","200"),
    array("Sally Park","900000005","40","39","40","40","190","38","40","40","38","198","39","40","200"),
    );

foreach( 
\$table as \$line ){
    if( ! fputcsv( 
\$file_handle, \$line ) ){
        exit( "Cannot write to file, 
\$filename" );
    
}
}
echo "Successfully wrote <a href=\"
\$filename\">\$filename</a>\\n";

fclose( 
\$file_handle );

// since fputcsv() isn't ready yet, we'll need to write our own
function fputcsv( 
\$handle, \$row, \$del=',', \$quot='"' )
{
    
\$count = 0;
    foreach (
\$row as \$item){
        // check/correct data format
        
\$count ++;
    
}
    fputs( 
\$handle, implode( \$del, \$row ) . "\\n" );
    return 
\$count;
}

?>

</body>
</html>
END;
print_code($tmp);
?>

<li><p>Save the file in your space as <i class=link>csv_writer.php</i> and view it in
a browser.</p>

<li><p>Next, create a new file with the following content:</p>

<?php $tmp = <<<END
<html>
<body>

<p>Grades</p>
<table border=1>

<?php

\$filename = "data/file2.csv";

\$file_handle = fopen( \$filename, "r" );

\$line  = 0;
while( (
\$row = fgetcsv(\$file_handle)) !== FALSE ){
    echo "<tr>";
    
\$line++;
    
\$total = 0;
    
\$field_num = 0;
    foreach (
\$row as \$element){
        
\$field_num++;
        if(
\$field_num>2 and \$line>1){
            
\$total += \$element;
        
}
        echo "<td>" . 
\$element . "</td>";
    
}
    if(
\$line<2){
        echo "<td>total</td>";
    
}else{
        echo "<td>
\$total</td>";
    
}
    echo "</tr>
\\n";
}

fclose( 
\$file_handle );

?>
</table>
 
</body>
</html>
END;
print_code($tmp);
?>

<li><p>Save the file in your space as <i class=link>csv_reader.php</i> and view it in
a browser.</p>

<li><p><b>Extra</b> - Edit the file you wrote in the second example, <i class=link>file_reader.php</i> so that the <i class=link>$filename</i> points to <i class=link>data/file2.csv</i>.  View it in a browser.</p>

</ol>

<?php
include "navbar.php";
include 
"footer.php";
?>

 

Penn State

Web Conference 2005

Writing PHP for ITS/ASET Web services

Saving Data in Files

<- Back - Saving Data on the Server|Up |Saving Data in SQLite Database Files - Next ->

Write to a file

  1. For the next exercise, create a new file in your text editor with the following content:

    <html>
    <body>

    <?php 

    $filename 
    "data/file1.txt";

    $file_handle fopen$filename"w" );

    if( ! 
    fwrite$file_handle"Hello world!\n" ) ){
            exit( 
    "Cannot write to file, $filename" );
    }else{
        echo 
    "Successfully wrote <a href=\"$filename\">$filename</a>\n";
    }

    fclose$file_handle );

    ?>

    </body>
    </html>
  2. Save it as file_writer.php on your site. View the page online - you should see a few errors. This is intentional.

  3. What needs to happen next is you must first create the folder called data. Try it again.

  4. Now you need to make the folder writable by the PHP server.

  5. If everything worked correctly, you should be able to see the text "Successfully wrote..." and follow the link to the file.

Read from a file

  1. Now try to read the file back into a script; create a new file with the following content:

    <html>
    <body>
    <style type="text/css">
    <!--
        p.border { border: solid 1px; }
    -->
    </style>

    <?php

    $filename 
    "data/file1.txt";

    $file_handle fopen$filename"r" );

    $file_data fread$file_handlefilesize($filename) );

    echo 
    "<p>Contents of $filename:</p>\n";
    echo 
    "<p class=border>$file_data</p>\n";

    fclose$file_handle );

    ?>

    </body>
    </html>
  2. Save the file in your space as file_reader.php and view it in a browser.

  3. Now try changing the content written to your datafile as you have set in file_writer.php; see if it changes in file_reader.php.

Using Comma Separated Values (CSV) files

Files can be formatted in various ways, many of which are useful to aid in reading back into a script for automatic parsing. One such format is Comma Separated Values (CSV). CSV files are plain text files that store tables of data. Each line is a successive row of the table, and each field in a row is separated from the others by a comma. You can use other characters such as semicolon as the delimiter. CSV are popular formats for data as they are easy to read and understand. Many popular programs such as Microsoft Excel are able to import/export in CSV.

PHP includes functions such as fgetcsv() and fputcsv() that help you work with CSV files.

  1. Create a new file in your text editor with the following content:

    <html>
    <body>

    <?php

    $filename 
    "data/file2.csv";

    $file_handle fopen$filename"w" );

    $table = array(
        array(
    "Name","PSU-ID","HW1","HW2","HW3","HW4","MT1","HW5","HW6","HW7","HW8","MT2","HW9","HW10","FN"),
        array(
    "John Doe","900000001","36","29","39","40","189","38","26","32","35","191","36","34","200"),
        array(
    "Jane Dough","900000002","26","18","29","30","159","29","27","33","37","180","37","32","178"),
        array(
    "Jeff Felps","900000003","32","27","38","28","175","35","26","31","32","170","35","39","171"),
        array(
    "Mike Mork","900000004","34","24","33","40","189","38","26","32","35","191","36","34","200"),
        array(
    "Sally Park","900000005","40","39","40","40","190","38","40","40","38","198","39","40","200"),
        );

    foreach( 
    $table as $line ){
        if( ! 
    fputcsv$file_handle$line ) ){
            exit( 
    "Cannot write to file, $filename" );
        }
    }
    echo 
    "Successfully wrote <a href=\"$filename\">$filename</a>\n";

    fclose$file_handle );

    // since fputcsv() isn't ready yet, we'll need to write our own
    function fputcsv$handle$row$del=','$quot='"' )
    {
        
    $count 0;
        foreach (
    $row as $item){
            
    // check/correct data format
            
    $count ++;
        }
        
    fputs$handleimplode$del$row ) . "\n" );
        return 
    $count;
    }

    ?>

    </body>
    </html>
  2. Save the file in your space as csv_writer.php and view it in a browser.

  3. Next, create a new file with the following content:

    <html>
    <body>

    <p>Grades</p>
    <table border=1>

    <?php

    $filename 
    "data/file2.csv";

    $file_handle fopen$filename"r" );

    $line  0;
    while( (
    $row fgetcsv($file_handle)) !== FALSE ){
        echo 
    "<tr>";
        
    $line++;
        
    $total 0;
        
    $field_num 0;
        foreach (
    $row as $element){
            
    $field_num++;
            if(
    $field_num>and $line>1){
                
    $total += $element;
            }
            echo 
    "<td>" $element "</td>";
        }
        if(
    $line<2){
            echo 
    "<td>total</td>";
        }else{
            echo 
    "<td>$total</td>";
        }
        echo 
    "</tr>\n";
    }

    fclose$file_handle );

    ?>
    </table>
     
    </body>
    </html>
  4. Save the file in your space as csv_reader.php and view it in a browser.

  5. Extra - Edit the file you wrote in the second example, file_reader.php so that the $filename points to data/file2.csv. View it in a browser.

<- Back - Saving Data on the Server|Up |Saving Data in SQLite Database Files - Next ->

If you have any questions, feel free to ask me.

Content by: Jeff D'Angelo <jcd@psu.edu> © 2005

Show normal

Last update on: Sun Dec 28, 2008, 8:21:26 PM