dongyan8929 2016-03-17 10:10
浏览 66

Laravel表现得很疯狂 - 显示错误的图像

I have a website satoshirps.site

When a new user registers, his profile photo remains empty. Then goto Upload picture to upload a profile picture.

After returning back to dashboard (www.satoshirps.site/dashboard) the user see his uploaded profile picture. Now goto delete picture to delete the current profile picture and then upload an another profile picture using upload.

This time returning to dashboard gives the old picture(which is weird).

When I tried this thing, I uploaded the picture successfully for the second time (I checked the file in my app's folder). Then I was not getting the correct image. Does lavarel have cache or this is a cookie problem. I tried again by deleting browser's cache and cookie but nothing worked.

The upload page :

<!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>

The controller

<?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";
    }
  }
}

And the routes file

<?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');
});

Also if you visit dashboard after deleting the file you will see your old profile picture

Please some will please try this and help me.

  • 写回答

1条回答 默认 最新

  • duanqujing3863 2016-03-17 12:33
    关注

    From the documentation, to delete files you should use Storage::delete($filename)

    Save your pictures in the storage directory and serve them by creating a route as follows in your 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');
    }
    

    Then, in your Views, create img tags using the HTML and the URL helper from Laravel:

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

    Make sure you have the Intervention package installed and you are good to go

    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)