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

Denny Schouten
Denny Schouten
8,246 Points

writing-csv what is wrong with my code? Permission denied

What it suppose to do is to add a line in the people.csv (location root/data/csv/people.csv) from the form(see below) however I get the Warning: fopen(../data/csv/people.csv): failed to open stream: Permission denied in /home/treehouse/workspace/inc/write_csv.php on line 11 line 11 is if (($fh = fopen('../data/csv/people.csv', 'a+' )) !== false) { Please halp!?

this is my write_csv.php file, location root/inc/write_csv

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

this is the add_person.php page, location root

<?php
/**
 * Form for adding more people to the csv file
 * first,last,website,twitter,img
 */
$title = 'Add Person';
require 'inc/header.php';
?>
<form method="post" action="inc/write_csv.php">
    <div class="form-group input-group-lg">
        <label class="control-label" for="first">First Name</label>
        <input class="form-control" type="text" name="first" id="first" />
    </div>

    <div class="form-group input-group-lg">
        <label for="last">Last Name</label>
        <input class="form-control" type="text" name="last" id="last" />
    </div>

    <div class="form-group input-group-lg">
        <label for="website">Website</label>
        <div class="input-group input-group-lg">
            <span class="input-group-addon" id="website-addon">http://</span>
            <input class="form-control" aria-describedby="website-addon" type="text" name="website" id="website" />
        </div>
    </div>

    <div class="form-group input-group-lg">
        <label for="twitter">Twitter</label>
    <div class="input-group input-group-lg">
        <span class="input-group-addon" id="twitter-addon">@</span>
        <input class="form-control" aria-describedby="twitter-addon" type="text" name="twitter" id="twitter" />
    </div>
    </div>

    <div class="form-group input-group-lg">
        <label for="img">Image</label>
        <input class="form-control" type="text" name="img" id="img" placeholder="http://" />
    </div>

    <button class="btn-lg btn-default" type="submit"><?php echo $title; ?></button>
</form>
<?php
require 'inc/footer.php';
Victoria Loisi
Victoria Loisi
7,070 Points

Hi there! Have you tried changing the permissions of the csv file before fopen()? Have a look at: http://php.net/manual/en/function.chmod.php Hope it helps!

Clare A
Clare A
23,994 Points

I'm having the same issue. I went back and looked at the notes on file permissions then checked it using fileperms() - the response was "rw-r--r--" - so that suggests that I do have read and write permission (unless I'm not the owner??). Nevertheless I tried updating it anyway using chmod() as Victoria suggests. Result was "chmod(): Operation not permitted".

Hoping someone chimes in with an answer :-(

1 Answer

Alena Holligan
Alena Holligan
Treehouse Teacher

I have updated both the workspace and the Teacher's notes in case people still have issues