Ad

How To Upload Image Or Large File To Temp Folder Before Submit Form In Laravel

- 1 answer

I'm using this code to upload files or images. It's working but can't upload a large file and I want to upload a file when selecting it from the local computer like the below image. enter image description here

I use below PHP code in the controller.

$image = $request->file('file_upload');
        $new_name = rand() . '.' . $image->getClientOriginalExtension();
        echo $new_name;
        $image->move(public_path('images'), $new_name);
Ad

Answer

Try to use this code.

Change the $request->sharing_file with your field name.

You can increase the $size for image what you want, currently it is 16mb and working fine.

$myimage = $request->image;

$size = getClientSize();
if($sizes < 16777216){

$fileMimeType = explode('/', $myimage->getClientMimeType());
$fileType = $fileMimeType[0];

$originalFileName = substr($myimage->getClientOriginalName(), 0, strpos($myimage->getClientOriginalName(), "."));

$originalFileName = substr(str_replace(' ', '-', $originalFileName),0,10);
$rand = rand(9,1000);

$fileName = $rand.'-'.$originalFileName.'.'.$myimage->getClientOriginalExtension();

$upload = $values->move(public_path('images'), $fileName);

if($upload) {
    $message = 'File Uploaded';
} else {
    $message = "Failed to upload file";
}

}
else {
    $message = 'Files size should be less than 16 MB.';
}
Ad
source: stackoverflow.com
Ad