douxi4414 2010-05-24 22:10
浏览 399
已采纳

如何为目录提供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?

Edit: It is hosted on Hostgator shared hosting using Apache.

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);

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.)

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).

Edit 3: The server runs CentOS.

  • 写回答

11条回答 默认 最新

  • dongpo7467 2010-05-24 22:20
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(10条)

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来