The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP5 - Beginner - can't find csv data
Discuss Beginner - can't find csv data in the PHP Development forum on Dev Shed. Beginner - can't find csv data PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 12th, 2012, 10:21 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 3
Time spent in forums: 35 m 16 sec
Reputation Power: 0
|
|
|
PHP5 - 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>
|

October 13th, 2012, 12:18 AM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
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.
|

October 13th, 2012, 09:30 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 3
Time spent in forums: 35 m 16 sec
Reputation 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.
|

October 13th, 2012, 11:04 AM
|
 |
Lost in code
|
|
|
|
|
It should create it if the PHP script has write access to the parent directory.
|

October 13th, 2012, 05:58 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
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.
|

October 13th, 2012, 06:17 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 3
Time spent in forums: 35 m 16 sec
Reputation 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.
|

October 13th, 2012, 06:45 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|