ユーザーがプロフィール画像を更新した場合、古いユーザー画像を削除しようとしています。私はLaravel 4.1と多忙なUsersControllerを使用しています。Laravel 4、ポスト方式で画像を削除/可能ですか?
画像を更新すると問題はありません。新しいものが私のフォルダに保存され、ファイルネーブはデータベースに上書きされます。しかし、私は古い画像を削除したいと思います。したがって、私はルートに空のページにそれを使用する場合、私は正常に動作し、次のコードを、持っている「試験」を取得例えば、私は、画像の更新処理に古いものを、これを実装しよう
$oldimage = Auth::user()->profile_picture;
File::delete('img/profile_pictures/users/' . $oldimage);
毎回削除されません。私はすでにファイル名を上書きする前に古いものを削除しなければならないことを覚えています。
これはコントローラが更新に使用するPOSTメソッドで何かする必要がありますか?どうすれば修正できますか?
public function update($id){
$validation = Validator::make(
Input::all(), [
'name'=>'required',
'surname'=>'required',
'email'=>'required',
'email' => 'email']);
if($validation->fails()){
return Redirect::to(
'dashboard#profile'
)->withInput()->withErrors(
$validation->messages());
}
$user = User::find($id);
$user->name = Input::get('name');
$user->surname = Input::get('surname');
$user->email = Input::get('email');
if (Input::hasFile('profile_picture_update')) {
$oldimage = Auth::user()->profile_picture;
File::delete('img/profile_pictures/users/' . $oldimage);
}
$imageFile = Input::file('profile_picture_update');
$newprofile_picture = Image::make(
$imageFile->getRealPath()
)->resize(400, null, true)->crop(400, 400);
$name = time() . '-' .
Input::get('name') .
'-' . Input::get('surname') .
'.' . $imageFile->getClientOriginalExtension();
// $name = time() . '-' . $profile_picture->getClientOriginalName();
// Below the profile_picture variable is overrridden.
$newprofile_picture = $newprofile_picture->save(
public_path().'/img/profile_pictures/users/'.$name
);
$user->profile_picture = $name;
$user->save();
return Redirect::to(
'dashboard#profile'
)->with(
'updatemessage', 'Yep! Deine Änderungen wurden gespeichert.'
);
}
と交換してください。投稿リクエストを処理するControllersメソッドの完全なコードを投稿できますか? – chris342423
ここで行った – patrick
アクセス権は私の最初の推測だろう。 second:なぜ実際にファイルを削除する必要がありますか?単純に上書きします。 – Kjell