【问题标题】:Redactor image upload doesn't work编辑器图像上传不起作用
【发布时间】:2014-09-01 18:33:27
【问题描述】:

在子域上运行编辑器的网站。它报告错误: chmod() - 无法打开流:没有这样的文件或目录 和 move_uploaded_file() - 无法打开流:没有这样的文件或目录。 所有文件权限都设置正确。图片上传代码如下:

// files storage folder
$dir = $_SERVER['DOCUMENT_ROOT'].'/Picks/Redactor/';

$_FILES['file']['type'] = strtolower($_FILES['file']['type']);

if ($_FILES['file']['type'] == 'image/png'
|| $_FILES['file']['type'] == 'image/jpg'
|| $_FILES['file']['type'] == 'image/gif'
|| $_FILES['file']['type'] == 'image/jpeg'
|| $_FILES['file']['type'] == 'image/pjpeg'
|| $_FILES['file']['error'] == 0)
{
    // setting file's mysterious name
    $filename = md5(date('YmdHis')).'.jpg';
    $file = $dir.$filename;

    // copying
    chmod($dir, 777);
    move_uploaded_file($_FILES['file']['tmp_name'], $file);

    // displaying file
 $array = array(
  'filelink' => 'Picks/Redactor/'.$filename
 );

 echo stripslashes(json_encode($array));

}

【问题讨论】:

    标签: php file file-upload upload redactor


    【解决方案1】:

    首先,这与 Redactor 无关。

    您最有可能收到此错误,因为该目录不存在。 chmod 不会创建它。

    试试这个

    if (!file_exists($dir)) {
        //dir, rights, recursive
        mkdir($dir, 0777, true); //Maybe we need not that much permissions...
    }
    

    你确定,dir 是那个,你怎么想的?

    var_dump($dir);
    

    对linux不太了解,但至少在windows下,如果我们要授予(目录)权限,执行脚本(不包括,你在浏览器中调用的真实文件)需要同样的权限。

    站点节点:我们不应该授予来自用户的文件的执行权限。另请注意,mimetype 不能准确。图片专用,getimagesize会更靠谱。

    【讨论】:

      【解决方案2】:
      // This is a simplified example, which doesn't cover security of uploaded images.
      // This example just demonstrate the logic behind the process.
      
      
      // files storage folder
      $dir = 'uploads/data/'; // or use '../uploads/data/' but not use '/uploads/data/'
      
      $_FILES['file']['type'] = strtolower($_FILES['file']['type']);
      
      if ($_FILES['file']['type'] == 'image/png'
      || $_FILES['file']['type'] == 'image/jpg'
      || $_FILES['file']['type'] == 'image/gif'
      || $_FILES['file']['type'] == 'image/jpeg'
      || $_FILES['file']['type'] == 'image/pjpeg')
      {
          // setting file's mysterious name
          $filename = md5(date('YmdHis')).'.jpg';
          $file = $dir.$filename;
      
          // copying
          move_uploaded_file($_FILES['file']['tmp_name'], $file);
      
          $array = array(
              'filelink' => "/".$dir.$filename
          );
      
          echo stripslashes(json_encode($array));
          //print_r($_FILES['file']);
      
      }
      

      【讨论】:

        猜你喜欢
        • 2019-06-06
        • 2023-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多