简体   繁体   English

访问被拒绝 - PHP move_uploaded_file - Ubuntu LAMP / var / www

[英]access denied - PHP move_uploaded_file - Ubuntu LAMP /var/www

I realize there is some sort of problem with permissions on either my tmp folder or the images folder I created in the /var/www folder. 我意识到我的tmp文件夹或我在/ var / www文件夹中创建的images文件夹的权限存在某种问题。 I know that /var/www initially has root access. 我知道/ var / www最初具有root访问权限。 I have been following some online tutorials to try and fix this issue and have changed my permissions to who knows what over the last hour or so. 我一直在关注一些在线教程,试图解决这个问题并将我的权限更改为谁知道过去一小时左右的内容。

I receive this error when trying to upload a file from an HTML form using PHP/MySQL: 尝试使用PHP / MySQL从HTML表单上传文件时收到此错误:

Warning: move_uploaded_file(images/verified-gw.gif): failed to open stream: Permission  
denied in /var/www/addscore.php on line 40 Warning: move_uploaded_file(): Unable to move 
'/tmp/phpla4QCP' to 'images/verified-gw.gif' in /var/www/addscore.php on line 40 cannot 
move uploaded file or something beavis

So I think the permissions on either the /var/www/images folder are incorrect or the permissions on the tmp folder are root and the kernel of ubuntu is not letting the php script move from this root owned file to the images folder which has permissions of my user account to my knowledge and is in the group nobody. 所以我认为/ var / www / images文件夹上的权限不正确,或者tmp文件夹的权限是root,ubuntu的内核不允许php脚本从这个root拥有的文件移动到具有权限的images文件夹根据我的知识,我的用户帐户是在没有人的组。

I am pretty lost, any help is definitely appreciated. 我很迷茫,任何帮助都绝对值得赞赏。

Ubuntu Linux gnome文件夹权限的截图

Above you can see a picture of the permissions on the images folder I am trying to move the file from the tmp directory to. 上面你可以看到我试图将文件从tmp目录移动到的images文件夹的权限图片。

Oh and here is the PHP script that fails: 哦,这是失败的PHP脚本:

if (!empty($screenshot)) { 
move_uploaded_file($_FILES['screenshot']['tmp_name'], $target) 
or die (' cannot move uploaded file or something beavis'); 

Respectfully, 尊敬,

user 用户

working with USER DIR mod 使用USER DIR mod

If this is developer machine, and i think it is, dont try to use /var/www (there will be always permission problem) Try to use user dir 如果这是开发者机器,我认为它是,不要尝试使用/ var / www(将始终存在权限问题)尝试使用用户目录

you will keep all your files and projects in your home and there will be no permission problems. 您将把所有文件和项目保存在家中,不存在任何权限问题。

working with Apache root dir 使用Apache root dir

Ok, so if you cant work with user dir, you have to set up file system like this: 好的,所以如果你不能使用用户目录,你必须像这样设置文件系统:

1. Find out apache owner user name. 1.找出apache所有者用户名。 If you use Ubuntu, it should be www-data. 如果您使用Ubuntu,它应该是www-data。 Go to terminal and type: 转到终端并输入:

$ ps aux | grep apache

You should get something like this below. 你应该在下面得到这样的东西。 First column is username. 第一列是用户名。

www-data 2070 0.0 0.0 463244 7788 ? S sty25 0:00 /usr/sbin/apache2 -k start

2. Find out apache user group. 2.找出apache用户组。 Again terminal, type: 再次终端,输入:

$ sudo groups www-data

You should get something like this below. 你应该在下面得到这样的东西。 Second column is group name. 第二列是组名。

www-data : www-data

3. Set RIGHT owner user and owner group for apache root dir: 3.为apache root dir设置RIGHT所有者用户和所有者组:

$ sudo chown -R www-data:www-data /var/www/

4. Set RIGHT privileges for apache root dir: 4.为apache root dir设置RIGHT权限:

$ sudo chmod -R 775 /var/www/

Now you have your system cleaned up, and we try to fix your user to have access to /var/www directory. 现在您已清理系统,我们尝试修复您的用户以访问/ var / www目录。

5. Add apache group to your user: 5.将apache组添加到您的用户:

$ sudo usermod -a -G www-data {YOUR_USER_NAME}

WARNING! 警告! dont forget -a switch! 别忘了 - 换! this way you append new group to existing list and not replace old groups list with new one. 这样,您可以将新组附加到现有列表,而不是用新组替换旧组列表。

Now you have read/write access to files located in apache root dir. 现在您具有对位于apache根目录中的文件的读/写访问权限。 And we need to tell apache that new files should be readable/writable to group members 我们需要告诉apache新文件对组成员应该是可读/可写的

6. Add umask command to apache enviroment config: 6.将umask命令添加到apache环境配置:

$ sudo echo umask 002 >> /etc/apache2/envvars

or with graphic editor: 或者使用图形编辑器:

$ gksudo gedit /etc/apache2/envvars

and place umask 002 as last line in this file 并将umask 002作为此文件的最后一行

7. Voila 7.

$ sudo apache2ctl restart

Good luck! 祝好运!

This solved my problem: http://ubuntuforums.org/showthread.php?t=1842090&s=8b97ec72fe7cd753910f488329a90f0f&p=11239688#post11239688 这解决了我的问题: http//ubuntuforums.org/showthread.php?t = 1842090&s = 8b97ec72fe7cd753910f488329a90f0f&p = 11239688#post11239688

tl:dr - I typed this: sudo chown -R www-data:www-data /var/www tl:dr - 我输入了这个: sudo chown -R www-data:www-data /var/www

I do not think it was a combination of trying to chmod the chown the tmp directory and this. 我不认为这是尝试chmod chmp tmp目录和这个的组合。 I think it is just changing the permissions on the /var/www folder where all LAMP files are kept. 我认为只是更改/ var / www文件夹中保留所有LAMP文件的权限。

Now the PHP script works and tosses the image file in the directory I made. 现在PHP脚本工作并将图像文件扔到我制作的目录中。 It would be easier if Ubuntu installed test servers like this LAMP stack in the user directory without having to learn file permissions like I am protecting my files from the nazis' submarines. 如果Ubuntu在用户目录中安装了像这个LAMP堆栈这样的测试服务器而不必学习文件权限,就像我保护我的文件免受纳粹潜艇的攻击,那会更容易。

try this: sudo chown -R 777 /var/www/images 试试这个: sudo chown -R 777 /var/www/images

you need to give the file a public permission 777 OR 755 so the file can be uploaded to the specified directory its only a permission 你需要给文件一个公共权限777或755所以文件可以上传到指定的目录,它只有一个权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM