Question

Hi stackoverflow community!

I need a bit of help as to where i'm going wrong here.

Situation: On a site i'm building, I have a section with a form that the user fills out with name, food choice, and allergies. I'm using php 5.5 on IIS 7 and have a PHP page that the form submits to called form.php. Once they fill in the form and submit, it brings them to the php page where it says thank you for submitting, waits 5 seconds and returns them to the previous page.

I've confirmed that the posted values and keys are gathered no problem by outputting them into variables and echoing on screen, as well as using var_dump($_POST);

Here is my form code:

<form action="form.php" method="post">
    <p>First Name: <input type="text" name="fname"></p>
    <p>Last Name: <input type="text" name="lname"></p>
    <p>Food selection:
        <select name="food">
            <option value="no_selection">---Please Select---</option>
            <option value="chicken">Chicken</option>
            <option value="beef">Beef</option>
            <option value="corn">Corn</option>
        </select>
    </p>
    <p>Allergies:<input type="text" name="allergies"></p>
    <hr />
    <input type="submit" value="Submit" />
</form>

And here is my PHP code to output to CSV:

<?php

  $fname = $_POST['fname'];
  $lname = $_POST['lname'];
  $fullname = $_POST['fname']." ".$_POST['lname'];
  $food = $_POST['food'];
  $allergies = $_POST['allergies'];

  $currentData = array($fname,$lname,$food,$allergies);

  $file=fopen('food.csv', 'a');
  fputcsv($file, $currentData);
  fclose($file);
?>

No file is created or written to, and when i've created the file manually and placed it in the dir it still doesn't write to it and it does have full permission to the file. I've checked the server and it's not a file lock issue either.

Any help is greatly appreciated! Let me know if there's anything else I should write to help.

Thanks in advance!

EDIT with ANSWER:

Great! Thank you to Darragh for the referral to the is_writable() function. This helped me understand what i did and did not have permission to write to as the php user (IUSR in IIS).

Thank you also to both greg0ire and Spechal for the tips about debugging with the error log settings.

The answer was definitely a permission issue, as I had the IUSR as read/write for the root of the folder, but inheritance was turned off so the subfolders and files did not have access.

On top of the permission issue, Spechal's mention of imploding the data before writing it was a good one (but wasn't required for my purpose as fputcsv did all the work with relation to comma separation) especially if you want to change the delimiter.

Thank you everyone for the fast and helpful responses!

Was it helpful?

Solution

Check the following for development:

In php.ini: display_errors = On error_reporting = E_ALL | E_STRICT

You need to check to make sure the path you are writing to is in fact writable. You should use the absolute path instead of a relative path also.

You may also want to implode your $currentData with a comma before writing it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top