Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP File Handling with PHP Parsing Specific Formats Writing CSV

My write_csv.php is not writing

<?php

$new_person = [ filter_input(INPUT_POST, 'first', FILTER_SANITIZE_STRING), filter_input(INPUT_POST, 'last', FILTER_SANITIZE_STRING), filter_input(INPUT_POST, 'website', FILTER_SANITIZE_URL), filter_input(INPUT_POST, 'twitter', FILTER_SANITIZE_STRING), filter_input(INPUT_POST, 'img', FILTER_SANITIZE_URL) ];

if(($fh=fopen('../data/csv/people.csv', 'a+')) !== false){ fseek($fh, -1, SEEK_END); if(fgets($fh) !== PHP_EOL){ fputs($fh, PHP_EOL); } fputcsv($fh, $new_person); fclose($fh); } header('location: /people.php');


why is this not writing?

I have exactly the same problem. My code is the same as in the video:

<?php

$new_person = [ filter_input(INPUT_POST, 'first', FILTER_SANITIZE_STRING), filter_input(INPUT_POST, 'last', FILTER_SANITIZE_STRING), filter_input(INPUT_POST, 'website', FILTER_SANITIZE_URL), filter_input(INPUT_POST, 'twitter', FILTER_SANITIZE_STRING), filter_input(INPUT_POST, 'img', FILTER_SANITIZE_URL) ];

if (($fh = fopen('../data/csv/people.csv', 'a+')) !== false ) { fseek($fh, -1, SEEK_END); if (fgets($fh) != PHP_EOL) { fputs($fh, PHP_EOL); } fputcsv($fh,$new_person); fclose($fh); } header('location: /people.php');

2 Answers

Ray Knag
Ray Knag
8,110 Points

This same thing happened to me and I realized that like in the previous lesson, you need to allow write permissions for people.csv. To do this, you'll need to open up the console, type "cd data/csv" to get to the right sub-directory, then type "ls -l" to view current permissions which will probably be "-rw-r--r--" for people.csv. This says that the user can write to this file, but the group and others can't. Since this is just for practice, you can enable full permissions for group, user, and other by finally typing "chmod ugo+rwx people.csv". Then do "ls -l" again and it should display full permissions "-rwxrwxrwx". You'll then be able to resubmit the form and the new individual should appear at the bottom of your page.

Hope this helps!

Sebastian Pikand
Sebastian Pikand
13,987 Points

This solves the issue. Why wasn't it included in the video, like...? :D

Alena Holligan
STAFF
Alena Holligan
Treehouse Teacher

I have updated the workspace and also added to the Teachers Notes, if you have permission denied or are not able to write, you may go to View > Show Console then

chmod 777 data/csv/people.csv