简体   繁体   English

用PHP和我的家庭服务器写入文件时遇到麻烦

[英]Trouble with writing to file with php and my home server

So, I have this simple little php script. 所以,我有这个简单的小php脚本。 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. 我正在运行32位Debian 6.0.6的个人家庭Web服务器上运行它。 It's apache with php. 它是用apache编写的php。 And I know for a fact that php is working on the server. 而且我知道php在服务器上工作的事实。

    <?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. 我已经在Google上搜索了几天,但无法解决该问题。 I'm a php 'noob' and I'm very new to running a linux-based web-server, but hey, you gotta learn somehow. 我是php'noob',对于运行基于linux的网络服务器非常陌生,但是,您必须以某种方式学习。 :*l :* l

tried to check the permissions to the file? 试图检查文件的权限? The Linux file system have a very strict permission system. Linux文件系统具有非常严格的权限系统。 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. 请确保阅读本文 ,以确保Apache对该文件具有“写”权限。 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). 如果不是,请使用chmod命令授予Apache对文件的访问权限(或chown命令,如果此文件的所有者具有写权限,则将该文件的所有者更改为apache)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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