vipin93's avatar

How to delete image after put image S3

I try to delete image after uploaded successfully to the S3 bucket but it not delete

 if (Input::hasFile('file'))
        {

        $file       = Input::file('file');

        $name       = time() . '-' . $file->getClientOriginalName();

        $file       = $file->move(public_path(). '/images/'.Auth::user()->username .'/', $name);

        $image      = Imag::make($file->getRealPath())->resize('1024','700')->save($file);

        $namee      = Flysystem::put($file, $image);

        Flysystem::connection('local')->delete($file);

        $img->file  = $namee;

        
        }

And path or URL of image not saving in databse thanks

0 likes
4 replies
michaeldyrynda's avatar

At no point in this snippet do you actually save the $img instance.

Also, you have a typo in the line $image = Imag::make(...) - should that be Image::make? If it's returning null, you're writing null to the filesystem. Can you verify that the file is actually uploaded?

vipin93's avatar

@deringer

public function store()
    {
         $input = Input::all();

         $this->uploadImageForm->validate($input);
         //$user = Auth::user();
         $img = new Image;

         $img->title          = Input::get('title');
         $img->description    = Input::get('description');
         $img->status         = Input::get('status');
         $img->user_id        = Auth::user()->id;
         //$user->images()->attach($img->id);

          if (Input::hasFile('file'))
        {

        $file       = Input::file('file');

        $name       = time() . '-' . $file->getClientOriginalName();

        $file       = $file->move(public_path(). '/images/'.Auth::user()->username .'/', $name);

        $image      = Imag::make($file->getRealPath())->resize('1024','700')->save($file);

        $namee      = Flysystem::put($file, $image);

        Flysystem::connection('local')->delete($file);

        $img->file  = $namee;

        
        } 

       // 
           // Add the connection between the image and the user

         if ($img->save()) 
         {
            return Redirect::back()->with(['global' => 'Your image file has been uploaded successfully.', 'type' => 'success']);
         }else
         {
            return Redirect::back()->with(['global'=> 'Your upload could not be succeeded.' , 'type' => 'danger']);
         }
    }

I can see in public folder uploaded image and my bucket it make folder "C:\lamp\www\flash\public" and inside it make folder "images" and inside it image name "Vipin\1425369470-beautiful_girls_10-wallpaper-1366x768.jpg"

vipin93's avatar

any one know how to store url of s3 into database as my above

$namee     = Flysystem::put($file, $image);
$url           = Flysystem::read($file, $image);
$img->file =  $url;

But i dint work in my database file column ??

vipin93's avatar
  if (Input::hasFile('file'))
        {

        $file       = Input::file('file');

        $name       = time() . '-' . $file->getClientOriginalName();

                  $path       = Auth::user()->username.'/'.$name;

        $file       = $file->move(storage_path().'/files/', $name);

        $image      = Imag::make($file->getRealPath())->resize('1024','700')->save($file);

            Flysystem::put($path,$image);
 
        $img->file  = 'https://s3-us-west-2.amazonaws.com/getdukaan/'.$path; 

    
          if (File::exists(storage_path().'/files/'. $name))
           {
              File::delete(storage_path(). '/files/'. $name);

               
           } 
       $img->save();       
        } 

Its working fine now after very hard work withe google

Please or to participate in this conversation.