【问题标题】:PHP mkdir failed to created directory in serverPHP mkdir 未能在服务器中创建目录
【发布时间】:2014-05-17 07:31:23
【问题描述】:

我在服务器(centos)中创建文件夹时遇到问题,有一个带有文件上传字段的表单,其中 ajax 回调总是返回错误为:

警告:mkdir(): Permission denied in /home/sitename/public_html/inc/callback/request_update.php on line 90

警告:move_uploaded_file(../../images/listing/16/805202.jpg):无法打开流:/home/sitename/public_html/inc/callback/request_update.php 中没有这样的文件或目录第 95 行

警告:move_uploaded_file():无法将“/tmp/phpnrtrdp”移动到/home/sitename/public_html/inc/callback/request_update 中的“../../images/listing/16/805202.jpg”。第95行的php

它表明文件request_update.php中的行导致错误:

mkdir($output_dir, 0755, true);

request_update.php:

if(isset($_FILES['files'])){

foreach($_FILES['files']['tmp_name'] as $key => $tmp_name){

    $file_name = $key.'_'.$_FILES['files']['name'][$key];
    $file_size = $_FILES['files']['size'][$key];
    $file_tmp = $_FILES['files']['tmp_name'][$key];
    $file_type = $_FILES['files']['type'][$key];

    //explode fine name and extension
    $ext_x = explode('.', $_FILES['files']['name'][$key]);
    $ext = strtolower(end($ext_x));
    $file_name = str_replace('.'.$ext, '', $_FILES['files']['name'][$key]);

    //new file name
    $output_dir = '../../images/listing/'.$list_id;
    $new_file_name[] = rand(1, 999999).'.'.$ext;
    $pathfile = $output_dir.'/'.end($new_file_name);

    // create directory if does not exist
    if(is_dir($output_dir) == false){
        mkdir($output_dir, 0755, true); /*this is where error indicate*/
    }

    if(is_dir($pathfile) == false){

        if(move_uploaded_file($file_tmp, $pathfile)){
            
            //watermark
            $water_path = '../../images/watermark.jpg';
            $watermark = WideImage::load($water_path);
            
            //resize original image
            WideImage::load($pathfile)->resize(300, 360)->merge($watermark, '50% – 25', '100% – 40', 80)->saveToFile($pathfile);

            
        }

    }

}

}

我试过在另一个共享服务器甚至本地主机上运行相同的代码,文件夹可以一直正常创建,发生了什么?

【问题讨论】:

  • 让你的目录可写,chmod a+w images/listing/listing/16/
  • 你试过mkdir($output_dir, 0777, true);吗? , 仅用于测试目的..
  • @Shankar,已尝试 0777 但也失败了。

标签: php mkdir permission-denied


【解决方案1】:

我要添加到 Simon Groenewolt 的评论答案是,您应该从检查 PHP 是否真的可以使用 is_writable 以您想要的路径写入开始功能:

例子:

$filename = 'test.txt';
if (is_writable($filename))
  {
    echo 'The file is writable';
  } 
else
  {
    echo 'The file is not writable';
  }

只需设置适当的权限,如 Log1c 所述,通过运行chmod a+w /yourpath/yourdir(即666)在您尝试写入的目录上,即读取 并且 对于所有人,userownerworld


is_writable() 如果文件名存在且可写,则返回 TRUE。 filename 参数可能是一个目录名,允许您检查目录是否可写。

请记住,PHP 可能会以 Web 服务器运行的用户 ID(通常是“nobody”)访问文件。不考虑安全模式限制。

编辑 1:

确保 PHP open_basedir 设置,也设置为访问 /tmp。您可以出于测试目的禁用它,看看您得到了什么。尝试通过运行检查 open_basedir 的设置是什么:

echo ini_get('open_basedir');

【讨论】:

  • 是的,我只是嵌入这个代码,它显示The file is not writable,我应该在服务器设置中做什么?
  • @conmen 终端命令的输出是什么:ls -lsa /path_to/images/listing/?
  • 输出:4 drwxr-xr-x 13 moment69 nobody 4096 Apr 4 19:41 ./ 4 drwxr-xr-x 5 moment69 nobody 4096 Apr 2 00:44 ../ 4 drwxr-xr-x 2 moment69 nobody 4096 Apr 4 00:18 1/ 4 drwxr-xr-x 2 moment69 nobody 4096 Apr 4 00:18 10/
  • 您似乎以无人身份运行 Apache (mod_php) 并且您拥有 group xr 值。运行chmod 666 /path_to/images/listing 以确保group 也可以写入,并且到目前为止group(Apache 以无人身份运行)只能读取/执行,即(xr)。我应该解决你的问题。
  • @conmen 从 PHP 运行的echo ini_get('open_basedir'); 的输出是什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-02
  • 2015-04-06
  • 2016-11-19
  • 2012-12-04
  • 2023-03-09
  • 1970-01-01
  • 2011-11-27
相关资源
最近更新 更多