Lunarpages Web Hosting Forum

Author Topic: Writing to file with PHP - noob with problem  (Read 2715 times)

Offline 6string

  • Pong! (the videogame) Master
  • *****
  • Posts: 20
Writing to file with PHP - noob with problem
« on: June 13, 2004, 01:00:27 PM »
Greets lunarpeople,

I'm a bit of a noob with PHP, and was wondering if someone could help me out with a little problem I'm having.

I have a small .php file that I am using to write to a text file.  All of this is done on the server side and I have permissions set on the file for writing as well as reading.  When the script runs, I get a page listing errors, starting with something that looks like this...

Quote
Warning: fopen(airhead_fs9_deke.txt): failed to open stream: Permission denied in /home/.../airworthy/download/dl.php on line 17


Anyone here doing charity work and can give me some direction as to what is going wrong?
Regards,
Dave

Offline TranzNDance

  • Princess of Naboo
  • Berserker Poster
  • *****
  • Posts: 11567
    • Thu Tu's Blog
Writing to file with PHP - noob with problem
« Reply #1 on: June 13, 2004, 01:39:36 PM »
Could you please show us what is on line 17, and a few lines above and below it?

Also, /home/.../airworthy/download/dl.php  what does the ... signify? Is it username/public_html ?

What exactly is the CHMOD value that you have for the file? It would be a 3-digit number with values of 5, 6, or 7.
:whip: :love: :whip: :love: :whip: :love:

Offline 6string

  • Pong! (the videogame) Master
  • *****
  • Posts: 20
Writing to file with PHP - noob with problem
« Reply #2 on: June 13, 2004, 02:14:59 PM »
Ok, there are two parts, one is invoked if the txt file doesn't exist, the other is invoked if it it does.  Very similar in code.  Anyway...

This is what I am using in my href:
http://airhead.hfdd.com/airworthy/download/dl.php?file=airhead_fs9_deke&type=.zip

This is the first few lines of PHP file:
$FILE=$_GET[file];
$TYPE=$_GET[type];


Starting at line 17 (lines above this are for if file already exists, both produce same error)
if (!file_exists("$FILE.txt"))
{
$_file=fopen("$FILE.txt","w+");
$contents=fread($_file,filesize("$FILE.txt"));
settype($contents, "integer");
fclose($_file);
$_file2=fopen("$FILE.txt","w+");
$hits=fwrite($_file2,$contents+1);
fclose($_file2);
chmod("$FILE.txt",0777);
}


Yes, the ... is there to abbreviate for posting.

In so far as CHMOD, I had set it by hand thru the CPanel, but now tweaking it in the PHP file.  From what I know, it would need to be 777 for full read/write permissions.

This whole thing seems like it should be relatively simple, but I'm a greenhorn, so what do I know?  I would actually like to write this info to the mysql db, but figured it might be easier to start with a text file first.  I have tried setting all this up locally for experimentation, no bones.  Got Apache running with PHP, but can't get mysql to work and play well with the other kids.

Any assistance is greatly appreciated. :)
Regards,
Dave

Offline TranzNDance

  • Princess of Naboo
  • Berserker Poster
  • *****
  • Posts: 11567
    • Thu Tu's Blog
Writing to file with PHP - noob with problem
« Reply #3 on: June 13, 2004, 03:10:37 PM »
What will $_GET[file], and consequently, $FILE, be? Would it be filename.txt? Because here:
Code: [Select]
if (!file_exists("$FILE.txt")) it seems redundant... like it would be filename.txt.txt :?

If you're not sure, add this:
Code: [Select]
echo $FILE;
:whip: :love: :whip: :love: :whip: :love:

Offline 6string

  • Pong! (the videogame) Master
  • *****
  • Posts: 20
Writing to file with PHP - noob with problem
« Reply #4 on: June 13, 2004, 03:35:56 PM »
Well, $FILE is actually just the file name, not including the extension.  The extension for the actual downloaded file is in a separate variable, $TYPE (see href line in prev post).  I'm just taking the $FILE variable value and adding the .txt file extension to it.  The text file (using same name as dl file with .txt extension) will contain a number based on downloads, and will be incremented everytime someone grabs the file.

In truth, I think the PHP code is ok (not boosting my own ego, I had help with it).  The more I think about it, the more it seems this is some kind of file access and permissons issue, but I really don't know.

All I'm really trying to do here is track file downloads (you probably figured that out already).  I figured writing to a text file would be easier for my first attempt, my utopian dream here would be to have the download file name and download count logged in my db.  As I can't seem to get the text file thing working, I must admit I am a bit afraid of even messing with the db at this point.
Regards,
Dave

Offline 6string

  • Pong! (the videogame) Master
  • *****
  • Posts: 20
Writing to file with PHP - noob with problem
« Reply #5 on: June 14, 2004, 02:50:33 AM »
TranzNDance,

Just wanted to post and say thanks for helping out.  After many hours of pounding my head on my keyboard (a bit hard on the keyboard, lucky  for me I have a thick skull), I have managed to get writing/updating the data in my db working, therefore bypassing the need to write to the text file.  Regardless, I do appreciate you taking the time to assist me. :)

Regards,
Dave
Regards,
Dave

Offline TranzNDance

  • Princess of Naboo
  • Berserker Poster
  • *****
  • Posts: 11567
    • Thu Tu's Blog
Writing to file with PHP - noob with problem
« Reply #6 on: June 14, 2004, 02:56:51 AM »
Quote from: 6string
TranzNDance,

Just wanted to post and say thanks for helping out.  After many hours of pounding my head on my keyboard (a bit hard on the keyboard, lucky  for me I have a thick skull), I have managed to get writing/updating the data in my db working, therefore bypassing the need to write to the text file.  Regardless, I do appreciate you taking the time to assist me. :)

Regards,
Dave

You're welcome. :)

It's best to write to a dB anyway. :D
:whip: :love: :whip: :love: :whip: :love: