php Laravel上传到存储目录或公共图像
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Laravel上传到存储目录或公共图像相关的知识,希望对你有一定的参考价值。
https://laracasts.com/discuss/channels/laravel/how-to-display-image-saved-in-storage-folder-on-blade?page=1
<?php
/*
1) Link the storage directory to public
$ php artisan storage:link
*/
// 2) IN CONTROLLER
public function store(Request $request)
{
$tasks_count = Task::count() ;
if ( $tasks_count < 20 ) {
// dd( $request->all() ) ;
// dd($request->file('photos'));
$this->validate( $request, [
'task_title' => 'required',
'task' => 'required',
'project_id' => 'required|numeric',
'photos.*' => 'sometimes|required|mimes:png,gif,jpeg,txt,pdf,doc' // photos is an array: photos.*
]) ;
// dd($request->all() ) ;
// First save Task Info
$task = Task::create([
'project_id' => $request->project_id,
'task_title' => $request->task_title,
'task' => $request->task,
'priority' => $request->priority
]);
// Then save files using the newly created ID above
if ( $request->file('photos' ) != '' ) {
foreach ($request->photos as $file) {
// To Storage
$filename = $file->store('public'); // /storage/app/public
// filename will be saved as: public/wKZsF9ltDSNj82ynh.png
// explode this value at / and get the second element
$filename = explode("/", $filename ) ;
// If you want to save into /public/images
//$filename = $file->getClientOriginalName(); // get file name: cat.jpg
//$file->move('images',$filename);
// save to DB
TaskFiles::create([
'task_id' => $task->id,
'filename' => $filename[1] // [0] => public, [1] => wKZsF9ltDSNj82ynh.png
]);
}
}
Session::flash('success', 'Task Created') ;
return redirect()->route('task.show') ;
}
else {
Session::flash('info', 'Please delete some tasks, Demo max tasks: 20') ;
return redirect()->route('task.show') ;
}
}
// 3) IN VIEW: ?>
@extends('layout')
@section('content')
<!--
<strong>Debug vars:</strong><br>
task_view->project->id : {{ $task_view->project->id }} <br>
task_view->project->project_name: {{ $task_view->project->project_name }} <br>
task_view->id: {{ $task_view->id }}<br>
-->
<div class="col-md-8">
<h1>{{ $task_view->task_title }}</h1>
<div class="form-group">
<label>Description:</label>
<p>{!! $task_view->task !!}</p>
</div>
<div class="btn-group">
<a href="{{ route('task.edit', ['id' => $task_view->id ]) }}" class="btn btn-primary"> edit </a>
<a class="btn btn-default" href="{{ redirect()->getUrlGenerator()->previous() }}">Go Back</a>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<strong>Project: </strong>
<span class="label label-info">
<a href="{{ route('task.list', [ 'projectid' => $task_view->project->id ]) }}">{{ $task_view->project->project_name }}</a>
</span>
</div>
<div class="form-group">
@if ( $task_view->priority == 0 )
<strong>Priority: </strong><span class="label label-info">Normal</span>
@else
<strong>Priority: </strong><span class="label label-danger">High</span>
@endif
</div>
<div class="form-group">
@if ( $task_view->completed == 0 )
<strong>Status: </strong><span class="label label-warning">Open</span>
@else
<strong>Status: </strong><span class="label label-success">Closed</span>
@endif
</div>
</div>
@if( count($taskfiles) > 0 )
<h1>Files:</h1>
<div>
<ul>
@foreach ( $taskfiles as $taskfile )
<li>
<img src="<?php echo asset("storage/$taskfile->filename") ?>">
</li>
@endforeach
</ul>
</div>
@endif
@stop
<?php
/*
task
--------------------------------------------------------------------
id | project_id | task_title | task | priority | completed
task_file
---------------------------
id | task_id | filename
*/
?>
以上是关于php Laravel上传到存储目录或公共图像的主要内容,如果未能解决你的问题,请参考以下文章