如何授予PHP对目录的写访问权限? [英] How do I give PHP write access to a directory?

查看:636
本文介绍了如何授予PHP对目录的写访问权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP创建文件,但是它不起作用.我假设这是因为它没有写访问权限(以前一直是问题).我试图通过使文件夹chmod 0777来测试这是否是问题,但最终使该目录中的每个脚本都返回了500条错误消息,直到我将其改回为止. 如何授予PHP对文件系统的写访问权限,以便它可以创建文件?

I'm trying to use PHP to create a file, but it isn't working. I am assuming this is because it doesn't have write access (it's always been the problem before). I tried to test if this was the problem by making the folder chmod 0777, but that just ended up making every script in that directory return a 500 error message until I changed it back. How do I give PHP write access to my file system so it can a create a file?

它托管在使用Apache的Hostgator共享托管上.

It is hosted on Hostgator shared hosting using Apache.

有人要求输入代码: 该代码是GD映像脚本.我知道它的其余部分都可以像以前一样在调用每个ime时创建图像.现在,我尝试在添加新文本时创建它们并将其保存到文件夹.我写的行是: imagejpeg(null,$ file,85);

Edit 2: Someone asked for the code: The code is a GD image script. I know the rest of it works as previously I was creating the image every ime it was called. Now I am trying to create them when new text is added and save them to a folder. The write line I have is: imagejpeg(null,$file,85);

我还创建了一个测试文件来检查它是否只是一个损坏的脚本(主要是从tizag复制): http://gearboxshow.info/rkr/lesig.jpg/testfile.txt (我不知道是否/如何在此处正确发布代码.这是PHP脚本的内容,减去PHP标记.)

I also created a test file to check if it was just a broken script (mainly copied from tizag): http://gearboxshow.info/rkr/lesig.jpg/testfile.txt (I don't know if/how to post the code here properly. Here is the contents of the PHP script, minus PHP tags.)

它返回13,13,1(单独的行),因此看起来好像认为自己写了一些东西,但是testfile.txt是空白的(我上传了空白),或者不存在(如果我删除了它) ).

It returns 13,13,1 (separate lines), so it looks as if it thinks it wrote something, but the testfile.txt is blank (I uploaded a blank one), or non-existent (if I delete it).

服务器运行CentOS.

Edit 3: The server runs CentOS.

推荐答案

一种简单的方法是让PHP首先创建目录本身.

An easy way is to let PHP create the directory itself in the first place.

<?php
 $dir = 'myDir';

 // create new directory with 744 permissions if it does not exist yet
 // owner will be the user/group the PHP script is run under
 if ( !file_exists($dir) ) {
     mkdir ($dir, 0744);
 }

 file_put_contents ($dir.'/test.txt', 'Hello File');

这为您节省了权限的麻烦.

This saves you the hassle with permissions.

这篇关于如何授予PHP对目录的写访问权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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