Laravel-如何更改上传文件路径 [英] Laravel - How to change upload file path

查看:575
本文介绍了Laravel-如何更改上传文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将图像上传到文件夹项目图像.

I need to upload images to folder project images.

我在共享主机中的项目结构文件夹是:

My project structure folder in share host is this:

- css
- images
- fonts
- js
- laravel-code
- .htaccess

laravel-code文件夹中的其余文件.

Inside laravel-code folder are the remaining files.

该网站运行良好,显示效果很好,但是当我上传新图像时,它将图像保存在laravel-code/中并创建了此文件夹public/images ...

The site work well, show images well but when i upload a new image it save the image in laravel-code/ and create this folders public/images ...

我想将图像保存在根目录下的图像文件夹中.

I want to save images in image folder at root directory.

我该怎么做?

当我上传文件时,它会检测它是pdf还是其他文件;如果是pdf,则将文件保存在存储文件夹中;如果其他文件(图像),则保存在图像文件夹中.这是我的代码...我想我不能使用public_path

When I do an upload file it detects if it was a pdf or other file; if it was pdf saves file on storage folder, if it other file (images) save in images folder. This is my code... I think I cannot use public_path

if (false === stripos($name, '-pdf')) {
    $basePath = trim(public_path(), '/');
    $newPath = trim(public_path('images'), '/');
} else {
    $basePath = trim(storage_path(), '/');
    $newPath = trim(storage_path(), '/');
}

推荐答案

要获取根目录父目录的完整路径,您将需要使用PHP内置的

To get the full path to the parent of the root directory, you will need to use PHP's built in dirname function.

第一个参数指示要评估的目录.第二个参数是您要遍历的父目录的级别,在您的示例中,仅上一层.

The first argument indicates the directory to be evaluated. The second argument is the level of parent directories you want to traverse, in your example, only one level up.

$root = base_path();
$parent = dirname($root, 1); 

这篇关于Laravel-如何更改上传文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆