laravel - str_random的问题

时间:2016-04-12 08:18:10

标签: php laravel variables random

我有这个代码用Laravel上传图片:

f=open('yourfilename','r').read()
f1=f.split('\n')
p=open('outputfilename','w')
for i in range (len(f1)):
    p.write(str(f1[i])+'\n')
p.close()

所以图片上传一切正常,但当我看到我的 public function store(Requests\ArticleRequest $request) { $article['photo']= 'http://nationaluasi.com/dru/content/hotelIcon.png'; $file = array('image' => $request->file('image')); // setting up rules $rules = array('image' => 'required',); //mimes:jpeg,bmp,png and for max size max:10000 // doing the validation, passing post data, rules and the messages $validator = Validator::make($file, $rules); if ($validator->fails()) { // send back to the page with the input data and errors $article['photo'] = null; } else { // checking file is valid. if ($request->file('image')->isValid()) { $destinationPath = public_path().'/images'; // upload path $extension = $request->file('image')->getClientOriginalExtension(); // getting image extension $article['photo'] = str_random(5).'.'.$extension; // renameing image $request->file('image')->move($destinationPath, $article['photo']); // uploading file to given path // sending back with message } else { } } $article = new Article($request->all()); $article['key']= str_random(30); Auth::user()->articles()->save($article); Alert::success('nnn','Good job!')->persistent("Close"); return redirect('auctions'); } 文件夹时,图片名称为/public/images,但在数据库中存储了DiYhh.png

为什么,探测器是什么? 为什么不在数据库/tmp/phpVu3QOa列中存储相同的名称(image)而不是/ tmp / ... ???

1 个答案:

答案 0 :(得分:1)

在您的代码中,首先将文章设置为某个内容,然后覆盖它。所以当你这样做时:

$article['photo']  = str_random(5).'.'.$extension;
$request->file('image')->move($destinationPath, $article['photo']);

您已成功将文件移至正确的位置。但接下来几行,你这样做:

$article = new Article($request->all());

现在,代码忘记了$article['photo']包含您想要的文件名 - 因为它已被覆盖。

我建议将新文章行进一步移到顶部,然后对该对象进行更改,最后在验证完所有内容后保存它。