Php 如何在Laravel 4中更新文件上载?
我已经建立了一些我希望能工作的东西。它应该上传文件并用新文件的路径替换记录的“附件”条目。方法如下:Php 如何在Laravel 4中更新文件上载?,php,laravel,Php,Laravel,我已经建立了一些我希望能工作的东西。它应该上传文件并用新文件的路径替换记录的“附件”条目。方法如下: public function update($id) { $article = Article::find($id); $article->username = Session::get('username'); $article->body = Input::get('body'); $article->title = Input::get(
public function update($id)
{
$article = Article::find($id);
$article->username = Session::get('username');
$article->body = Input::get('body');
$article->title = Input::get('title');
if(Input::hasFile('attachment'))
{
$file = Input::file('attachment');
$name = time() . '-' . $file->getClientOriginalName();
$file = $file->move(public_path() . '/documents/articles/', $name);
$article->attachment = $name;;
}
$article->save();//Commit this edit to the database
return Redirect::route('articles.index');
}
及表格:
<div class="form-general">
{{ Form::model($article, array('route' => array('articles.update', $article->id, 'files' => true), 'method' => 'PUT')) }}
<div>
{{ Form::label('title', 'Title:') }}
{{ Form::text('title', null, array('class' => 'form-control' )) }}
</div>
<br />
<div>
@if(! empty($article->attachment))
{{ Form::label('attachment', 'Attachment:') }} <br />
<div class="attachment-replace" style="display:none;">
{{ Form::file('attachment', null, array('class' => 'form-control' )) }}
</div>
<a class="current-attachment" href="/documents/articles/{{ $article->attachment }}">{{ $article->attachment }}</a> <a href="#" class="current-attachment"><span class="glyphicon glyphicon-remove-circle remove-attachment"></span></a>
@else
{{ Form::label('attachment', 'Attachment:') }}
{{ Form::file('attachment', null, array('class' => 'form-control' )) }}
@endif
</div>
<br />
<div>
{{ Form::label('body', 'Body:') }}
{{ Form::textarea('body', null, array('class' => 'form-control' )) }}
</div>
<br />
<div>{{ Form::submit('Edit Post', array('class' => 'btn btn-default')) }}</div>
{{ Form::close() }}
</div>
{Form::model($article,array('route'=>array('articles.update',$article->id,'files'=>true),'method'=>PUT'))}
{{Form::label('title','title:')}
{{Form::text('title',null,array('class'=>'Form control'))}
@如果(!空($article->attachment))
{{Form::label('attachment','attachment:')}
{{Form::file('attachment',null,array('class'=>'Form control'))}
@否则
{{Form::label('附件','附件:')}
{{Form::file('attachment',null,array('class'=>'Form control'))}
@恩迪夫
{{Form::label('body','body:')}
{{Form::textarea('body',null,array('class'=>'Form control'))}
{{Form::submit('Edit Post',array('class'=>'btn btn default'))}
{{Form::close()}}
目前,什么都没有发生。该方法将被执行,其他字段将正常更新,但附件不会。文件输入“附件”似乎从未设置过。如何正确更新文件路径并立即上载其替换文档?您的
files
参数是错误数组的一部分。您有一行,它将打开您的表单:
{{Form::model($article,array('route'=>array('articles.update',$article->id,'files'=>true),'method'=>PUT'))}
如果仔细观察,您会发现files=>true
选项是route
数组的一部分,但是它应该在数组之外。像这样:
{{Form::model($article,array('route'=>array('articles.update',$article->id),'files'=>true,'method'=>PUT'))}
正如他们所说,魔鬼在于细节;) 你能确保它进入
hasFile
if吗?只需在那里添加dd('test')
。它似乎没有。我想这意味着它从未看到文件被附加,但我仍然不确定为什么会发生这种情况。我将“files”设置为true,输入加载并允许选择文件。Ok。我现在只是在抓救命稻草,但可能有问题,因为您使用的是Form::model
<代码>$article->attachment是路径,名为attachment的字段是文件上载。尝试将文件上载重命名为其他内容。我不确定是否理解您的建议。你建议我重命名什么$article->attachment指的是名为attachment的字段。是的,我知道,我想知道这是否是问题所在。因此,将Form::file('attachment')
重命名为Form::file('attachmentInput')
,并对Input::hasFile
和Input::file
执行相同的操作,但保持原样。这只是为了测试,我知道这有点违背了表单模型背后的概念……谢谢,先生。我很感激。我没有意识到我把它嵌套在路由的数组中。我以后必须更加小心。