Laravel-公用文件夹中的相对路径

cle

我正在使用tinymce编辑器将图像上传到服务器中,并且在设置路径时遇到问题。postAcceptor.php/public,它将把图像上传到其中的文件夹/articles/assets/images//public

在中web.php,我Route::get('/articles/{id}', function(){...})用来显示已在服务器中上传的图像。但是,图像指示来源错误,因此无法显示图像。它的来源应该是:

localhost:8000/articles/assets/images/fragment%20life%20cycle.png

但是,实际上,它指示:-

localhost:8000/articles **/articles**/assets/images/fragment%20life%20cycle.png

postAcceptor.php

<?php
  /*******************************************************
   * Only these origins will be allowed to upload images *
   ******************************************************/
  $accepted_origins = array("http://localhost:8000", "http://192.168.1.1", "http://example.com");

  /*********************************************
   * Change this line to set the upload folder *
   *********************************************/
  $today = date("y-m-d");
  $imageFolder = "articles/assets/images/";

  reset ($_FILES);
  $temp = current($_FILES);
  if (is_uploaded_file($temp['tmp_name'])){
    if (isset($_SERVER['HTTP_ORIGIN'])) {
      // same-origin requests won't set an origin. If the origin is set, it must be valid.
      if (in_array($_SERVER['HTTP_ORIGIN'], $accepted_origins)) {
        header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
      } else {
        header("HTTP/1.1 403 Origin Denied");
        return;
      }
    }

    /*
      If your script needs to receive cookies, set images_upload_credentials : true in
      the configuration and enable the following two headers.
    */
    // header('Access-Control-Allow-Credentials: true');
    // header('P3P: CP="There is no P3P policy."');

    // Sanitize input
    if (preg_match("/([^\w\s\d\-_~,;:\[\]\(\).])|([\.]{2,})/", $temp['name'])) {
        header("HTTP/1.1 400 Invalid file name.");
        return;
    }

    // Verify extension
    if (!in_array(strtolower(pathinfo($temp['name'], PATHINFO_EXTENSION)), array("gif", "jpg", "png"))) {
        header("HTTP/1.1 400 Invalid extension.");
        return;
    }

    // Accept upload if there was no origin, or if it is an accepted origin
    $filetowrite = $imageFolder . $temp['name'];
    move_uploaded_file($temp['tmp_name'], $filetowrite);


    // Respond to the successful upload with JSON.
    // Use a location key to specify the path to the saved image resource.
    // { location : '/your/uploaded/image/file'}
    echo json_encode(array('location' => $filetowrite));
  } else {
    // Notify editor that the upload failed
    header("HTTP/1.1 500 Server Error");
  }
?>

有什么办法可以解决这个问题,还是绝对或相对路径的任何线索?

/公用文件夹

上传成功

/ articles / 44-显示图像页面

图像src

德文·格雷(Devin Gray)

Laravel内置了路径帮助器,您可以使用它们来确保始终始终获得通往事物的正确路径。

尝试改变

$imageFolder = "articles/assets/images/";

$imageFolder = public_path("articles/assets/images/");

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在laravel的根目录中获取文件的相对路径

python中的相对路径几个文件夹

Laravel资产与相对路径

Laravel sqlite相对路径

Laravel Mix生成相对路径

Laravel,保护文件夹公用文件夹中

Laravel:对缓存文件夹使用绝对路径

Laravel:存储不将文件放在公用文件夹中

重命名 Laravel 公用文件夹中的文件

在Laravel中访问公用文件夹中的图像

如何从Laravel的公用文件夹中调用模型?

在laravel 4.2中限制对公用文件夹的访问

从不同的子文件夹中查找文本文件的相对路径

如何从Rmarkdown中的.tex文件夹(相对路径)获取图像?

更改Laravel 5.1的公用文件夹

Laravel公用文件夹Php工匠

Laravel公用文件夹禁止

如何在VS 2012中设置HTML和CSS中的相对路径和文件夹路径?

Laravel:如何在Laravel中找到公用文件夹?

javamelody - 存储文件总是在 tomcat 的临时文件夹的相对路径中

PlayFramework(2.6.12): HTTPS 模式下使用 conf 文件夹(相对路径)中的 JKS 文件

无法通过Express到达父级兄弟姐妹文件夹中js文件的相对路径?

如何获取.net core项目中项目文件夹中存在的文件的相对路径

使用ASP.NET C#中的相对路径将图像保存在文件夹中

在Python中,如何从具有相对路径的祖父母文件夹中导入?

如何使用该位置的相对路径在一个位置中创建多个文件夹?

如何连接相对路径或生成嵌套文件夹中包含的唯一标识符 svg?

标记文件中的相对路径

HTML文件中的相对路径