1

我读过我可以使用is_writable()检查文件夹或文件是否可写。

如何检查文件是否可以写入文件夹?

  1. 我是否检查文件夹以及该文件夹是否可写?然后允许将文件放入该文件夹中吗?
  2. 如果文件已写入文件夹怎么办,我如何检查它是否可以再次写入(编辑)?我需要吗?如果是这样,我是否检查文件而不是文件夹?
  3. 这是一种安全的方法(正确)吗?
4

2 回答 2

1

The PHP function is_writable is exactly for this purpose. If you want to check if file is still writable after you've written the file, you can use the same function.

于 2015-01-17T20:57:02.873 回答
1

Read the documentation as linked in the question you pointed to. is_writable() is working on files and directories.

But mind: If you have code like this:

if (is_writeable("foo.txt")) {
    $fp = fopen("foo.txt", "w");
    /* ...*/
}

This might still fail. For instance there might be a lock or a race condition (permissions change between the two commands). Better simply try to open and then handle the error.

$fp = @fopen("foo.txt", "w");
if (!$fp) { 
    report_error_in_some_way();
}
于 2015-01-17T20:57:50.820 回答