Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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