guillermo_rojas's avatar

I can't save a image in public path --Can't write image data to path --

First of all, thank you for taking your time to read my problem.

I have the following issue and I can't figure it out. I'm trying that an User will be able to upload an image when it's created. In my store method on UserController I have this



namespace App\Http\Controllers;


use Illuminate\Http\Request;
use Laracasts\Flash;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests\UserRequest;
use App\UserPhoto;
use App\User;
use DB;
use Image;

/*Other methods*/

public function store(UserRequest $request)
    {
        $user = new User($request->all());
        $user->password = bcrypt($request->password);
        $user->save();

        if ($request->hasFile('photos')) {
            $user_photos = $request->file('photos');
            $last_user = User::orderBy('created_at', 'desc')->first();
            foreach ($user_photos as $user_photo) {

                $path = public_path('storage/users/photos/' . $user_photo->hashName());
                $img = Image::make($user_photo->getRealPath());

                $watermark = Image::make(public_path('storage/watermarks/MAK-50-Blanco.png'));

                $watermark->resize(150, 150, function ($constraint) {
                    $constraint->aspectRatio();
                });

                $img->insert($watermark, 'center');

                $img->resize(160, 160, function ($constraint) {
                    $constraint->aspectRatio();
                });
                
                // If I do this, i get a mkdir(): Permission denied
                
                /* if (!file_exists($path)) {
                    mkdir($path, 666, true);
                }*/


                $img->save($path);

                UserPhoto::create([
                    'name' => $user_photo->hashName(),
                    'user_id' => $last_user->id
                ]);
            }

        }

        flash('user created sucessfully')->success();
        return redirect('/users');
    }

An then, when I sumbit the form, this error jumps

Can't write image data to path
(/home/mito/PhpstormProjects/makpropiedades/public/storage/users/photos/Wr3kbBukXu9FEbjlYncBo2rmImpelRnY4Xgd8DYi.jpeg)
>>> in Image.php (line 143)
at Image->save('/home/mito/PhpstormProjects/makpropiedades/public/storage/users/photos/Wr3kbBukXu9FEbjlYncBo2rmImpelRnY4Xgd8DYi.jpeg')

>>> in UserController.php (line 79)

I thought when you code img->save($imgPath) this will create the folders if they're not created. Also, I've tried to sudo chmod -R app/public/storage but the same error again.

I also did

php artisan storage:link

I was following this tutorial

I don't know what to do. Please help :(

0 likes
1 reply
jlrdw's avatar

How is storage a public path? That path is above www, or htdocs, are you trying to use asset folder?,, I know for a fact this works,

    public function add(Request $request)
    {
        if (isset($_POST['submit'])) {
            echo "made it here";
            $lid = DB::table('dc_dogs')->count();
            $lid = $lid + 1;
            $file = $request->file('ufile');
            $file_name = $file->getClientOriginalName();
            $file_ext = $file->getClientOriginalExtension();

            $fileInfo = pathinfo($file_name);
            $filename = $fileInfo['filename'];
            $newname = $filename . $lid . "." . $file_ext;
            $destinationPath = ASSET . 'upload/imgdogs';
            $file->move($destinationPath, $newname);
            $dogpic = Cln::fixValue($newname);
            $dogname = ucfirst(Cln::fixValue($_POST['dogname']));
            $sex = ucfirst($_POST['sex']);
            $comments = Cln::fixValue($_POST['comments']);
            $adopted = (isset($_POST['adopted']) == '1' ? '1' : '0'); ///added
            $lastedit = date("Y-m-d H:i:s");
            echo "adpt=" . $adopted . "aaa";

            if (!isset($error)) {
                $postdata = array(
                    'dogpic' => $dogpic,
                    'dogname' => $dogname,
                    'sex' => $sex,
                    'comments' => $comments,
                    'adopted' => $adopted,
                    'lastedit' => $lastedit
                );

                DB::table('dc_dogs')->insert($postdata);
            }
        }
        $title = 'Dog add';
        $view = 'dog/add';
        $layout = ViewLayout::getLayout('admin/addtp');
        $content = View::make($view);


        return view($layout)->with('content', $content)->with('title', $title);
    }

ASSET is a constant I made, to store you use a path, but to display via src it's a url. This works for assets folder. A lot of this stuff is just resolving a path correctly

To display

<td><img src="<?php echo asset('assets/upload/imgdogs') . '/' . $row->dogpic; ?>" alt=""></td>

All works, tested laravel 5.4

In fact here you go ASSET

define('ASSET', realpath(dirname(__FILE__)). DS . 'assets' . DS);

You have to define DS also, here you go

defined('DS') || define('DS', DIRECTORY_SEPARATOR);

Ignore this part

        $title = 'Dog add';
        $view = 'dog/add';
        $layout = ViewLayout::getLayout('admin/addtp');
        $content = View::make($view);


        return view($layout)->with('content', $content)->with('title', $title);

I use php / html, not blade. Just some tweaks for me , got info from the api docs.

Please or to participate in this conversation.