PHP file doesn't write to current directory?

时光毁灭记忆、已成空白 提交于 2019-12-02 06:52:45

问题


I have a PHP file on a web-host that is as follows:

<?php

file_put_contents('test.txt','TEST');    
echo 'OK';

?>

But the file test.txt is not created when I execute the page from a browser. Why not? Is it a permissions issue?


回答1:


yes the PHP Doc Says

If filename does not exist, the file is created. Otherwise, the existing file is overwritten, unless the FILE_APPEND flag is set.

That is only if you write have permission on that folder .... try

error_reporting(E_ALL);
ini_set("display_errors", "on");

if (! is_writable(__DIR__)) {
    trigger_error("I don't have permission");
}

file_put_contents('test.txt', 'TEST');



回答2:


Why not? Is it a permissions issue?

The question you should be asking yourself is why don't you know already?

The code will be producing an error or warning - why don't you know what that is?

It's likely a permissions issue - what are the permissions?

BTWE: writing content within your document root, to a directory which will execute PHP is a huge security vulnerability. i.e. if it is a permissions issue, then changing the permissions is the wrong way to solve the problem.



来源:https://stackoverflow.com/questions/13008850/php-file-doesnt-write-to-current-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!