php不会创建文件 [英] php won't create file

查看:71
本文介绍了php不会创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我尝试使用fopen(file,w)用php创建文件.但它返回一个错误.但是,这仅在服务器端,当我在localhost(XAMPP)上运行它时,它可以正常工作

So I tried creating a file with php using fopen(file, w). but it returns an error. However this is only on the server side, it works fine on when I run it on 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();
}}

这段代码在localhost上运行,但不在服务器上运行.我尝试了像这样的以前的问题给出的许多不同的方法和解决方案,但都没有用.但是,在尝试附加到现有文件之后,很明显它可以找到该文件,因此不是错误的文件路径.它只是拒绝打开文件.所以我的文件夹结构如下.

This piece of code runs on localhost but not on the server. I tried a lot of different things and solutions given by previous questions like this, which all didnt work. But after trying to append to an existing file instead it was quite clear it could find the file so it wasn't the filepath that was wrong. It simply refuses to open the file. So my folder structure is as follows.

/phpscripts.php
/Datafiles/Data.csv
/files/images.png <--- tried moving the file here didn't work either.

我可以从中得出的唯一结论是服务器不允许php编写和打开文件,但是我不知道为什么或如何更改它.所以没有人知道如何解决这个问题.

The only conclusion I can draw from this is that the server doesnt allow php to write and open files, but I dont know why or how to change this. So does anyone know how to solve this problem.

推荐答案

我认为这是用户权限错误.确保您的httpd用户对您要写入的文件/目录具有写入权限.不过可以肯定的是,您应该启用日志记录功能,并在尝试open($f, 'w')时检查php抛出的警告.

I'd assume this is a user permissions error. Make sure your httpd user has write permission to the file/directory you're trying to write to. To be certain though, you should enable logging and check the warning php throws when attempting to open($f, 'w').

再次假设这是一个权限错误,并且这是一个linux服务器,您应该先执行chown httpduser:httpduser /Datafiles/然后执行chmod u+w /Datafiles/chmod g+w /Datafiles/.

Again assuming this is a permission error and that this is a linux server, you should do a chown httpduser:httpduser /Datafiles/ and then a chmod u+w /Datafiles/ or chmod g+w /Datafiles/.

这篇关于php不会创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆