2 years ago
I have created a volume called /offers, I need to upload an image from the backend with Lavarel and put this image in the fontend. I have set the variable RAILWAYRUNUID=0 for the permissions but it still gives me an error, I leave you my code, help!
all());
$request->validate([
'image' => 'required|image|mimes:jpeg,png,jpg,gif,webp,svg',
]);
$imageName = 'principal.' . $request->image->extension();
$pathToVolume = '/ofertas';
$extensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg'];
foreach ($extensions as $extension) {
$oldImage = $pathToVolume . "ofertas/principal.{$extension}";
if (file_exists($oldImage)) {
unlink($oldImage);
}
}
// Mueve la nueva imagen
$request->image->move($pathToVolume, $imageName);
return response()->json([
'success' => 'Imagen subida con éxito.',
'image' => $imageName
]);
}
public function get()
{
$extensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg'];
$imagen = '';
// Ruta actualizada a tu volumen `/ofertas`
$pathToVolume = '/ofertas';
foreach ($extensions as $extension) {
if (file_exists($pathToVolume . "/ofertas/principal.{$extension}")) {
$imagen = "principal.{$extension}";
break;
}
}
if ($imagen === '') {
return response()->json(['error' => 'Imagen no encontrada'], 404);
}
return response()->json(['image' => "/ofertas/{$imagen}"]);
}}
14 Replies
2 years ago
then name volume is /ofertas, in spanish not /offes
2 years ago
yes this is in the network : message: "Unable to write in the "/ofertas" directory.",…}
exception
:
"Symfony\Component\HttpFoundation\File\Exception\FileException"
file
:
"/app/vendor/symfony/http-foundation/File/File.php"
line
:
122
message
:
"Unable to write in the \"/ofertas\" directory."
trace
:
[{file: "/app/vendor/symfony/http-foundation/File/UploadedFile.php", line: 168,…},…]
2 years ago
Try mounting a volume to /app/storage and then write to that location in code.
2 years ago
Thank you, i Will try this
2 years ago
I have tried app/storage, it uploads the image, but in the frontend only the alt is seen, it seems that this path is not public
Attachments
2 years ago
Make sure you are saving the images in /app/storage and also serving them from /app/storage
2 years ago
yes, i show my code:
public function update(Request $request)
{
\Log::info($request->all());
$request->validate([
'image' => 'required|image|mimes:jpeg,png,jpg,gif,webp,svg',
]);
$imageName = 'principal.' . $request->image->extension();
$pathToVolume = '/app/storage';
$extensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg'];
foreach ($extensions as $extension) {
$oldImage = $pathToVolume . "/principal.{$extension}";
if (File::exists($oldImage)) {
File::delete($oldImage);
}
}
$request->image->move($pathToVolume, $imageName);
return response()->json([
'success' => 'Imagen subida con éxito.',
'image' => $imageName
]);
}
public function get()
{
$extensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg'];
$imagen = '';
$pathToVolume = '/app/storage';
foreach ($extensions as $extension) {
if (file_exists($pathToVolume . "/principal.{$extension}")) {
$imagen = "principal.{$extension}";
break;
}
}
if ($imagen === '') {
return response()->json(['error' => 'Imagen no encontrada'], 404);
}
$url = secure_url($pathToVolume . '/' . $imagen);
return response()->json(['url' => $url]);
}2 years ago
Hi? Help please
2 years ago
/app/storage, in the backend