I need to upload data from a web form, including a file. However, the user does not need to download the file. And if the file is not loaded, then Laravel itself should load some default file, for example, public/images/deault.jpg.
But I don't understand how I can add it to an already received request instead of $file = $request->file('image')
Is it possible?
UPD: I already tried file_get_contents() and it doesn't work. I need to use the $file->getClientOriginalExtension() method on the received file. If the file is obtained via file_get_contents(), then this is not possible.
$request->file('image'):
Illuminate\Http\UploadedFile {#1505 ▼
-test: false
-originalName: "1393001921377.jpg"
-mimeType: "image/jpeg"
-error: 0
#hashName: null
path: "C:\xampp\tmp"
filename: "phpE7CB.tmp"
basename: "phpE7CB.tmp"
pathname: "C:\xampp\tmp\phpE7CB.tmp"
extension: "tmp"
realPath: "C:\xampp\tmp\phpE7CB.tmp"
aTime: 2022-06-09 07:32:27
mTime: 2022-06-09 07:32:27
cTime: 2022-06-09 07:32:27
inode: 3096224744023481
size: 48222
perms: 0100666
owner: 0
group: 0
type: "file"
writable: true
readable: true
executable: false
file: true
dir: false
link: false
linkTarget: "C:\xampp\tmp\phpE7CB.tmp"
}
I need same object, but file_get_contents('img/test.png') get:
b"ëPNG\x1A\x00\x00\x00IHDR\x00\x00\x00@\x00\x00\x00@\x08\x03\x00\x00\x00ØÀüý\x00\x00\x01\vPLTE÷÷÷┴┴┴ííí;;;HHH¶¶¶???¸¸¸¨¨¨OOOÔÔÔ°°°───mmmçççfffZZZ§§§···QQQGGG±±±MMMÞÞÞ¹¹¹CCCåååÒÒÒ@@@KKK┼┼┼ÂÂÁ\x10Íô}I Xƒö¬ÐPıl\x19\x03\x0E°|%Èi\x03B└\x03-\x04ü$Iƶâ\x0FÎ^-═³╗ð\x15\x00\x00\x00\x00IEND«B`é ◀"
CodePudding user response:
To get the file:
$fileContent = file_get_contents($filePath);
Then you can either simply paste the content into the database or copy the file to a desired location. I suggest following this answer regarding how to upload files from a path. Upload a file using file_get_contents
CodePudding user response:
I found answer in Laravel - file path to UploadedFile instance
Need create new UploadedFile object:
if ($request['image']) {
$file = $request->file('image');
} else {
$path = public_path('images/deault.jpg');
$file = new UploadedFile(
$path,
'default-stadium.png',
'image/png'
);
}