Мне нужна помощь в удалении фотографии из моей папки в 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">×</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>
Это мой путь, чтобы хранить мои фотографии:
(изображение и эскиз сохраняется в той же папке, фотография, которая начинается с Т.Н. - is thumbnail)
Я попробовал оба пути, и он просто дает мне URL не найден – David
, может быть что-то неправильно с функцией удаления() в Photo.php? – David
no, not found означает, что ваша маршрутизация неверна - вы нажимаете неверный URL-адрес (а не тот, который указан на route.php). вы увидите другую ошибку, если ваше приложение достигло контроллера. установите флажок «Сеть» в Chrome и посмотрите, куда именно идет ваш сайт, когда вы нажимаете «Удалить». затем сравните его с записью «php artisan route: list» - вы увидите, где ошибка! –