简体   繁体   中英

Trouble with writing to file with php and my home server

So, I have this simple little php script. It runs and compiles fine and works the way I want it to on the machine that I coded it. I'm running it on a personal home web-server running Debian 6.0.6, 32bit. It's apache with php. And I know for a fact that php is working on the server.

    <?php
    $hitsfile = "hits.txt";                                                 #name of file
    $filehandle = fopen($hitsfile, 'r') or die ("Couldn't read file.");     #Opens file, 'hitsfile' to be read.
    $hits = fread($filehandle, 5);                                          #reads file to the introduced variable, 'hits'
    fclose($filehandle);                                                    #closes file
    $hits++;                                                                #increments the variable that it read.
    $filehandle = fopen($hitsfile, 'w') or die ("Couldn't write to file."); #opens file to be read. 
    fwrite($filehandle, $hits);                                             #writes the hits variable to file.
    fclose($filehandle);                                                    #closes file.
    echo $hits;                                                             #outputs the hits variable.
    ?>

When I access the file from the server, via a web browser, I get the "Couldn't write to file." error. So then, it's opening the file properly, and reading it. And when it opens it to write, it fails. I'm assuming this is some sort of problem with permissions or something. I'm sort of at a loss as to how to solve the issue. Any ideas? Assistance would be greatly appreciated! I've googled for a couple days now, and I can't solve the issue. I'm a php 'noob' and I'm very new to running a linux-based web-server, but hey, you gotta learn somehow. :*l

tried to check the permissions to the file? The Linux file system have a very strict permission system. Write on terminal:

ls -la /path/to/my/file.txt

This would give you your permissions on the left column. Please read this article to be sure, and check if Apache have the "write" permissions to the file. If not, use the chmod command to give Apache access to the file (or the chown command, to change the owner of this file to apache, if the owner of this file have writing permissions).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM