/    Sign up×
Community /Pin to ProfileBookmark

I’ve got a php page grabbing news storys from a csv file. Yes, I know I could/should use a database addition like MySQL, but i’m not using it.

[code=php] $handle = fopen(‘data/news.csv’, ‘r’);
$newsline = “0”;
if ($newsnum == “”)
{
$newsnum = “5”;
}
if ($loggedinyn == “1”)
{
$data = (fgetcsv($handle, 0, ‘,’));
while ($newsline != $newsnum && $data[0] != FALSE)
{
echo “*****”;
echo $data[0];
echo “*****”;
echo $data[1];
$data = (fgetcsv($handle, 0, ‘,’));
$newsline = $newsline + 1;
}
}
fclose($handle);[/code]

$loggedinyn is set to 1 when the user logs in.
If they’ve logged in, then $newsnum is set by their preferences to the number of news stories to show.
$newsline is set to zero, and added to every time it goes through the while loop.
Last, the two parts where it says echo “*****”; I had a rather long amount of text in them, so in the interest of fitting everything into this post, I omitted it.

Ok, so no problems, it works. Why am I posting? Well, when I tryed changing “$handle = fopen(‘data/news.csv’, ‘r’);” from read mode (r) to read+write (r+) it told me:

[quote]

Warning: fopen(data/news.csv) [function.fopen]: failed to open stream: Permission denied in C:Documents and Settings****My Documentsweb server****index.php on line 117

Warning: fgetcsv() expects parameter 1 to be resource, boolean given in C:Documents and Settings*****My Documentsweb server****index.php on line 125

Warning: fclose(): supplied argument is not a valid stream resource in C:Documents and Settings*****My Documentsweb server****index.php on line 136

[/quote]

So that’s not quite what I was looking for…. Any ideas what I did wrong?

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@cridleyJul 08.2006 — sorry, this might sound a wee bit obvious, but do you have write permissions to the file in question? (the last two errors are only a consequence of the first, ie it cannot open the file to write)
Copy linkTweet thisAlerts:
@AmazingAntauthorJul 08.2006 — The computer that the file is stored on, happens to be about 13 inches away from me at this very moment. The user who's documents folder contains the file, is my username, and i'm logged on. I've changed the file about 10 times in the last 5 minutes, so I doubt that there is any lack of write permissions to the file. I've already got this php page writing to normal txt file, and i was just hoping that I could write to a csv easilly... guess not.

Actually, to be more specific, while my hand is on the mouse my hand is about 3 inches away from the computer... not that it matters.
Copy linkTweet thisAlerts:
@cridleyJul 08.2006 — fine. just trying to be helpful dude. a csv file is just a normal text file, so not really sure whats going on there, haven't tried your code here.

just tried

<i>
</i>$handle = fopen('news.csv', 'r+');

fwrite($handle, "some text");

<i> </i>fclose($handle);


works fine here
Copy linkTweet thisAlerts:
@NogDogJul 08.2006 — If it's a Linux box, the webserver user under whom your PHP scripts are run might not have write permission on the file: as it is not the same user as your login account. (I'm not sure how different flavors of Windows/IIS handle that, so I won't comment on the possibilities there.)

You can do a quick check with something like:
[code=php]
$file = 'data/news.csv';
if(file_exists($file))
{
if(is_writable($file))
{
// continue with rest of your code
}
else
{
echo "ERROR: you do not have write permission on file $file";
}
}
else
{
echo "ERROR: file $file was not found";
}
[/code]
Copy linkTweet thisAlerts:
@AmazingAntauthorJul 08.2006 — Unfortunatly cridley, you were writing text to the file with it as a text file, and not a csv. I'm not an expert, so i don't know if it makes a difference or not. However, I can't be sure i'm writing the information correctly unless i'm writing to it as a csv. as to nogdog, it's my main home computer running WinDoze Xp. Since i'm not the only user of the computer, I havn't tryed Linux yet. I probably should, but i'm gonna have to wait a little bit. I ran the check you suggested, and according to it, there's no problem. But for some reason, it still doesn't want to write to it the file once i tell it that it's a csv.
Copy linkTweet thisAlerts:
@NogDogJul 08.2006 — Do you have the file open in another application when you run your script? (It could be a problem with that application locking the file for writing.)
Copy linkTweet thisAlerts:
@cridleyJul 08.2006 — seing as how it's the fopen thats failing, the writing part doesn't even enter the equation
Copy linkTweet thisAlerts:
@AmazingAntauthorJul 08.2006 — Well, I don't see any changes to the file, because I havn't made any yet. But somehow, it's accessing the file in r+ mode, and I found that the problem writing to the file is that when I tell it to write something to $data[X] it is in fact writing the data. it's just not writing the array back into the csv. Any idea how I can write the text back into the csv?


Ahh, I pressed submit and then your post came up... Yea, like I said, I don't know what changed but it's working. The fopen problem just.... vanished.

Oh, NogDog's post just appeared too. I think that was the problem. I'm not quite sure, but I might have had it open in excel still. That would be a very good explanation for why it wasn't working though.
×

Success!

Help @AmazingAnt spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...