我试图通过在控制器中执行以下操作在数据库中上载多个图像:
公共函数存储(请求$request){
$pictures=[];
$input=$request->all();
if($file=$request->file('images')) $pictures[]=$request->file('images');
if($file=$request->file('image1')) $pictures[]=$request->file('image1');
if($file=$request->file('image2')) $pictures[]=$request->file('image2');
if($file=$request->file('image3')) $pictures[]=$request->file('image3');
if($file=$request->file('image4')) $pictures[]=$request->file('image4');
if($file=$request->file('image5')) $pictures[]=$request->file('image5');
if($file=$request->file('image6')) $pictures[]=$request->file('image6');
foreach($pictures as $file)
for($name=0;$name<=7;$name++)
{
$name=$file->getClientOriginalName();
}
$file->move('image',$name);
$input['images']=$name;
$input['image1']=$name;
$input['image2']=$name;
$input['image3']=$name;
$input['image4']=$name;
$input['image5']=$name;
$input['image6']=$name;
Detail::create($input);
return redirect('/');
}
它采用图像表单,并将所选图像存储在public/image文件夹中,但在数据库中,它存储具有相同图像名称的所有图像。在显示的同时,它会多次显示相同的图像。
我知道这里的人有解决这个问题的办法,也许会有更好的主意。所以请帮我解决这个问题。提前谢谢。
上面的“完成”方法不适合我,因此我在控制器中执行了此操作
public function uploadSubmit(request $request)
{
// Coming soon...
$data=$request->all();
$imagename =[];
$i = 0;
$files =Input::file('images');
foreach($files as $file){
$extension = $file->getClientOriginalExtension();
$imagename[$i] = 'post'.str_random(10).'.jpg';
$destinationPath = 'assets/posts';
$file->move($destinationPath, $imagename[$i]);
$i++;
}
Detail::create($files);
return redirect('/');
}
在路线上:
Route::resource('/details','DetailController');
现在我得到了这样一个错误:DetailController中的FatalThrowableError。php第43行:调用数组上的成员函数getClientOriginalName()。有人能指出这个问题吗。谢谢
因此,我将设置一个自定义请求来验证上传的是图像,然后您可以直接在控制器中上传它们,如下面的示例所示。
app\Http\Requests
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ImageRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
// Logic to authorize the request
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$rules = [
// Any other required laravel validations
];
if (is_array($this->request->get('images'))):
foreach ($this->request->get('images') as $key => $val):
if($key == 0) continue;
$rules['images.' . $key] = 'required|image';
endforeach;
endif;
return $rules;
}
}
应用程序\Http\控制器\ImageController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\ImageRequest;
class ImageController extends Controller
{
public function store(ImageRequest $request) {
$uploadedImagesPath = array();
foreach($request->input("images") as $key => $image):
$thisFile = $request->file("images")[$key];
if($thisFile->isValid()):
// Get original file name
$imageName = $thisFile->getClientOriginalName();
// Upload file to default Laravel configured upload location
$uploadedImagesPath[$key] = $thisFile->storeAs('images', $imageName);
// Function was in your example code
// Detail::create($request->file("images"));
endif;
endforeach;
return redirect('/');
}
}
你可以在Laravel的网站上阅读更多关于这个主题的内容
https://laravel.com/docs/5.4/requests#retrieving-uploaded-files
https://laravel.com/docs/5.4/validation#form-request-validation
我是PHP的新手,需要在一个项目中弄清楚这一点--我用HTML制作了一个image submit表单,它将div中的img更改为使用该表单选择的图像。下面是我如何完成的: 而且 我需要将这个图像上传到我为每个用户在MySql数据库中的medium Blob创建的列中。我尝试了许多不同的方法(尝试了等),但不知道我在做什么。 我现在正在学习本教程-http://www.sevenkb.com/php
问题内容: 我试图在数据库表的一行中上传多个图像并在显示页面中访问它们。我已经尝试过本教程: laraveldaily.com/upload-multiple- files-laravel-5-4/, 但是有两个不同的表建立关系。 我希望这在单个表中发生。 问题答案: 这是最适合我的: 首先以您的形式执行此操作: 这是多项选择: 现在在您的控制器中执行以下操作: 希望这行得通
我尝试了很多解决办法,但没有一次对我有效。 我以这种方式接收图像数据: 和使用multipart上载图像 我的接口是: 但仍收到null响应,而不是true或FALSE: 提前致谢
问题内容: 我必须使用JavaScript创建一个表单,用户将上载JPG文件并与其他信息(例如名称,电子邮件等)一起提交。当用户单击提交时,表单中的所有信息将被加载到值对象中。对于图像文件,我将其设置为。 所以假设: 我还设置了一个servlet来处理提交,但是我不确定如何开始。上传如何进行?用户提交时,如何获取图像信息?这是屏幕截图:http : //imageshack.us/f/32/776
被一个问题缠住了。我是android开发的新手。我的问题是我有一个SQLite数据库,我正在其中保存映像和一些数据,但当我检索这些数据时,映像没有显示在ListView中。其他数据正在被推翻。 这是我的自定义列表布局 null 这是我的自定义适配器 } 这是activity 这是数据列表
app.py result.html