1

我正在使用 codeigniter 的网站上工作,并且正在尝试使用 Froala Editor 在文本区域中上传图像。一切正常,直到我尝试将图像文件上传到我的本地文件夹而不是默认的http://i.froala.com/upload

这是我的 html 文件:

<textarea id="my_editor" name="my_editor" class="editor">
</textarea>
<script>
  $(function() {
    $('.editor').froalaEditor({
      imageUploadURL: "test/froala_upload",
    })
  });
</script>

这是froala_upload我的Test控制器文件中的函数:

function froala_upload() {

    // Allowed extentions.
    $allowedExts = array("gif", "jpeg", "jpg", "png", "blob");

    // Get filename.
    $temp = explode(".", $_FILES["file"]["name"]);

    // Get extension.
    $extension = end($temp);

    // Generate new random name.
    $name = sha1(microtime()) . "." . $extension;

    // Save file in the uploads folder.
    move_uploaded_file($_FILES["file"]["tmp_name"], getcwd(). "/assets/review/" . $name);

    // Generate response.
    $response = new StdClass;
    $response->link = "/assets/review/" . $name;
    echo stripslashes(json_encode($response));
}

我故意省略了finfo大多数关于 Froala Image Upload 的文档中使用的图像检查,因为有些人说这可能是问题 - 但我仍然无法上传图像文件。并且错误消息非常有帮助:“出了点问题。请重试”

我花了几个小时在这件事上摸不着头脑。并且在广阔的互联网世界上实际上没有完整的代码解决方案,所以我无法找出我做错了什么。谁能帮我这个?

4

3 回答 3

2

根据他们的文档,您应该使用此代码

<script>
  $(function() {
    $('.selector').froalaEditor({
      // Set the image upload URL.
      imageUploadURL: '/your_upload_image_script.php',

      imageUploadParams: {
        id: 'my_editor'
      }
    })
  });
</script>

接下来你必须创建一个名为 your_upload_image_script.php 的文件,它应该像

<?php
    // Allowed extentions.
    $allowedExts = array("gif", "jpeg", "jpg", "png", "blob");

    // Get filename.
    $temp = explode(".", $_FILES["file"]["name"]);

    // Get extension.
    $extension = end($temp);

    // An image check is being done in the editor but it is best to
    // check that again on the server side.
    // Do not use $_FILES["file"]["type"] as it can be easily forged.
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime = finfo_file($finfo, $_FILES["file"]["tmp_name"]);

    if ((($mime == "image/gif")
    || ($mime == "image/jpeg")
    || ($mime == "image/pjpeg")
    || ($mime == "image/x-png")
    || ($mime == "image/png"))
    && in_array(strtolower($extension), $allowedExts)) {
        // Generate new random name.
        $name = sha1(microtime()) . "." . $extension;

        // Save file in the uploads folder.
        move_uploaded_file($_FILES["file"]["tmp_name"], getcwd() . "/uploads/" . $name);

        // Generate response.
        $response = new StdClass;
        $response->link = "/uploads/" . $name;
        echo stripslashes(json_encode($response));
    }
?>
于 2016-05-18T10:50:55.690 回答
0
<?php

try {
  // File_Route.
  $fileRoute = "/Name_folder/Name";

  $fieldname = "file";

  // Get filename.
  $filename = explode(".", $_FILES[$fieldname]["name"]);

  // Validate uploaded files.
  // Do not use $_FILES["file"]["type"] as it can be easily forged.
  $finfo = finfo_open(FILEINFO_MIME_TYPE);

  // Get temp file name.
  $tmpName = $_FILES[$fieldname]["tmp_name"];

  // Get mime type.
  $mimeType = finfo_file($finfo, $tmpName);

  // Get extension. You must include fileinfo PHP extension.
  $extension = end($filename);

  // Allowed extensions.
  $allowedExts = array("gif", "jpeg", "jpg", "png", "svg", "blob");

  // Allowed mime types.
  $allowedMimeTypes = array("image/gif", "image/jpeg", "image/pjpeg", "image/x-png", "image/png", "image/svg+xml");

  // Validate image.
  if (!in_array(strtolower($mimeType), $allowedMimeTypes) || !in_array(strtolower($extension), $allowedExts)) {
    throw new \Exception("File does not meet the validation.");
  }

  // Generate new random name.
  $name = sha1(microtime()) . "." . $extension;
  $fullNamePath = dirname(__FILE__) . $fileRoute . $name;

  // Check server protocol and load resources accordingly.
  if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] != "off") {
    $protocol = "https://";
  } else {
    $protocol = "http://";
  }

  // Save file in the uploads folder.
  move_uploaded_file($tmpName, $fullNamePath);

  // Generate response.
  $response = new \StdClass;
  $response->link = $protocol.$_SERVER["HTTP_HOST"].dirname($_SERVER["PHP_SELF"]).$fileRoute . $name;

  // Send response.
  echo stripslashes(json_encode($response));

} catch (Exception $e) {
   // Send error response.
   echo $e->getMessage();
   http_response_code(404);
}
?>
于 2017-06-18T03:35:40.850 回答
0

它可能缺少extension价值。

尝试以下

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $_FILES["file"]["tmp_name"]);

$extension = end($temp);
if ($extension == "") {
    list($dummy, $extenstion) = explode("/", $mime);
}
于 2017-04-16T18:50:49.593 回答