如何使用 Vue & Laravel 访问另一个 docker 卷文件?
How to access another docker volume files with Vue & Laravel?
我正在 运行Vue 和 Laravel 项目 docker。
Project structure and everything about the setup
首先,我正在尝试在服务器上上传图片。
如果我 运行 这个项目没有 docker,它将按计划工作 (文件正在上传到服务器并且可以访问).
但是,如果我用 docker 运行 这个项目,它会抛出一个错误 (can't find directory).
重要提示:我猜测这可能是因为我在后端处理文件(laravel 控制器,代码在下面)。所以它只能访问后端卷。
正在尝试在前端卷中定位文件,但无法访问它。
那么,问题来了:如何访问该卷?
文件控制器:
public function uploadFile (Request $request) {
# Processing to base64
$exploded = explode(',', $request->image_data);
$decoded = base64_decode($exploded[1]);
# Configuring the extension
$extension = strtok(explode('/', $exploded[0])[1], ';');
# Defining the path
$path = '../../'.'frontend'.'/'.'src'.'/'.'assets'.'/'.'img'.'/'.'files'.'/'.$request->file_name;
# Uploading file to the storage
file_put_contents($path, $decoded);
}
你的路径在这里
$path = '../../'.'frontend'.'/'.'src'.'/'.'assets'.'/'.'img'.'/'.'files'.'/'.$request->file_name;
向上两步,然后进入 frontend
目录。但是你的 backend
容器中没有这样的目录。
backend:
volumes:
- ./backend:/app
- ./etc/php:/usr/local/etc/php/local.conf.d/
最后两行将 bakend
和 etc/php
安装在后端容器中。如您所见,没有前端,因此您在主机上使用的路径将不起作用。再添加一行来修复它:
backend:
volumes:
- ./backend:/app
- ./etc/php:/usr/local/etc/php/local.conf.d/
- ./frontend:/frontend # this line
这将使前端目录出现在 /frontend
并且应该足以修复它。
我正在 运行Vue 和 Laravel 项目 docker。
Project structure and everything about the setup
首先,我正在尝试在服务器上上传图片。
如果我 运行 这个项目没有 docker,它将按计划工作 (文件正在上传到服务器并且可以访问).
但是,如果我用 docker 运行 这个项目,它会抛出一个错误 (can't find directory).
重要提示:我猜测这可能是因为我在后端处理文件(laravel 控制器,代码在下面)。所以它只能访问后端卷。
正在尝试在前端卷中定位文件,但无法访问它。
那么,问题来了:如何访问该卷?
文件控制器:
public function uploadFile (Request $request) {
# Processing to base64
$exploded = explode(',', $request->image_data);
$decoded = base64_decode($exploded[1]);
# Configuring the extension
$extension = strtok(explode('/', $exploded[0])[1], ';');
# Defining the path
$path = '../../'.'frontend'.'/'.'src'.'/'.'assets'.'/'.'img'.'/'.'files'.'/'.$request->file_name;
# Uploading file to the storage
file_put_contents($path, $decoded);
}
你的路径在这里
$path = '../../'.'frontend'.'/'.'src'.'/'.'assets'.'/'.'img'.'/'.'files'.'/'.$request->file_name;
向上两步,然后进入 frontend
目录。但是你的 backend
容器中没有这样的目录。
backend:
volumes:
- ./backend:/app
- ./etc/php:/usr/local/etc/php/local.conf.d/
最后两行将 bakend
和 etc/php
安装在后端容器中。如您所见,没有前端,因此您在主机上使用的路径将不起作用。再添加一行来修复它:
backend:
volumes:
- ./backend:/app
- ./etc/php:/usr/local/etc/php/local.conf.d/
- ./frontend:/frontend # this line
这将使前端目录出现在 /frontend
并且应该足以修复它。