@
@
@Twitt2017-08-24 20:16:29
Laravel
@Twitt, 2017-08-24 20:16:29

What's weird about uploading images?

Created an Image model to load a file into it, here is its code:

class Image
{
    public $file;
    
    public function hash() {
        return $this->file->hashName();
    }
    
    public function saveFile() {
        $name = $this->hash();
        if($this->file->move(public_path() . '/imgs', $name)) {
            return $name;
        }
    }
}

In the controller, I need to accept this file from the request, push it into the Image model, call the saveFile () method in it, and pass the name of the loaded image (hashed, of course) to another model to save it in the database.
It turns out, but not as it should..
The really hashed file name is loaded into the imgs folder, and what is in the temp folder is stored in the database, for some reason:
D:\OServer\OpenServer\userdata\temp\php1BD6.tmp

Controller Action:
public function store(JournalStoreRequest $request) {
        $journal = new Journal;
        $image = new Image;
        $image->file = $request->file('image');
        $journal->image = $image->saveFile();
        if($journal->fill($request->all()) && $journal->save()) {
            redirect()->route('journals.index');
        }
    }

Why is the unhashed name stored in the database? Despite the fact that here, in theory, just this is how it should be transmitted, the hashed value
$journal->image = $image->saveFile();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
d-virt, 2017-08-24
_

Replaced by:
And DO NOT use (but meaningfully you can)$request->all()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question