PHP创建文件(如果不存在) [英] Php create a file if not exists

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

问题描述

我尝试创建文件并动态写入内容.下面是我的代码.

I try to create files and write the contents dynamically. Below is my code.

$sites = realpath(dirname(__FILE__)).'/';
$newfile = $sites.$filnme_epub.".js";

if (file_exists($newfile)) {
    $fh = fopen($newfile, 'a');
    fwrite($fh, 'd');
} else {
    echo "sfaf";
    $fh = fopen($newfile, 'wb');
    fwrite($fh, 'd');
}

fclose($fh);
chmod($newfile, 0777);

// echo (is_writable($filnme_epub.".js")) ? 'writable' : 'not writable';
echo (is_readable($filnme_epub.".js")) ? 'readable' : 'not readable';
die;

但是,它不会创建文件.

However, it does not create the files.

请分享您的答案和帮助.谢谢!

Please share your answers and help. Thanks!

推荐答案

尝试使用:

$fh = fopen($newfile, 'w') or die("Can't create file");

测试是否可以在此处创建文件.

for testing if you can create a file there or not.

如果您无法创建文件,则可能是因为该目录不能被Web服务器用户(通常是"www"或类似名称)写入.

If you can't create the file, that's probably because the directory is not writeable by the web server user (usually "www" or similar).

对要创建文件的文件夹进行chmod 777 folder,然后重试.

Do a chmod 777 folder to the folder you want to create the file and try again.

行得通吗?

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

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