【发布时间】:2016-11-15 17:48:44
【问题描述】:
我正在网站上制作一个功能,可以让用户上传图像并且图像将存储在项目目录中。图像路径也创建并存储在数据库中,以便可以使用路径获取图像。
本网站现在在 Windows Server 2008 R2 上使用 Laravel 5.3 和 PHP 7 和 MariaDB 10.1.6。当我尝试上传时,我发现目录和数据库中没有存储任何图像和路径,数据库的图像路径(我命名为imgsrc)列只存储了NULL,即使我选择了图像。
这是我的观点:
<form action="/task" method="POST" class="form-horizontal" enctype="multipart/form-data">
<!-- Bypass other input part -->
<div class="row">
<label for="task-image" class="col-sm-3 control-label">Image</label>
<div class="col-sm-6">
<input type="file" name="imgsrc" id="task-image" class="form-control" accept="image/*">
</div>
</div>
<!-- Bypass other input part -->
</form>
这是我的控制器部分:
<?php
namespace App\Http\Controllers;
use Image; // Intervention\Image
use App\Task;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
class crud_controller extends Controller
{
public function create_crud (Request $request)
{
/* Bypass other part */
if ( $request->hasFile('image') )
{
if ( $request->hasFile('photo') )
{
$filename = rand(10,100).$request->file('photo')->getClientOriginalName();
$formal_file = 'storage/images/'.$filename;
$img_file = Image::make( Input::file('photo') )
->resize('400',null)->save($formal_file);
// Fllowing part, none of all will fix problem.
// $image->save(storage_path('app/blogpost/' . $postId . '/' . $imageName));
// storage_path('storage/images'.);
// $img_file = Image::make( Input::file('photo') ->resize('400',null)->move(public_path().'/images/vijesti', $filename);
$task->imgsrc = $request->$formal_file;
}
}
$task->save();
return redirect('/');
}
/* Bypass other part */
}
我阅读了 Laravel 的文档和 Stack Overflow 上的一些问题,并尝试使用它们。但是,数据库的imgsrc 列仍然存储NULL,并且没有目录存储图像。
我的代码有什么问题?
【问题讨论】: