i'm bulding simple insert and delete function using laravel 5.0
However it give me an error :
FileNotFoundException in Filesystem.php line 381:
File not found at path: public/img/products/xxxxxx.xxx
My Code is :
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');
}
May i know what happen? i try few alternatives such :
File::delete (no error but image not deleted)
\File::delete (no error but image not deleted)
Namespace :
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;
5 months later I stumble on to the same exception using Laravel 5.1. Using Storage::delete(), I tried passing it the relative path to my delete target file from the root of the storage folder, and that failed with file not found. In the stack trace I saw that Filesystem->delete was being called with an invalid path relative to the physical machine's path. So I called it with storage_path().'/app/7/observe/1455749755-yet.JPG'
That's when the stack trace got interesting. When I called it with the full true file system path, it still threw a filenotfoundexception, but the path shown as passed to Filesystem->assertPresent() had the leading slash peeled off. Like so:
in Filesystem.php line 381
at Filesystem->assertPresent('home/vagrant/Proj/donk/storage/app/7/observe/1455749755-yet.JPG') in Filesystem.php line 232
at Filesystem->delete('/home/vagrant/Proj/donk/storage/app/7/observe/1455749755-yet.JPG') in FilesystemAdapter.php line 155
at FilesystemAdapter->delete('/home/vagrant/Proj/donk/storage/app/7/observe/1455749755-yet.JPG')
at call_user_func_array(array(object(FilesystemAdapter), 'delete'), array('/home/vagrant/Proj/donk/storage/app/7/observe/1455749755-yet.JPG')) in FilesystemManager.php line 293
at FilesystemManager->__call('delete', array('/home/vagrant/Proj/donk/storage/app/7/observe/1455749755-yet.JPG')) in Facade.php line 216
at FilesystemManager->delete('/home/vagrant/Proj/donk/storage/app/7/observe/1455749755-yet.JPG') in Facade.php line 216
at Facade::__callStatic('delete', array('/home/vagrant/Proj/donk/storage/app/7/observe/1455749755-yet.JPG')) in ObservationsController.php line 224
at Storage::delete('/home/vagrant/Proj/donk/storage/app/7/observe/1455749755-yet.JPG') in ObservationsController.php line 224
The path at line 3 of the stack trace, Filesystem->delete() is dead on. But what is passed to actually check file existence [assertPresent()] is relative, and in this case not a valid path.
My workaround was to simply use File::delete() rather than Storage::delete(). Perhaps the latter can work when it's a file that's sitting at the root of the storage folder, but I'm not going to bother testing that because that's not what I want to do. If that's the limitation of the Storage class, it's not worth much IMO.
Found this interesting related thread at github. Apparently it's by design? Seems, uh, flawed.
https://github.com/thephpleague/flysystem/issues/477
Related
does anyone know how to save generated pdf file and name it dynamicly using dompdf in laravel?
i always encounter this error
failed to open stream: No such file or directory
while trying to generated pdf file and save it to project directory.
here its my controller code
$bills = Tagihan::join('pesanan', 'tagihan.id_pesanan', 'pesanan.id_pesanan')
->join('kendaraan', 'pesanan.id_kendaraan', 'kendaraan.id_kendaraan')
->join('kategori_mobil', 'kendaraan.id_kategori_kendaraan', 'kategori_mobil.id_kategori')
->join('users', 'pesanan.id_pengguna', 'users.id')
->where('pesanan.id_pesanan', $save->id_pesanan)
->select('pesanan.*', 'users.*', 'tagihan.*', 'kendaraan.*', 'kategori_mobil.nama_kategori')
->first();
$today = Carbon::now('GMT+7')->toDateString();
$pdf = new PDF();
$pdf = PDF::loadView('admin.tagihan.pdf', compact('bills','today'));
file_put_contents('public/bills/'.$noOrder.'.pdf', $pdf->output() );
return redirect()->route('pesananIndex');
i want to save my pdf file with custom string with $noOrder.'pdf', but when i use static name like this
file_put_contents('public/bills/bubla.pdf', $pdf->output());
i had no error, any solution?
You can getOriginalContent from http response into a variable then push it to your file.
$pdf = PDF::loadView('admin.tagihan.pdf', compact('bills','today'));
$content = $pdf->download()->getOriginalContent();
Storage::put('public/bills/bubla.pdf',$content);
...
Storage::disk('local')->makeDirectory('/pdf/order');
$pdf = PDF::loadView('PDF.Order.OrderPDF-dev', ['invoiceDetail'=>$invoiceDetail]);
$path = 'storage/pdf/order/';
$pdf->save($path . $fileName);
return url($path.$fileName);
In my example first i created folder for PDF. makeDirectory(...) will create folder if exist or not.
$path is location where i want to put my file in Dir. do not use absolute path or public path function here. only define location where you want to create file.
I am creating file in Storage folder. so i added storage/.....
I am having problems with unlink() I have used before and worked fine but in this particular one it isn't. will explain.
this is the code
public function kill($id){
$post = Post::withTrashed()->where('id', $id)->first();
unlink(public_path(). "/" . $post->featured);
$post->forceDelete();
Session::flash('success', 'Post has been permanently deleted');
return redirect()->back();
}
and i get this error
ErrorException in PostsController.php line 158:
unlink(/Applications/XAMPP/xamppfiles/htdocs/angie/public/http://angie.dev/uploads/posts/1506133455image_1.jpg): No such file or directory
so basically is adding "http://angie.dev/" before file name. however looking in the database file name is normal. how do I get rid of it?
thanks
Use this
unlink(public_path(). str_replace("angie.dev/","/", $post->featured));
I've written the following:
public function show($id)
{
$image = Image::find($id);
$path = $image-file_path;
return response()->file();
}
In routes/web.php I have the following:
Route::resource('image', 'ImageController');
Now I don't know why when I go to http://localhost:8000/image/5 in my browser, I get:
FatalErrorException in ImageController.php line 48:
Class 'App\Http\Controllers\Image' not found
in ImageController.php line 48
What can I do to fix this?
You have a typo here $path = $image-file_path;
This should be $image->file_path;
You need to provide a properly-namespaced path to "Image" on the line:
$image = Image::find($id);
And then fix in accordance with Kostas' suggestion so it's not breaking on the very next line.
Issue 1 : Class 'App\Http\Controllers\Image' not found
You are missing to include your model "Image" in your controller
Include you model in top of the contoller:
For eg :
use App\Image;
Issue 1 : $path = $image-file_path;
Change form $path = $image-file_path;
to
$path = $image->file_path;
You need include Image model in your controller
use App\Models\Image;
or if you don't have folder Models
use App\Image;
I am having the same error as this guy is :
Another thread
basically the error i have is uploading the image to the specific path, my code is as follows :
public function postCreate() {
$validator = Validator::make(Input::all() , Product::$rules);
if ($validator->passes()) {
$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');
$image = Input::file('image');
$filename = date('Y-m-d-H:i:s')."-".$image->getClientOriginalName();
Image::make($image->getRealPath())->resize(468, 249)->save('public/img/products'.$filename);
$product->image = 'public/img/products'.$filename;
$product->save();
return Redirect::to('admin/products/index')
->with('message' , 'Product created');
}
return Redirect::to('admin/products/index')->with('message' , 'something went wrong')
->withErrors($validator)->withInput();
}
I was just trying to follow a tutorial on laravel e-commerce web application.
I guess the problem is that i don't have write permisson in my directory , how do i add write permission in my directory. I.E. the public folder, I googled a few places , but i don't understand what is it that i have to edit ?
I.E the htaccesss file or can i make write changes on the cmd ? also how do i check what weather a directory is write protected .
PS. i am using windows . i am attaching a screenshot of the error .
Thank you.
You might want to change the dateformat since windows doesn't allow colons in filenames:
$filename = date('Y-m-d-H:i:s')."-".$image->getClientOriginalName();
And you also might want to add a trailing slash to your path so it doesn't concatenate the filename to the folder path:
Image::make($image->getRealPath())->resize(468, 249)->save('public/img/products'.$filename);
Generally this error occurs when you do not yet have the directory that will store the image inside the public directory. Sometimes it can be a permission issue.
Does you img directory exists in your public directory?
To fix this, follow the steps:
Use this snippet:
$relPath = 'img/'; //your path inside public directory
if (!file_exists(public_path($relPath))) { //Verify if the directory exists
mkdir(public_path($relPath), 666, true); //create it if do not exists
}
Or manually create the img directory in public
2.Then you can save your image:
Image::make($image)->resize(468, 249)->save(public_path('img/products'.$filename)); //save you image
$product->image = 'img/products'.$filename; //note
$product->save();
**NOTE: We do not need to specify the public directory in the path because we are using a relative path. The img directory will be created inside public directory.
Along with this, you need to make sure the folder path exists and which has right permissions set.
$relPath = 'img/product/';
if (!file_exists(public_path($relPath))) {
mkdir(public_path($relPath), 777, true);
}
Where $relPath is the path relative to public directory.
This requirement is however windows specific. In linux, folder directory will be created if it does not exist.
I also recommend all of you to check if $path exists. Like Jose Seie use native PHP check, I recommend you to thought about build-in helpers.
This can be achieved with File Facade helper:
File::exists($imagePath) or File::makeDirectory($imagePath, 777, true);
Advice you to use Laravel built-in functions, classes & helpers to improve the performance of your application!
well , i made the correction that john suggested and then made the following corrections :
I replaced the below code :
Image::make($image->getRealPath())->resize(468, 249)->save('public/img/products'.$filename);
with :
$path = public_path('img/products/'.$filename);
Image::make($image->getRealPath())->resize(468, 249)->save($path);
problem solved , i don't know why public_path works , but never mind .
if I print the content of an instance of UploadedFile, this is what I get
array (
'opt_image_header' =>
Symfony\Component\HttpFoundation\File\UploadedFile::__set_state(array(
'test' => false,
'originalName' => 'triangle-in-the-mountains.jpg',
'mimeType' => 'image/jpeg',
'size' => 463833,
'error' => 0,
)
And this is how I get the uploaded file in the Controller. Before of moving it, I should resize it.
foreach($request->files as $uploadedFile){
$ext = '.' . $uploadedFile['opt_image_header']->guessExtension();
$filename = sha1(uniqid(mt_rand(), true)) . $ext;
$uploadedFile['opt_image_header']->move($path . '/images/', $filename);
}
so there's no the "tmp_name" that I'd need for resizing the image before of saving it.
Do I need to read it directly from the $_FILE array?
Use $uploadedFile->getRealPath()
Symfony\Component\HttpFoundation\File\UploadedFile extends Symfony\Component\HttpFoundation\File\File, which extends PHP's SplFileInfo, so UploadedFile inherits all methods from SplFileInfo.
Use $uploadedFile->getRealPath() for the absolute path for the file. You can also use other methods on it, such as getFilename() or getPathname(). For a complete list of available methods (of SplFileInfo), see the docs.
Symfony's File class adds some extra methods, such as move() and getMimeType(), and adds backward compatibility for getExtension() (which was not available before PHP 5.3.6). UploadedFile adds some extra methods on top of that, such as getClientOriginalName() and getClientSize(), which provide the same information as you would normally get from $_FILES['name'] and $_FILES['size'].
If you are uploading a file with Doctrine, take a look at Symfony Documentation Upload a file
If you want to upload a file without Doctrine, you can try something like:
foreach($request->files as $uploadedFile) {
$filename = $uploadedFile->get('Put_Input_Name_Here')->getClientOriginalName();
$file = $uploadedFile->move($distination_path, $filename);
}
If there was any issue for uploading file move() will throw an exception
UPDATED
According to get the temp path of the uploaded file to resize the image you can use getPath() function in the mentioned loop
$tmp_name = $uploadedFile->get('Put_Input_Name_Here')->getPath();
If you ask why, because the Symfony File class is extends SplFileInfo