【发布时间】:2014-04-18 11:16:53
【问题描述】:
所以我尝试使用 fopen(file, w) 用 php 创建一个文件。但它返回一个错误。然而这只是在服务器端,当我在 localhost(XAMPP) 上运行它时它工作正常
代码如下
function createdatafile(){
//Rolls a random number which will be the file name.
$x = rand(0, 9999999);
$file = "Datafiles/".$x.".csv";
if(!file_exists($file)){
$nfile = fopen($file, "w") or die("cannot create file: $file ");//fails
//createdata() just returns a string. However this doesnt even get executed.
fwrite($nfile, createdata());
fclose($nfile);
}else{
//calls itself for another roll if the file already existed.
$this->createdatafile();
}}
这段代码在本地主机上运行,但不在服务器上。我尝试了很多不同的事情和以前的问题给出的解决方案,但都没有奏效。但是在尝试附加到现有文件之后,很明显它可以找到该文件,因此不是文件路径错误。它只是拒绝打开文件。所以我的文件夹结构如下。
/phpscripts.php
/Datafiles/Data.csv
/files/images.png <--- tried moving the file here didn't work either.
我可以从中得出的唯一结论是服务器不允许 php 写入和打开文件,但我不知道为什么或如何更改这一点。那么有谁知道如何解决这个问题。
【问题讨论】:
-
检查文件夹的读写权限
-
权限与用户相关。 php不是用户。服务器不允许来宾读取/写入此文件夹。