使用laravel 5.0
建立简单的插入和删除功能然而它给了我一个错误:
我的代码是:
public function postCreate(Requests\ProductRequest $request)
{
$product = new Product;
$product->category_id = Input::get('category_id');
$product->title = Input::get('title');
$product->description = Input::get('description');
$product->price = Input::get('price');
$product->status = Input::get('status');
$image = Input::file('image');
$filename = date('m-d-Y-H_i_s')."-". $image->getClientOriginalName();
$path = public_path('img/products/' . $filename);
Image::make($image->getRealPath())->resize(468, 249)->save($path);
$product->image = 'img/products/'.$filename;
$product->save();
return redirect('admin/products/')->with('flash_message','Product created');
}
public function postDestroy($id)
{
$product = Product::find($id);
if ($product) {
Storage::delete('public/'.$product->image);
$product->delete();
return redirect('admin/products')->with('flash_message', 'Product deleted');
}
return redirect('admin/products')->with('flash_message','Something went wrong , please try again');
}
我可以知道会发生什么吗?我尝试了几个替代方案:
文件::删除(没有错误,但图片未删除) \ File :: delete(没有错误,但图片未删除)
命名空间:
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Product;
use App\Category;
use Request;
use Illuminate\Support\Facades\Input;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Facade;
use Storage;
use League\Flysystem\Filesystem;
use File;
答案 0 :(得分:3)
5个月后,我使用Laravel 5.1偶然发现同样的异常。使用Storage::delete()
,我尝试将其从存储文件夹的根目录传递到我的删除目标文件的相对路径,并且在找不到文件时失败。在堆栈跟踪中,我看到使用相对于物理机器路径的无效路径调用Filesystem-> delete。所以我用storage_path().'/app/7/observe/1455749755-yet.JPG'
当堆栈跟踪变得有趣时。当我使用完整的真实文件系统路径调用它时,它仍然抛出了一个filenotfoundexception,但显示为传递给Filesystem->assertPresent()
的路径已经剥离了前导斜杠。像这样:
Filesystem->assertPresent('home/vagrant/Proj/donk/storage/app/7/observe/1455749755-yet.JPG')
Filesystem->delete('/home/vagrant/Proj/donk/storage/app/7/observe/1455749755-yet.JPG')
FilesystemAdapter->delete('/home/vagrant/Proj/donk/storage/app/7/observe/1455749755-yet.JPG')
call_user_func_array(array(object(FilesystemAdapter), 'delete'), array('/home/vagrant/Proj/donk/storage/app/7/observe/1455749755-yet.JPG'))
FilesystemManager->__call('delete', array('/home/vagrant/Proj/donk/storage/app/7/observe/1455749755-yet.JPG'))
FilesystemManager->delete('/home/vagrant/Proj/donk/storage/app/7/observe/1455749755-yet.JPG')
Facade::__callStatic('delete', array('/home/vagrant/Proj/donk/storage/app/7/observe/1455749755-yet.JPG'))
Storage::delete('/home/vagrant/Proj/donk/storage/app/7/observe/1455749755-yet.JPG')
堆栈跟踪第3行的路径Filesystem->delete()
已经死亡。但传递给实际检查文件存在[assertPresent()]
的内容是相对的,在这种情况下不是有效路径。
我的解决方法是简单地使用File::delete()
而不是Storage :: delete()。也许后者可以在它位于存储文件夹根目录的文件中工作,但是我不打算去测试,因为那不是我想做的事情。如果这是存储类的限制,那么它不值得IMO。
在github上找到了这个有趣的相关主题。显然它是设计的吗?似乎,呃,有缺陷。