Php 在服务器上写入文件的权限被拒绝
我想从我的Android手机上传一个文件到运行apache2的Raspberry Pi服务器上。我想通过php实现这一点,但php不起作用,因此我将问题简化为一个同样失败的最小示例。我认为问题与权限有关 在服务器上具有以下wtest.php文件:Php 在服务器上写入文件的权限被拒绝,php,Php,我想从我的Android手机上传一个文件到运行apache2的Raspberry Pi服务器上。我想通过php实现这一点,但php不起作用,因此我将问题简化为一个同样失败的最小示例。我认为问题与权限有关 在服务器上具有以下wtest.php文件: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/s$ <html lang="en" xmlns="http://www.w3.or
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/s$
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<?php
echo 'Current script owner: ' . get_current_user();
echo "\n";
$file2 = fopen('testfile2', 'w');
if ($file2 === false)
{
print_r(error_get_last());
}
$fwrite2 = fwrite($file2, 'hello');
if ($fwrite2 === false){
echo "\n";
echo "write 2 error\n";
}
else{
echo "write2 OK\n";
}
fclose($file2);
?>
</html>
如果我在Raspberry Pi服务器上以脚本的形式运行php标记中的内容,那么它可以正常工作暂时我已将wtest.php和gcm文件夹的权限更改为777。我知道这对于一个永久性的解决方案来说是一个坏主意,但我已经尝试了很短的时间,试图缩小问题的范围
到目前为止,一切都不起作用。我们将非常感谢您提供的任何帮助,而不是下面的原始代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/s$
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<?php
echo 'Current script owner: ' . get_current_user();
echo "\n";
$file2 = fopen('testfile2', 'w');
if ($file2 === false)
{
print_r(error_get_last());
}
$fwrite2 = fwrite($file2, 'hello');
if ($fwrite2 === false){
echo "\n";
echo "write 2 error\n";
}
else{
echo "write2 OK\n";
}
fclose($file2);
?>
</html>
而且
如果文件名不存在,则创建该文件。否则,现有文件将被覆盖,除非设置了file\u APPEND
标志
注意:只需将整个文件替换为上述内容即可。不需要
DocType定义
您确定web服务器用户和文件所有者是否相同吗?您想对应该写入的文件执行chmod 777
吗?@PraveenKumar在执行代码之前,要写入的文件实际上并不存在。php创建了它。它可以在服务器上运行的脚本中正常工作,但不能通过浏览器。检查php运行的所有者/组,然后将web文件夹的所有者/组设置为相同的。@Novocaine。从浏览器运行php时,get current_用户返回'pi',如我的原始帖子所示。gcm文件夹的用户组显示为pi-pi。@NickT你能用chmod 777和Apache2在Ubuntu笔记本电脑上运行它吗。我创建了一个文件夹并安装了wtest.php。它在那台机器上也失败了。这一定是Apache的问题,我本希望这可能是Raspbian的怪异之处,但显然不是。@NickT你能尝试创建文件并尝试一下吗?@NickT写的文件正确吗?检查是否存在文件\u
?啊哈,如果我首先使用用户、组和所有人的写入权限创建它,它确实可以工作。非常感谢。但是,如何在没有现有文件的情况下修复它?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/s$
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<?php
echo 'Current script owner: ' . get_current_user();
echo "\n";
$file2 = fopen('testfile2', 'w');
if ($file2 === false)
{
print_r(error_get_last());
}
$fwrite2 = fwrite($file2, 'hello');
if ($fwrite2 === false){
echo "\n";
echo "write 2 error\n";
}
else{
echo "write2 OK\n";
}
fclose($file2);
?>
</html>
<?php
if (file_put_contents("testfile2", "hello") !== false)
echo "File Written";
else
echo "Failed";
?>