我正在创建一个表单,用户可以在其中上传图片并将其与表单中的信息一起发送到服务器。在本地一切正常,但是当我将代码上传到服务器时,我开始出现错误。我更改了有关路径的所有代码,但它仍然给我一些错误,但至少我现在可以检索图片。
所以现在要来谈谈问题。当我提交表单时,它给了我一个"无法创建"http://xxx.xx.xx/profileLogo"目录"。这是我尝试将图片保存到文件夹中的时候。
这是我认为问题来自的代码:
$path = $request->file('logo')->move(
asset("/profileLogo/"), $fileNameToStore
);
以下是控制器的完整代码:
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppHttpRequests;
use IlluminateSupportFacadesStorage;
use DB;
use File;
class CompanyProfileController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
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 IlluminateHttpResponse
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param IlluminateHttpRequest $request
* @return IlluminateHttpResponse
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return IlluminateHttpResponse
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return IlluminateHttpResponse
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param IlluminateHttpRequest $request
* @param int $id
* @return IlluminateHttpResponse
*/
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 IlluminateHttpResponse
*/
public function destroy($id)
{
//
}
}
那么如何才能成功转到文件夹并存储图片呢?
更改要保存文件的文件夹的权限。对于 linux:
chmod a+rwx folder_name -R
它将更改folder_name和包含的所有文件夹的权限。您必须以系统根身份执行此操作。
对于窗口:
C:>icacls "D:path_to_folder" /grant John:(OI)(CI)F /T
如果您是 linux,则可以登录到服务器并导航到要在其中放置上传文件夹的文件夹,然后使用
mkdir uploads
之后,为上传文件夹授予读取和写入权限,如下所示
sudo chmod -R 777 [path-to-your-uploads-folder]
这就是我所做的,它工作得很好
在执行任何读写操作之前,您需要授予对该目录的完全访问权限。要授予权限,您可以在 linux 控制台中编写以下命令。
sudo chmod -Rf 777 /path_to_folder
在您的情况下
sudo chmod -Rf 777 /var/www/html/laravel/assets/profileLogo
确保提供文件夹的正确路径,因为这只是示例。