简体   繁体   中英

Upload Images to Laravel-Lumen API

With my API i need to upload images from mobile app to my server and save the image path to database so i have the following issues:

  • Where to save the images? I tried to save them in the storage/app under images folder (which is work fine)

     public function fileUpload(Request $request) { if ($request->hasFile('image')) { $image = $request->file('image'); $name = time().'.'.$image->getClientOriginalExtension(); $destinationPath = storage_path('/app/images'); $image->move($destinationPath, $name); return response()->json(['data'=>"image is uploaded"]); }

    }

    and make a symbolic link to this folder in the public folder but its not working due to access permission error which will lead to the other issue.

  • What permission should i gave to the storage folder to make the whole operation works save the images and the saved link is readable (even 777 didn't work) Access forbidden!

    sudo chmod -R 777 storage

Any ideas will be much appreciated

Storage directory is not open to user's requests, public directory is, you need to create a symbolic link from storage directory to public directory:

php artisan storage:link

This command will create a symbolic link from public/storage to storage/app/public , in this case you can store your file under storage/app/public and make it accessible from the web too:

$image = $request->file('image');
$image->storeAs('public', $name); // => storage/app/public/file.img
URL::asset('storage/'.$name); // => http://example.com/stoage/file.img

I would advice you try to create a folder in the public folder and store your files there. You can use base_path()."/public/uploads"

if you problem is save on public folder, i have same problem.

public static function upload_image_file($base64_encoded = '')
{
    if (empty($base64_encoded))
        return 'default.jpg';

    $uploads_dir = base_path('public/uploads');
    $file_name = uniqid();

    $allowed_mimes = ['data:image/jpeg;base64', 'data:image/png;base64'];

    $the_image = explode(',', $base64_encoded);

    // Log::debug(var_export($the_image,true));

    $mime = $the_image[0]; // data:image/jpeg;base64
    $image = $the_image[1];
    $extension = '.jpg';

    if (!in_array($mime, $allowed_mimes))
        return response([
            'message' => ['File not allowed']
        ], 403);

    $file = fopen($uploads_dir.'/'.$file_name.$extension, 'wb');
    fwrite($file, base64_decode($image));
    fclose($file);

    return $file_name.$extension;
}

But a cant get view on subdirectory for return my front end. Return 404 ever when get at url and Angular.

The best place to store the images would inside the storage folder and create a symbolic link in the public folder.

sudo chmod -R 777 storage should work. Make sure you are accessing the right url.

To access the image you should use something like the below urls. My folder structure looks like /storage/app/public/images/user/avatar/

Local: http://localhost/project-name/public/storage/images/user/avatar/HdXzVnKxzUSdAssvXaoM2n0ixzEEG7jlK8acLiHV.png

Production: http://example.com/storage/images/user/avatar/3133L07guCv8shINNZM30c2Ux1HdfpFt04YdLRpf.png

You can check this gist to figure out if you are missing something: https://gist.github.com/danish-shaikh/9c328288a817309f93238c6b5d48ed7e

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM