2

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`é ◀"
Ostet
  • 175
  • 1
  • 12
  • Welcome, to improve your experience on SO please take the [tour] and read [ask]. You can then start writing an [On Topic question](https://stackoverflow.com/help/on-topic), then look at the [Question Check list](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist), the [perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and how to create a [mre] to improve it before submitting it. – N69S Jun 08 '22 at 16:04
  • Is it possible? Of course `$request->file('image')` returns a reference to an uploaded file, but if you want to reference a static file in your system, you can do that instead. Just have a look at the Filestorage logic in Laravel: https://laravel.com/docs/9.x/filesystem; you should be able to reference a local file with the information on that page. Follow-up question would be what do you intend to do with this file? If you just display it, like `$user->image` (or similar), you can just do `$user->image ?? $defaultImage` (which references your `default.jpg`, etc.) Please add more details – Tim Lewis Jun 08 '22 at 16:58
  • Thank for answer. I updated the question. I need $file->getClientOriginalExtension(). – Ostet Jun 09 '22 at 08:00

2 Answers2

0

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

Mash tan
  • 159
  • 11
0

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.png');
    $file = new UploadedFile(
        $path,
        'default-stadium.png',
        'image/png'
    );
}
Ostet
  • 175
  • 1
  • 12