欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

保存图像图片 自动创建目录 laravel image save img auto create directory Saving Intervention Image In Owners Folder in Laravel 5 有大用

I can change my code to save the uploaded image in the public dir but not when I want to their uploaded image in a folder as their company's name. For example of what works:
/public/company_img/<filename>.jpg
       

If the user's company name is Foo, I want this when they save save their uploaded image:

/public/company_img/foo/<filename>.jpg
       

This is in my controller:

$image = Input::file('company_logo');
$filename = $image->getClientOriginalName();
$path = public_path('company_img/' . Auth::user()->company_name . '/' . $filename);

// I am saying to create the dir if it's not there.
File::exists($path) or File::makeDirectory($path); // this seems to be the issue

// saving the file
Image::make($image->getRealPath())->resize('280', '200')->save($path);
       

Just looking at that you can easily see what it's doing. My logs shows nothing and the browser goes blank after I hit the update button. Any ideas

shareimprove this question            





    1 Answer  正确答案 

    activeoldestvotes            

    up vote4down voteaccepted            
    File::exists($path) or File::makeDirectory($path);
    
                   

    This line does not make sense, as you check if a file exists and if not you want to attempt to create a folder ( in your $path variable you saved a path to a file not to a directory )

    I would do something like that:

            // directory name relative to public_path()
        $dir = public_path("company_img/username"); // set your own directory name there
    
        $filename = "test.jpg"; // get your own filename here
    
        $path = $dir."/".$filename;
    
        // check if $folder is a directory
        if( ! \File::isDirectory($dir) ) {
    
            // Params:
            // $dir = name of new directory
            //
            // 493 = $mode of mkdir() function that is used file File::makeDirectory (493 is used by default in \File::makeDirectory
            //
            // true -> this says, that folders are created recursively here! Example:
            // you want to create a directory in company_img/username and the folder company_img does not
            // exist. This function will fail without setting the 3rd param to true
            // http://php.net/mkdir  is used by this function
    
            \File::makeDirectory($dir, 493, true);
           // 493 什么意思 应该是 0755吧    }    // now save your image to your $path
                   

    But i really can't say your behaviour has something to do with that... Without error messages, we can only guess.



      来自  https://stackoverflow.com/questions/29534316/saving-intervention-image-in-owners-folder-in-laravel-5

      Create folder inside public path when uploading images (L5.2)

      PUBLISHED 2 YEARS AGO BY SUBBE

      I am trying to create folders based on the user id, before uploading and saving images in this folder.

      In L4.2, i did

      if (! File::exists("uploads/properties")) {
          File::makeDirectory("uploads/properties");
      }
      
                     

      tried the same on L5.2 and it fails. Anyone got any idea to go around this?

      Thanks

      Best Answer(As Selected By subbe)                    
      Snapey                                
      Snapey                                        
      2 years ago                                    

      I think you need to specify the full path to the folder you want to create.

      if (! File::exists(public_path()."uploads/properties")) {
          File::makeDirectory(public_path()."uploads/properties");
      }
      
                                                 

      Without trying it, you may need a backslash between the public path and your uploads folder                                            

      bestmomo                    
       bestmomo                            
      2 years ago(349,010 XP)                        

      When you says it fails, what is the error ?

      kingpabel                    
       kingpabel                            
      2 years ago(6,765 XP)                        

      May be you are facing,file permission problem.If my guess is right give 777 to parent directory where you want to create a folder

      Snapey                    
       Snapey                            
      2 years ago(872,355 XP)                        

      I think you need to specify the full path to the folder you want to create.

      if (! File::exists(public_path()."uploads/properties")) {
          File::makeDirectory(public_path()."uploads/properties");
      }
      
                                     

      Without trying it, you may need a backslash between the public path and your uploads folder                                

      Please sign in or create an account to participate in this conversation.


      来自  https://laracasts.com/discuss/channels/laravel/create-folder-inside-public-path-when-uploading-image...

      普通分类: