Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Writing CSV6:04 with Alena Holligan
In this video we take data from a form to added another person to our recommended list. We'll be appending our file to add an additional line to our CSV file.
If you are not able to write to the file, make sure the file permissions are set
- Open the console: View > Show Console
- Run: chmod 777 data/csv/people.csv
It's usually a good idea not to rely on 3rd parties to store important data for you like images. They are not required to keep that information up just so you site works properly. If YOU are purposefully storing files with a 3rd party like a CDN (content delivery network), that is perfectly valid. I have downloaded the Twitter images into the project "img" folder for the download and workspaces, I also changed the links in the CSV to point to the local copies of the files. If you want to add your own people and not rely on a 3rd party to host the image, make sure you download those images locally as well.
Properly Formatted CSV
A properly formatted CSV file will end with an empty line, but you shouldn't count on that.
fputcsv – formats a line (passed as a fields array) as CSV and write it (terminated by a newline) to the specified file handle. Only the first two parameters are required.
handle The file pointer must be valid, and must point to a file successfully opened by fopen() or fsockopen() (and not yet closed by fclose()).
fields An array of values.
delimiter The optional delimiter parameter sets the field delimiter (one character only). DEFAULT is comma. If you wish to save as tab delimited, you would use
enclosure The optional enclosure parameter sets the field enclosure (one character only). Treats the characters within the enclosed characters as a single item string. DEFAULT double quote. To use a single quote
escape_char The optional escape_char parameter sets the escape character (one character only).
$array = [ 'one 1', 'two ', 'three, "3"' ]; $fh = fopen('test.txt', 'w'); fputcsv($fh, $array); fputcsv($fh, $array, "\t", "'", "\\"); fclose($fh); //results //Line 1: "one 1","two's ","three, ""3""" //Line 2: 'one 1' 'two''s ' 'three, "3"'
fseek – Sets the file position indicator for the file referenced by handle. The new position, measured in bytes from the beginning of the file, is obtained by adding offset to the position specified by whence.
fopen – binds a named resource, specified by filename, to a stream.
fgets – Gets a line from file pointer.
fputs – Alias of fwrite(), which writes the contents of string to the file stream pointed to by handle.
fclose – closes the file pointed to by handle.
So reading CSV files is handy and 0:00 we were able to use that data to display a suggestion page. 0:02 But what if I want to add more suggestions? 0:07 I'm always discovering new things and meeting new people. 0:09 So, I'm sure that I will have more suggestions in the future. 0:12 We could open the CSV file directly, but 0:16 that wouldn't work if I wanted to fill out a form that adds a person. 0:18 So let's look at using PHP to add an additional line to our CSV file. 0:23 Let's create a new file in the inc directory. 0:28 We'll name this write_csv.php. 0:33 We'll start by creating an array for the new contact. 0:41 We'll be using the form data from our add person form. 0:53 So, we want to filter our input. 0:57 First. 1:07 FILTER_SANTIZE_STRING, Last website, Twitter and image. 1:09 The website and the image will actually be URLs. 1:29 So, we can change this to FILTER_SANITIZE_URL. 1:32 Now use fopen to open our CSV file. 1:44 Once again, wrapping it in an if statement. 1:49 We'll need to go up one level before we enter data/csv/people.csv. 1:59 This time we'll use a for the mode meaning append. 2:09 It opens the file and places the file pointer at the end of the file. 2:13 And fclose. 2:26 We can now use fputcsv. 2:32 Passing the file handler just like we did for the fgetcsv. 2:36 This time we also passed the new array. 2:40 And we'll finish off this file by redirecting to the people page. 2:46 Let's give this a try in the browser. 2:59 Go to Add Person, and fill out the form. 3:02 I'm going to add the Treehouse CEO, Ryan Carson. 3:07 He's a great person to follow. 3:09 Let's grab his profile picture from Twitter. 3:18 If you right click, you can copy image address. 3:24 We were redirected to the People page, but it didn't show up right. 3:36 So, let's take a look at the CSV file. 3:40 We can't read CSV files in Workspaces, so we need to rename it first. 3:43 We'll change this to txt. 3:55 The problem is that there is no new line between the last person and Ryan. 4:01 If the file doesn't end on a new line when we try to append, this could be a problem. 4:06 Let's delete this edition and rename the file again. 4:11 I'm going to show you how to check for a new line. 4:16 So, let's make sure this does not end on a new line. 4:19 First, we need to be able to read, as well as write. 4:30 So, let's add a plus to the mode. 4:34 Before we call fputcsv, we're going to use the fseek function. 4:37 This will allow us to move the file pointer. 4:47 The fseek function accepts three parameters, the file handler, 4:50 the offset, which in this case we want to move back one, so 4:54 -1, and wents, which tells the point where you are starting from. 5:00 We'll use SEEK_END. 5:06 Because we want to go back one byte from the end of the file. 5:11 Now, we can check if our file ends in a new line. 5:15 And if not, add a new end of line character. 5:18 If fgets. 5:24 Is not equal to the end of line. 5:28 Then we're going to fputs. 5:38 A new end of line. 5:44 Let's go back to the browser and resubmit the form. 5:47 This time, our people page shows the new suggestion 5:58 properly because it was placed on its own line. 6:01
You need to sign up for Treehouse in order to download course files.Sign up