2016-01-10 3 views
1

Мне нужна помощь в удалении фотографии из моей папки в Laravel 5.2. Когда я удаляю фотографию, она удаляется из db, но не из папки.Удалить фото из папки Laravel 5.2

Вот мой маршрут

Route::group(['middleware' => ['web']], function() { 
    Route::resource('flyer', 'FlyersController'); 

    Route::delete('photos/{id}', [ 
    'uses' => '\App\Http\Controllers\[email protected]', 
    'as' => 'flyer.destroy' 
]); 

}); 

Вот мой Photo.php Модель: (удалить фото на дне)

<?php 

namespace App; 

use Image; 
use Illuminate\Database\Eloquent\Model; 
use Symfony\Component\HttpFoundation\File\UploadedFile; 


class Photo extends Model { 

    /** 
    * @var string 
    * The associated table. 
    */ 
    protected $table = "flyer_photos"; 

    /** 
    * @var array 
    * Fillable fields for a photo. 
    */ 
    protected $fillable = ['name', 'path', 'thumbnail_path']; 

    /** 
    * @var 
    * The UploadedFile instance. 
    */ 
    protected $file; 

    /** 
    * @var 
    * The file name instance. 
    */ 
    protected $name; 


    /** 
    * A photo belongs to a flyer 
    */ 
    public function flyer() { 
     return $this->belongsTo('App\Flyer'); 
    } 


    /** 
    * Make a new instance from an uploaded file. 
    */ 
    public static function fromFile(UploadedFile $file) { 
     // Make a new instance. 
     $photo = new static; 

     // Assign the Uploaded file to the $file object. 
     $photo->file = $file; 

     // Set $photo to the fill properties, which are 
     // the name, path, and thumbnail path of a photo. 
     $photo->fill([ 
      'name' => $photo->setFileName(), 
      'path' => $photo->filePath(), 
      'thumbnail_path' => $photo->thumbnailPath() 
     ]); 

     // Then return the photo. 
     return $photo; 
    } 


    /** 
    * Get the photos base directory. 
    */ 
    public function baseDir() { 
     return 'FlyerPhotos/photos'; 
    } 


    /** 
    * This function gets the name and extension of a photo. 
    */ 
    public function setFileName() { 

     // Set $t = to time() 
     $t = time(); 
     // This will reduce the likelihood of a double call because it will 
     // only be a problem if the calls spanned a minute (vs a second), 
     $t -= $t % 60; 

     // hash the name of the file with the $t function. 
     $hash = sha1(
      $t . $this->file->getClientOriginalName() 
     ); 

     // Get the extension of the photo. 
     $extension = $this->file->getClientOriginalExtension(); 

     // Then set name = merge those together. 
     return $this->name = "{$hash}.{$extension}"; 
    } 


    /** 
    * Get the full file path of the photo, with the name. 
    */ 
    public function filePath() { 
     return $this->baseDir() . '/' . $this->name; 
     // Ex: 'FlyerPhotos/photos/foo.jpg' 
    } 


    /** 
    * Get the full file thumbnail path of the photo, with the name. 
    */ 
    public function thumbnailPath() { 
     return $this->baseDir() . '/tn-' . $this->name; 
     // Ex: 'FlyerPhotos/photos/tn-foo.jpg' 
    } 


    /** 
    * Upload the file to the proper directory. 
    */ 
    public function upload() { 

     // Move the file instance to the base directory with the file name. 
     $this->file->move($this->baseDir(), $this->name); 

     // Make the thumbnail. 
     $this->makeThumbnail(); 

     return $this; 
    } 


    /** 
    * Function to make the actual thumbnail. 
    * -- make and save reference the Image intervention library, not Eloquent. -- 
    */ 
    protected function makeThumbnail() { 
     Image::make($this->filePath())->fit(200)->save($this->thumbnailPath()); 
    } 



/** 
* Delete the photo path and thumbnail path in DB. 
* Access the delete function in [email protected] method 
*/ 
public function delete() { 

    \File::delete([ 
     $this->filePath(), 
     $this->thumbnailPath() 
    ]); 

    parent::delete(); 
} 


} 

Вот мой контроллер: (укороченный)

<?php 

namespace App\Http\Controllers; 

use Auth; 
use App\User; 
use App\Flyer; 
use App\Photo; 
use App\Http\Requests; 
use Illuminate\Http\Request; 
use App\Http\Requests\FlyerRequest; 
use App\Http\Controllers\Controller; 
use App\Http\Requests\AddPhotoRequest; 
use App\Http\Requests\UserEditRequest; 


class FlyersController extends Controller { 

    /** 
    * Add photos to flyer. 
    */ 
    public function addPhoto($zip, $street, AddPhotoRequest $request) { 

     // Set $photo to the fromFile() function, 
     // and get the $requested file which is set to 'photo', 
     // and upload it using the upload function(). 
     // -- 'photo' comes from the <script> tags in show.blade.php. 

     // Create a new photo instance from a file upload. 
     $photo = Photo::fromFile($request->file('photo'))->upload(); 

     // Set Flyer::loacatedAt() in (Flyer Model) 
     // = to the zip and street, and add the photo. 
     // -- Find the flyer and add the photo. 
     Flyer::locatedAt($zip, $street)->addPhoto($photo); 

    } 


    /** 
* Delete a photo. 
* -- Access delete() in Photo.php Model -- 
*/ 
public function destroyPhoto($id) { 

    Photo::findOrFail($id)->delete(); 

    return redirect()->back(); 

} 


} 

А вот мое мнение:

<div class="img-wrap"> 
<form method="post" action="{{ route('flyer.destroy', ['id' => $photo->id]) }}" enctype="multipart/form-data"> 
{!! csrf_field() !!} 
<input type="hidden" name="_method" value="DELETE"> 
@if ($user && $user->owns($flyer)) 
<button type="submit" class="close">&times;</button> 
@endif 
<a href="/project-flyer/{{ $photo->path }}" data-lity> 
<img src="/project-flyer/{{ $photo->thumbnail_path }}" alt="" data-id="{{ $photo->id }}"> 
</a> 
</form> 
</div> 

Это мой путь, чтобы хранить мои фотографии: My Path To Images Folder

(изображение и эскиз сохраняется в той же папке, фотография, которая начинается с Т.Н. - is thumbnail)

ответ

0

Как я вижу, ваш маршрут не совпадает с URL-адресом в форме:

Route::delete('photos/{id}', '[email protected]'); 

И форма:

<form method="post" action="/project-flyer/FlyerPhotos/photos/{{ $photo->id }}"> 

Предполагая, что ваша группа маршрут не встраивается в другой маршрут, вы формируете путь действия должны быть только:

/photos/{{ $photo->id }} 

Кроме того, если вы не» t иметь реальные запросы DELETE AJAX, и все удаления происходят из браузера, почему бы не сделать его настоящим POST-маршрутом, а не методом spoof? Например.

Route::post('photos/delete/{id}', '[email protected]'); 

Ваш метод уничтожения() функции редирект, так что вы не сможете использовать его для RESTful AJAX запросов в любом случае

+0

Я попробовал оба пути, и он просто дает мне URL не найден – David

+0

, может быть что-то неправильно с функцией удаления() в Photo.php? – David

+0

no, not found означает, что ваша маршрутизация неверна - вы нажимаете неверный URL-адрес (а не тот, который указан на route.php). вы увидите другую ошибку, если ваше приложение достигло контроллера. установите флажок «Сеть» в Chrome и посмотрите, куда именно идет ваш сайт, когда вы нажимаете «Удалить». затем сравните его с записью «php artisan route: list» - вы увидите, где ошибка! –

0

Попробуйте эти шаги:

Сначала удалите эту строку из routes.php

Route::delete('photos/{id}', '[email protected]'); 

Затем измените тэг формы на ваш взгляд

<form method="post" action="{{ route('flyers.destroy', ['id' => $flyer->id]) }}"> 

Дайте ему попробовать

+0

Да, я получил его на работу! Хотя я сделал это **

**, и это для маршрут ** Route :: delete ('photos/{id}', [ 'использует' => '\ App \ Http \ Controllers \ FlyersController @ destroy', 'as' => 'flyer.destroy' ]); ** и это для метода delete() ** public function delete() { \ File :: delete ([ $ this-> filePath(), $ this-> thumbnailPath() ]); parent :: delete(); } ** – David

+0

Все работает сейчас, но фотографии в файлах все еще остаются там. Это не слишком большая вещь, но было бы неплохо, если бы они тоже удалили. Есть ли способ сделать это? – David

0

Измените ваш метод формы из сообщения, чтобы удалить. В противном случае событие «Маршрут :: удалить» не будет выполнено. Обычно файлы хранятся в папке/storage/app. Laravel дает вам некоторые функции, чтобы получить файлы очень легко.

+0

Хорошо, что он отлично работает теперь даже с методом POST в форме, но, как я сказал ниже, файлы в папке не удаляются. – David

1

Я получил его на работу, мне просто пришлось кое-что изменить в моей функции удаления в Photo.php, чтобы исправить пути, которые я использовал.

/** 
    * Delete the photo path and thumbnail path in DB. 
    * Access the delete function in [email protected] method 
    */ 
    public function delete() { 

     $image = $this->path; 
     $thumbnail_image = $this->thumbnail_path; 

     File::delete([ 
      $image, 
      $thumbnail_image 
     ]); 

     parent::delete(); 
    } 
Смежные вопросы