laravel中的“无法创建目录”错误(在服务器上)

时间:2018-03-21 10:50:48

标签: php laravel

我正在创建一个表单,用户可以上传图片并将其与表单中的信息一起发送到服务器。一切都在本地工作正常,但当我将代码上传到服务器时,我开始收到错误。我改变了有关路径的所有代码,但它仍然给我一些错误,但至少我现在可以检索图片了。

所以现在问题。当我提交表单时,它会给我一个“无法创建”http://xxx.xx.xx/profileLogo“目录”。这是我试图将图片保存到文件夹中的时候。

以下是我认为问题来自的代码:

$path = $request->file('logo')->move( asset("/profileLogo/"), $fileNameToStore );

以下是控制器的完整代码:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\Storage;
use DB;
use File;

class CompanyProfileController extends Controller
{
/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $result= DB::table('tblcompany')->get();
    if($result==null)
    {
        DB::table('tblcompany')->insert([
            'companyName' =>' ']
     );
    }
    $pic = DB::table('tblcompany')->select('logoName')->limit(1)->value('logoName');
    if($pic==null)
    {
        DB::table('tblcompany')->update(['logoName' => 'noimage.png',]);
    }
    $data['getAllDetails']= DB::table('tblcompany')->get();

    return view('companyProfile.companyProfile', $data);
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    //
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    //
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
    //
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function edit($id)
{
    //
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(Request $request)
{
//dd(asset(''));
    $this->validate($request, [
        'companyname' =>'required',
        'shortCode' =>'required',
        'telnum' =>'required',
        'emailid' =>'required',
        'address' =>'required',

    ]);

    //$file_path =  asset('') . '/profileLogo/';
    //$file_path =  asset("/profileLogo/");

    if($request->hasFIle('logo')){
        $filenameWithExt = $request->file('logo')->getClientOriginalName();
        $filename = pathinfo($filenameWithExt,PATHINFO_FILENAME);
        $extention = $request->file('logo')->getClientOriginalExtension();
        $fileNameToStore = $filename.'_'.time().'.'.$extention;
       $pic = DB::table('tblcompany')->select('logoName')->limit(1)->value('logoName');
       if($pic!='noimage.png')
       {

            //$deletePicPath = $file_path . $pic;
            //die($deletePicPath);
            //File::delete($deletePicPath );
       }



        $path = $request->file('logo')->move(
           asset("/profileLogo/"), $fileNameToStore
        );

        DB::table('tblcompany')->update(['logoPath' => $path,'logoName' => $fileNameToStore,]);

    }


    $companyname= trim($request['companyname']);
    $shortCode= trim($request['shortCode']);
    $telnum= trim($request['telnum']);
    $emailid= trim($request['emailid']);
    $address= trim($request['address']);

    DB::table('tblcompany')->update(
            ['companyName' => $companyname,
            'shortCode' => $shortCode,
            'phoneNo' => $telnum,
            'emailAddress' => $emailid,
            'contactAddress' => $address,]
        );

        return redirect('/company-profile')->with('message','Form Updated');
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{
    //
}

}

那么我怎样才能成功进入文件夹并存储图片?

2 个答案:

答案 0 :(得分:0)

在执行任何读写操作之前,您需要提供对该目录的完全访问权限。要获得权限,您可以在Linux控制台中编写以下命令。

sudo chmod -Rf 777 /path_to_folder

在你的情况下

sudo chmod -Rf 777 /var/www/html/laravel/assets/profileLogo

确保您提供文件夹的正确路径,因为这只是示例。

答案 1 :(得分:0)

更改要保存文件的文件夹的权限。对于linux:

chmod a+rwx folder_name -R

它将更改folder_name和所有包含的文件夹的权限。 您必须将其作为系统根目录。

对于Windows:

C:\>icacls "D:\path_to_folder" /grant John:(OI)(CI)F /T