#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Beginner - can't find csv data


    Hi, Just started my first tutorial on php and I don't know what i am doing wrong. My form goes to my thank you page as if it worked but i don't see the csv file created in the www directory. What directory should it be in?

    I am using ubuntu 10.04 with LAMP

    Thanks!

    My turorial code::

    PHP Code:
    <?php
    if($_POST['formSubmit'] == "Submit")
    {
        
    $errorMessage "";
        
        if(empty(
    $_POST['formMovie']))
        {
            
    $errorMessage .= "<li>You forgot to enter a movie!</li>";
        }
        if(empty(
    $_POST['formName']))
        {
            
    $errorMessage .= "<li>You forgot to enter a name!</li>";
        }
        
        
    $varMovie $_POST['formMovie'];
        
    $varName $_POST['formName'];

        if(empty(
    $errorMessage)) 
        {
            
    $fs fopen("mydata.csv","a");
            
    fwrite($fs,$varName ", " $varMovie "\n");
            
    fclose($fs);
            
            
    header("Location: thankyou.html");
            exit;
        }
    }
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "***"> 
    <html>
    <head>
        <title>My Form</title>
    </head>

    <body>
        <?php
            
    if(!empty($errorMessage)) 
            {
                echo(
    "<p>There was an error with your form:</p>\n");
                echo(
    "<ul>" $errorMessage "</ul>\n");
            } 
        
    ?>
        <form action="index.php" method="post">
            <p>
                What is your favorite movie?<br>
                <input type="text" name="formMovie" maxlength="50" value="<?=$varMovie;?>" />
            </p>
            <p>
                What is your name?<br>
                <input type="text" name="formName" maxlength="50" value="<?=$varName;?>" />
            </p>                
            <input type="submit" name="formSubmit" value="Submit" />
        </form>
    </body>
    </html>
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,295
    Rep Power
    9400
    You need to create the file yourself first. Empty. Then change the permissions on it so it is world-writable. If you need to give whatever you use (chmod, FTP program) a number it is 0666.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0
    That was it, thanks!

    Code:
    sudo chmod 0666 mydata.csv
    The instructions had said that opening the file in "a" mode would create it. I guess that doesn't always apply depending on your system.
  6. #4
  7. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    It should create it if the PHP script has write access to the parent directory.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  8. #5
  9. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,295
    Rep Power
    9400
    Exactly, but that would require making the parent folder be world-writable. And really that only matters for when the file is first created, then afterwards the permissions on the folder won't matter. So I figured the best thing to do would be to create the file and set it so it can be written to.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0
    So can this file be outside my www directory, or not accessible by typing mywebsite.com/mydata.csv in a browser? Tried to put it in my home folder and it didn't seem to work.
  12. #7
  13. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,295
    Rep Power
    9400
    PHP Code:
    $fs fopen("mydata.csv","a"); 
    That tells fopen() to look for mydata.csv in the current directory, which may be (but isn't always!) the same directory as the script. If you move the file somewhere else then you need to update this path; if you put it in the folder above your web root (probably your "home folder") then you can use
    PHP Code:
    $fs fopen($_SERVER["DOCUMENT_ROOT"] . "/../mydata.csv","a"); 
    (DOCUMENT_ROOT is the root of your website)

    As for being web accessible, unless you do something special then any file in or under the web root will be accessible to anyone on the Internet (if they know where to look). You should put the file outside the web root to prevent this, such as in your home directory.

IMN logo majestic logo threadwatch logo seochat tools logo