【问题标题】:Laravel behaving madly - showing wrong imagesLaravel 行为疯狂 - 显示错误的图像
【发布时间】:2016-07-03 14:44:16
【问题描述】:

我有一个网站 satoshirps.site

当新用户注册时,他的个人资料照片仍然是空白的。 然后去Upload picture上传头像。

返回仪表板 (www.satoshirps.site/dashboard) 后,用户会看到他上传的个人资料图片。现在转到delete picture 删除当前头像,然后使用上传上传另一张头像。

这次返回仪表板给出了旧图片(这很奇怪)。

当我尝试这个东西时,我第二次成功上传了图片(我检查了我的应用程序文件夹中的文件)。然后我没有得到正确的图像。 lavarel 是否有缓存或者这是一个 cookie 问题。我再次尝试删除浏览器的缓存和cookie,但没有任何效果。

上传页面:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Upload</title>
</head>
<body>
  @if(Session::has('success'))
      {{ Session::get('success') }}
@endif

@if(Session::has('error'))
        {{ Session::get('error') }}
@endif

  {{ Form::open(array('url' => 'upload', 'files' => true, 'method' => 'post')) }}
  {!! csrf_field() !!}
  {{ Form::file('image') }}
  {{ Form::submit('Upload') }}
  {{ Form::close() }}
</body>
</html>

控制器

<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Input;
use Session;
use Redirect;
use Image;
use Storage;
use App\User;
use File;


class HomeController extends Controller
{
  /**
  * Create a new controller instance.
  *
  * @return void
  */
  public function __construct()
  {
    $this->middleware('auth');
  }

  /**
  * Show the application dashboard.
  *
  * @return \Illuminate\Http\Response
  */
  public function index()
  {
    return view('home');
  }

  public function dashboard()
  {
    $user = Auth::user();
    return view('dashboard', compact('user'));
  }

  public function upload()
  {
    $user = Auth::user();
    return view('upload', compact('user'));
  }

  public function uploadsave(Request $request)
  {
    $user = Auth::user();
    if (Input::file('image')->isValid()) {
      $filename = 'profilepics/'.$user->username.'.jpg';
      File::delete($filename);
      $img = Image::make($_FILES['image']['tmp_name'])->encode('jpg',75)->resize(200,200)->save('profilepics/'.$user->username.'.jpg');
      $dbuser = User::where('id', $user->id)->first();
      $dbuser->photo_path = '/profilepics/'.$user->username.'.jpg';
      $dbuser->save();  //update user's profile picture location
      Session::flash('success', 'Upload successfull');
      return Redirect::to('upload');
    }
    else {
      // sending back with error message.
      Session::flash('error', 'Uploaded file is not valid');
      return Redirect::to('upload');
    }
  }


  public function deletephoto()
  {
    $user = Auth::user();
    $filename = 'profilepics/'.$user->username.'.jpg';
    File::delete($filename);
    if (file_exists($filename)) {
      echo "The image is not deleted";
    } else {
      echo "The image is deleted";
    }
  }
}

还有路由文件

<?php
Route::get('/', function () {
  return view('welcome');
});
Route::group(['middleware' => 'web'], function () {
  Route::auth();
  Route::get('/home', 'HomeController@index');
  Route::get('/dashboard', 'HomeController@dashboard');
  Route::get('/upload', 'HomeController@upload');
  Route::post('/upload', 'HomeController@uploadsave');
  Route::get('delete', 'HomeController@deletephoto');
});

此外,如果您在删除文件后访问仪表板,您将看到您的旧头像

请一些人试试这个并帮助我。

【问题讨论】:

  • 新上传后按CTRL+F5是否有效?
  • 您可以自己查看。我提供了网站链接,但您的方法也不起作用。
  • 它说您的网站无法访问(编辑:您的网址不包括 .site)
  • 该网站有时可能会关闭,因为我是自托管的,并且它在有时会过热的笔记本电脑上运行。但是现在检查一下

标签: php laravel


【解决方案1】:

the documentation,删除文件你应该使用Storage::delete($filename)

将您的图片保存在存储目录中,并通过在您的UsersController 中创建如下路由来提供它们:

public function profilePicture($user){
    $profile_picture_url = storage_path().'/profile_pictures/'.$user['id'].'/profile.jpg';

    if(!File::exists( $profile_picture_url ))
        App::abort(404);

    return Image::make($profile_picture_url)->response('jpg');
}

然后,在您的视图中,使用来自 Laravel 的 HTMLURL 助手创建 img 标签:

HTML::image(URL::action('UsersController@profilePicture', array($user['id'])))

确保您已安装 Intervention package 并且一切顺利

【讨论】:

  • Storage::delete($filename) 将从 App\storage\app\public\profilepics 中删除文件(不会退出)。但是图片存储在 App\public\profilepics
  • 阅读完整文档,在配置部分明确提到documentation
  • 为什么不将图片保存在存储文件夹中?
  • 我也试过了,但出现了另一个问题,即如何检索它们,因为“存储”会给出存储的 jpg 内容而不是文件名。
  • 我也尝试通过更改 config/filesystem.php 中的存储路径,但没有任何效果。
猜你喜欢
  • 2011-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-24
  • 2011-01-28
  • 1970-01-01
  • 1970-01-01
  • 2011-08-21
相关资源
最近更新 更多