logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Coding & Scripting Corner View previous topic :: View next topic
Reply to topic   Topic: PHP cannot create/write files
Author
drmed



Joined: 22 May 2006
Posts: 6

PostPosted: Tue 23 May '06 20:20    Post subject: PHP cannot create/write files Reply with quote

I installed PHP Version 5.1.4 on Apache SSL 2.2.2 fom Apachelounge.com on my Windows XP PRO SP2 and my server adds a little php script to track hits. The php script would add a hit to a text file anytime a page gets called. The problem is, I guess, that php cannot write to that file because the counter wouldn't progress after hits. even if I delete the tracking file, the script cannot create the file. Under linux the script was running fine with the same settings. Can somebody help? Since Windows doesn't have user rights, how can I give permission to php create files?
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7288
Location: Germany, Next to Hamburg

PostPosted: Tue 23 May '06 20:41    Post subject: Reply with quote

Do you use fopen for the script? If yes use 'a' as second parameter, so the file will be created if not exfist

i.e.
Code:

$m_user='user.txt';
$f=fopen($m_user, 'a');
fputs($f,$input ."\n");
fclose($f);
Back to top
drmed



Joined: 22 May 2006
Posts: 6

PostPosted: Tue 23 May '06 20:51    Post subject: Reply with quote

what about php not being able to write to existant files? I don't think it is a script problem, since the same scripts were working on linux. I guess it is more a php setup or maybe an access right problem.
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7288
Location: Germany, Next to Hamburg

PostPosted: Tue 23 May '06 21:00    Post subject: Reply with quote

if you don't post the original error message I can only guess.
Do you use fopen? You didn't told me! Post the script! Without informations I still guess.
Normaly PHP can write files in Windows. What about the permissions? Set by the script? In Windows you have to use 0666 and not 0777. If you use 0777 the file will under Windows not writeable only readable.
Was the textfile importet from linux or created on Windows`?

Another reason an be, if you run Apache as service maybe the User under the service is running has not the permissions to write files.
Back to top
pnllan



Joined: 05 Dec 2005
Posts: 221

PostPosted: Tue 23 May '06 22:03    Post subject: Reply with quote

Potentially, could be a '$path' statement (in the PHP script) that is still pointing to the old path that you used in Linux.

drmed,
You act like you know the answer to your question, but yet you still ask. Why?
James tries to get the ball rolling by asking a good question, and you dismiss him saying that the script is not the problem. Are you absolutely sure?

Let's see - by your own admission you said:
    -that you had been running this script on a Linux box without problems [HINT]
    -now, it is not functioning under Windows [HINT]

How does your script know where to find or write a new file - PFM (pure freaking magic)?

Maybe someone can help me - I'm in a place with no light. Is it that there is no light or are my eyes giving me trouble? It must be my eyes because I used to be able to see light. That's logic...
..
.
Back to top
drmed



Joined: 22 May 2006
Posts: 6

PostPosted: Wed 24 May '06 4:02    Post subject: Reply with quote

Excuse my ignorance but how do I set permissions for folders/files under windows xp pro? Under linux I used CHOWN/CHMOD but no clue under XP. Can you help me how to change permissions to 0666?

I will post the script tomorrow since I am currently not home and don't have access to my server.

The text file has been created on linux and has been copied to windows but even after deleting the file, the script still cannot create a file under windows.

My php.ini shows to C:\PHP\php.ini, so I guess it is reading the right ini file.

Dear pnllan, there is no need to be a smart%$%. I was just trying to logically figure out what is going on. I didn't dismiss anybodies help either. All I said was working scripts under linux stopped working under windows. Therefore there is a different configuration/user access setting that has to be changed and I have no clue where/what this is.
Back to top
drmed



Joined: 22 May 2006
Posts: 6

PostPosted: Wed 24 May '06 22:22    Post subject: Reply with quote

Here is the script:

<?php
$COUNT_FILE = "count_data.txt";
$IMG_DIR_URL = "./digits/";
$NB_DIGITS = 4;

if (file_exists($COUNT_FILE)) {
$fp = fopen("$COUNT_FILE", "r+");
flock($fp, 1);
$count = fgets($fp, 4096);
$count += 1;
fseek($fp,0);
fputs($fp, $count);
flock($fp, 3);
fclose($fp);
} else {
echo "Can't find file, check '\$file' var...<BR>";
exit;
}

chop($count);
$nb_digits = max(strlen($count), $NB_DIGITS);
$count = substr("0000000000".$count, -$nb_digits);

$digits = preg_split("//", $count);

for($i = 0; $i <= $nb_digits; $i++) {
if ($digits[$i] != "") {
$html_result .= "<IMG SRC=\"$IMG_DIR_URL$digits[$i].gif\">";
}
}

echo $html_result;
?>

I sincerely think there are some access right issues that I cannot figure out since bbclone or myphpadmin scripts cannot write either.
Back to top
pnllan



Joined: 05 Dec 2005
Posts: 221

PostPosted: Thu 25 May '06 18:46    Post subject: Reply with quote

Use Windows Explorer and navigate to the folder containing 'count_data.txt'. Right Click the file and select Properties. If the file is set to read only then uncheck and give it a try.

As far as phpmyadmin goes, if you are using MySQL 4.01 or higher you should use the MySQLi extension - according to the phpmyadmin documentation. I have used phpmyadmin for along time on Windows, and have NEVER had an issue with FILE PERMISSIONS.

I have no expereince with bbclone.

You have three distinct issues (writing to the count file, inop phpmyadmin, and bbclone)...Anything else not right?
Back to top
Tester



Joined: 17 Apr 2007
Posts: 2

PostPosted: Tue 17 Apr '07 22:55    Post subject: the same problem Reply with quote

I have the same problem

im searching for a solition

my files was in godady server
the link of the files

http://www.socoool.com/baraha/guests/index.php?id=show


and i copy (transfer) the files to other server " hostmonster"
but the file cant read the data from datafile

http://baraha.ae/guests/index.php?id=show

i wanna to slove the problem bacouse I pay for tow hosts now

any one know the solution?

<sorry for my bad english >
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7288
Location: Germany, Next to Hamburg

PostPosted: Wed 18 Apr '07 9:36    Post subject: Reply with quote

Can you post a snippet from your PHP code and post the exactly error message?
Back to top
Tester



Joined: 17 Apr 2007
Posts: 2

PostPosted: Wed 18 Apr '07 19:07    Post subject: Reply with quote

this is the snippet

http://www.socoool.com/test/guests.zip

you can download

i like to finsh this problem earlier
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7288
Location: Germany, Next to Hamburg

PostPosted: Thu 19 Apr '07 10:17    Post subject: Reply with quote

Hello Tester!
I wasn't able to read the code! The many many letters are in a language my system couldn't read. Of cause I could read the html, but I wasn't able to read the name of the variables, name of input fields and so on.

Than you work without $_POST or $_GET variables. So I guess you work with register globals = on. Confused But even if I turn that on. I don't see input fields.

Note there is missing include "../header2.htm" ;

Maybe you can use this existing source
Code:

$m_user='user.txt';
//if(!file_exists($m_user)){$fh=fopen($m_user, 'w'); fclose($fh);}

$ref=$_SERVER['HTTP_REFERER'];
$user=$_SERVER['HTTP_USER_AGENT'];
$where=$_SERVER['REQUEST_URI'];
$when=date("D M d, Y G:i");

if($_SERVER['HTTP_X_FORWARDED_FOR'] != ""){
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
$proxy = $_SERVER["REMOTE_ADDR"];
$host = @gethostbyaddr($_SERVER["HTTP_X_FORWARDED_FOR"]);
}else{
$ip = $_SERVER["REMOTE_ADDR"];
$host = @gethostbyaddr($_SERVER["REMOTE_ADDR"]);
$proxy = "none";
}

$input =$when;
$input .=" on page:";
$input .=$where;
$input .=" came from: ";
$input .=$ref;
$input .=" Visitor: ";
$input .=$ip;
$input .="  ";
$input .=$user;
$input .="  Proxy: ";
$input .=$proxy;
$input .="  Host: ";
$input .=$host;

$f=fopen($m_user, 'a');
fputs($f,$input ."\n");
fclose($f);
?>
Back to top
awatech



Joined: 12 May 2007
Posts: 1

PostPosted: Sat 12 May '07 11:15    Post subject: Same problem! Reply with quote

Hi,
Seem to be facing the same problem..

Using php i can fread on my pc's server but fwrite doesn't work.
However, i upload the code to the webhost and the same thing works perfectly.

How to fix it?


Perhaps the windows file permission is causing the error, its set to Everyone right now. I searched everywhere, people say that there is usually no need to change permission for folders if you are using apache on windows, if thats the case, what can be the problem?
Back to top
tdonovan
Moderator


Joined: 17 Dec 2005
Posts: 611
Location: Milford, MA, USA

PostPosted: Sat 12 May '07 19:20    Post subject: Reply with quote

hmmmmm.... this is the second reply saying "I have the same problem"

Since the original poster refused to tell us the error message, I don't understand how you know it is really the same?

Please post the error you receive. Look for error messages on your browser, and also in the Apache error.log file.

In order to see useful error messages on your browser, make sure your php.ini file contains the line:
Code:
display_errors = On


Thanks,

-tom-
Back to top


Reply to topic   Topic: PHP cannot create/write files View previous topic :: View next topic
Post new topic   Forum Index -> Coding & Scripting Corner