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

Sam Meisler
PLUS
Sam Meisler
Courses Plus Student 7,157 Points

getting error opening data/csv/people.csv

my write_csv file:

<?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');

I get this error: fopen(../data/csv/people.csv): failed to open stream: Permission denied in /home/treehouse/workspace/inc/write_csv.php

3 Answers

Samandar Mirzayev
Samandar Mirzayev
11,834 Points

There is you have to access by console on this file with "chmod ". Remember from console foundation course

Best regards Sam

Sam Meisler
Sam Meisler
Courses Plus Student 7,157 Points

Thanks for the answer, Samander. Just have not taken the console foundation course (that I remember) so I have to go on faith that my code works. Perhaps a reminder on how to do this in the video notes, treehouse team? I'm sure lots of others take this course independently of the foundation course as well. Thanks, Sam

Samandar Mirzayev
Samandar Mirzayev
11,834 Points

Most welcome bro. If it is help. I am happy to hear that bro. Best regards Sam Mirzaev

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