files-csv.php

<?php
    // Open a file for reading
    $res = fopen("data/sample-data.txt", "r");
    // Check to make sure that the file opened successfully
    if ($res === false)
        die("Could not open file");
    
    // Read the full contents of the file (filesize() bytes of data)
    $filecontents = fread($res, filesize("data/sample-data.txt"));
    
    // Write the contents string $filecontents to a file
    // Remember: Apache (or our web server or PHP fpm) must have permissions
    //           to write to the directory / file.  If not, we must allow.
    // Note: It is a security risk to make a directory served by the web server
    //       world writable.  It is encouraged to use a directory that is not
    //       served for our files.  In this case, we're writing to a data
    //       directory one level above (..) our current directory, making it
    //       outside of our public_html directory, and therefore not served
    //       by Apache.
    file_put_contents("../data/copy-text.txt", $filecontents);
    
    // Always close file resources
    fclose($res);
    
    // Read in a CSV from Github and format it using the first row of the file
    // as the headers.  That is, the first row will become the keys in our
    // associative array for each row.
    $data = [];
    $headers = [];
    if (($fh = fopen("https://raw.githubusercontent.com/snac-cooperative/data-samples/master/example_resources.csv", "r")) !== false) {
        $cr = 1;
        while (($row = fgetcsv($fh, 10000, ",")) !== false) {
            if ($cr++ == 1)  {
                $headers = $row;
            } else {
                $newRow = [];
                foreach ($row as $k => $v) {
                    $newRow[$headers[$k]] = $v;
                }
            
                array_push($data, $newRow);
            }
        }
        fclose($fh);
    }
    
    // var_dump provides detailed variable information
    var_dump($data);
    
    // print_r prints array data only
    print_r($data);
    
    // We can loop over the array and print it out nicely to the user as HTML
    foreach ($data as $item) {
        echo "<p>{$item["title"]}</p>\n";
    }
    
    // To write an array as CSV, use fputcsv for each ROW of our 2D array
    if (($fh = fopen("../data/resources.csv", "w")) !== false) { // create if not exists
        foreach ($data as $item) {
            fputcsv($fh, $item);
        }
        fclose($fh);
    }

    // Write it out to the user as JSON using json_encode() to turn the 
    // associative array into a JSON array/object.  Here, we may want to make
    // sure the header of the HTTP response lets the browser know that the
    // type of data is JSON (not HTML).  We can modify the headers with the
    // header() function.
    // NOTE: To change the header, it *must* be done *before* any text is
    //       written to standard out (i.e., sent/displayed to the user).  In
    //       class, we had commented out everything above so that this was the first
    //       line before any echo or print statements.
    header("Content-Type: text/json");
    echo json_encode($data, JSON_PRETTY_PRINT);

sample-data.txt

"The Force will be with you. Always."
- Obi-Wan Kenobi