Php Laravel文件上载在godaddy服务器中不工作
在我的Php Laravel文件上载在godaddy服务器中不工作,php,laravel,file-upload,Php,Laravel,File Upload,在我的laravel项目中上载文件时遇到问题。它在本地工作正常,但在godaddy服务器上不工作。我使用以下方法存储我的文件 第一种方法: $image = $request->file('avatar_file'); $imageName = time().'.'.request()->avatar_file>getClientOriginalExtension(); $destinationPath = public_path().'/images/' ; request(
laravel
项目中上载文件时遇到问题。它在本地工作正常,但在godaddy
服务器上不工作。我使用以下方法存储我的文件
第一种方法:
$image = $request->file('avatar_file');
$imageName = time().'.'.request()->avatar_file>getClientOriginalExtension();
$destinationPath = public_path().'/images/' ;
request()->avatar_file->move($destinationPath, $imageName);
第二种方法:
$image = $request->file('file');
$imageName = time().'.'.request()->file->getClientOriginalExtension();
$request->file('file')->storeAs('documents', $imageName);
我已为此在本地而非服务器上工作创建了符号链接。由于您在共享托管平台上托管了Laravel应用程序,您的应用程序不知道到公用目录的正确路径 因此,您需要在
public/index.php
文件中定义如下函数:(将其粘贴在index.php
文件的顶部)
在此处定义此函数会导致跳过同名的helper函数,从而允许重写其功能。这是为Laravel的“公共”目录使用“非标准”位置所必需的
现在为您的路径创建一个新的服务提供商:
php artisan make:PublicPathServiceProvider
在register()
函数中,您需要以下代码:
public function register()
{
if (env('PUBLIC_PATH') !== null) {
//An example that demonstrates setting Laravel's public path.
$this->app['path.public'] = base_path().'/../'.env('PUBLIC_PATH');
} else {
$this->app['path.public'] = base_path().'/../public_html';
}
// Possible environment changes
if ($this->app->environment() === 'local') {
} elseif ($this->app->environment() === 'test') {
} elseif ($this->app->environment() === 'production') {
}
}
现在,在您的应用程序中注册此提供商。
config/app.php
在“提供程序”下添加以下内容:
App\Providers\PublicPathServiceProvider::class,
最后一步,在.env
文件中,创建一个新变量:
PUBLIC\u PATH=/PUBLIC\u html
你可以走了
注意:不要忘记检查您上传Laravel应用程序的路径。(以上假设路径为public_html)
重要提示:更改配置后,可能需要清除缓存:
php-artisan-config:cache
php artisan缓存:清除
public function register()
{
if (env('PUBLIC_PATH') !== null) {
//An example that demonstrates setting Laravel's public path.
$this->app['path.public'] = base_path().'/../'.env('PUBLIC_PATH');
} else {
$this->app['path.public'] = base_path().'/../public_html';
}
// Possible environment changes
if ($this->app->environment() === 'local') {
} elseif ($this->app->environment() === 'test') {
} elseif ($this->app->environment() === 'production') {
}
}