Laravel test unable to find a file at path using Storage::fake() - laravel

I have created my own 'disk' in config/filesystems.php which looks like so;
'uploads' => [
'driver' => 'local',
'root' => storage_path('app/public') . '/uploads'
],
This seems to work fine when in my controller, it uploads the file and returns a response. my code is as follows;
public function store(Request $request)
{
if ($request->hasFile('filename')) {
foreach ($request->file('filename') as $image) {
$fileName = md5($image . microtime()) . '.' . $image->getClientOriginalExtension();
$image->storeAs('', $fileName, 'uploads');
}
}
// return goes here
}
but when I go to test my store method using the following code;
public function testUserCanSuccessfullySubmitSingleImage(): void
{
Storage::fake('uploads');
$this->postJson('/upload', [
'filename' => UploadedFile::fake()->image('image1.jpg')
]);
Storage::disk('uploads')->assertExists('image1.jpg');
Storage::disk('uploads')->assertMissing('missing.jpg');
}
I get the following error;
Unable to find a file at path [image1.jpg].
Failed asserting that false is true.
I have followed a few tutorials, but they all say the same thing and im really lost.
Any help would be greatly appreciated.
Cheers

The problem is that you are renaming your file in the controller with md5($image . microtime()) so you cannot assert that image1.jpg exists since you changed the name.
What you could do is let laravel name the file and then check that in your test:
In your Controller:
Replace storeAs with store, store will generate a unique ID to serve as the file name.
public function store(Request $request)
{
if ($request->hasFile('filename')) {
foreach ($request->file('filename') as $image) {
$image->store('', 'uploads');
}
}
}
In your test:
To assert if the image exists we will use the same method to generate the unique ID as laravel does when saving the image. Replace 'image1.jpg' with $image->hashName() in your assertion.
public function testUserCanSuccessfullySubmitSingleImage(): void
{
Storage::fake('uploads');
$this->postJson('/upload', [
'filename' => $image = UploadedFile::fake()->image('image1.jpg')
]);
Storage::disk('uploads')->assertExists($image->hashName());
Storage::disk('uploads')->assertMissing('missing.jpg');
}
From the docs:
In web applications, one of the most common use-cases for storing
files is storing user uploaded files such as profile pictures, photos,
and documents. Laravel makes it very easy to store uploaded files
using the store method on an uploaded file instance. Call the store
method with the path at which you wish to store the uploaded file:
public function update(Request $request)
{
$path = $request->file('avatar')->store('avatars');
return $path;
}
There are a few important things to note about this example. Note that
we only specified a directory name, not a file name. By default, the
store method will generate a unique ID to serve as the file name. The
file's extension will be determined by examining the file's MIME type.
The path to the file will be returned by the store method so you can
store the path, including the generated file name, in your database.

Related

laravel livewire uploaded file create object from path and save to s3

I'm uploading a file to a file upload component, and then passing the temporary file path to the parent component with an event. In the parent component I need to save the file to s3.
I need to pass the path or a file object or something back to the parent component, and then save it, but I can't seem to get it to work.
I've tried sending over a File object, as well as an UploadedFile object, my latest iteration is to try with a File object, and I'm getting the following error:
Unresolvable dependency resolving [Parameter #0 [ <required> string $path ]] in class Symfony\Component\HttpFoundation\File\File
So in my child component I have this code:
public function updatedFile()
{
$fileObj = new File($this->file->path());
$this->emitUp('fileUploaded', $fileObj);
}
In my parent component I'm listening for the fileUploaded event, which calls the save method:
public function save(File $uploadedFile)
{
if ($path = Storage::putFileAs(env('APP_ENV') . '/statements', $uploadedFile->name, 's3')) {
$this->statement = new Statement([
'location_id' => $this->location->id,
'file_name' => $uploadedFile->name,
'path' => $path,
'uploaded_by' => Auth::user()->id,
]);
$this->statement->save();
}
}
I've also tried using $uploadedFile->storeAs() and I get the same result. It seems like the $uploadedFile object is not the right type. I don't know if I need a Storage object or what and I can't seem to find a good answer in the docs.
The path I have available after uploading the file in my livewire component is the temporary file name that livewire saves the file as in local storage. I also need the original file name as well, like what was uploaded as I'm saving that to the database.
If I remove the type hint on the save() method I get Attempt to read property "name" on array. Why is $uploadedFile an array and not an object? I guess if I remove the type hint it just gets sent over as an array. I dunno..
Here's the solution I came up with:
child component:
public function updatedFile()
{
$this->validate([
'file' => 'required|max:12288'
]);
$this->emitUp('fileUploaded', [$this->file->path(), $this->file->getClientOriginalName()]);
}
parent component:
public function save($uploadedFile)
{
if ($path = Storage::disk('s3')->put(env('APP_ENV') . '/statements/' . $uploadedFile[1], file_get_contents($uploadedFile[0]))) {
$this->statement = new Statement([
'location_id' => $this->location->id,
'file_name' => $uploadedFile[1],
'path' => $path,
'uploaded_by' => Auth::user()->id,
]);
$this->statement->save();
}
}

Laravel upload image in the Public Folder not in Storage Folder

I want the uploaded file to be located in the public/uploads folder directly like public/uploads/my_file.jpeg. Why is it that my code uploads it to public/uploads/file_name/file.jpeg?
here is the filesystems.php.
'public_uploads' => [
'driver' => 'local',
'root' => public_path() . '/uploads',
],
and here is the controller.
function upload_template(Request $request)
{
$filee = $request->file('file_upload');
$file_ext = $filee->extension();
$file_name = $model->id . "." . $file_ext;
Storage::disk('public_uploads')->put($file_name, $filee);
}
This happened because you specify the directory to store as filename. The file_name, should be the directory name such as images.
Refer to this line :
Storage::disk('public_uploads')->put($file_name, $filee);
So you could change this to :
Storage::disk('public_uploads')->put('images', $filee);
// output : /images/234234234.jpg
You need to provide the file contents in the second argument not file object, try this :
Storage::disk('public_uploads')->put($file_name, file_get_contents($filee));
To specific the file name you can use move() method instead of storage() :
if($request->hasFile('file_upload'))
{
$filee = $request->file_upload;
$name = "my_file"; // name here
$fileName = $name . $filee->getClientOriginalName();
$filee->move('public_uploads',$fileName);
}
//this is the best way you create a trait with 2 functions saveImage and
//deleteImage
public function saveImage($name,$folder){
$extention=$name->getClientOriginalExtension();
$filename=time().'.'.$extention;
$path=public_path().'/'.$folder;
$name->move($path,$filename);
return $filename;
}
public function deleteImage($name,$folder){
$image_path=public_path().'/'.$folder.'/'.$name;
unlink($image_path);
}
function upload_template(Request $request){
$file = $request->file_upload;//$request->your input name
$img=$this->saveImage($file,'uploads');
//you can use $img for storing the image in database for example
User::create([
'avatar'=>$img
])
}
//don't forget to invoke your trait
I just found it Laravel 5.3 Storage::put creates a directory with the file name.
Need to provide the file contents in the second argument not file object.
Tt should be Storage::disk('public_uploads')->put($file_name, file_get_contents($filee));.

Upload array of files Laravel

I want to upload an array of files,in Laravel , and I am not sure what is the path and who to store the file. Eight now the data are stored ,but in my case the path is #. In the image below I have the data that I am sending from front (Vuejs and I am using vue-upload-component)
$fileName = [];
foreach($request->input('files') as $files){
$contractFile = new ContractFile();
$contractFile->fill([
'contract_id' => $contract->id,
'name' => $files['name'],
'path' => '#',
])->save();
}
ContractFile
class ContractFile extends Model
{
protected $fillable = ['path','contract_id','name'];
public function contract()
{
return $this->belongsTo(Contract::class);
}
}
ContractFile db
Schema::create('contract_files', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('contract_id');
$table->string('path');
$table->string('name');
$table->timestamps();
});
filesystems.php
'uploads' =>[
'driver' => 'local',
'root' => storage_path().'file/uploads',
],
You may use
foreach($request->file('files') as $uploadedFile){
$filename = time() . '_' . $uploadedFile->getClientOriginalName();
$path = $uploadedFile->store($filename, 'uploads');
$contractFile = new ContractFile();
$contractFile->contract_id = $contract->id;
$contractFile->name = $uploadedFile->getClientOriginalName();
$contractFile->path = $path;
$contractFile->save();
}
By default, the public disk uses the local driver and stores these
files in storage/app/public. To make them accessible from the web, you
should create a symbolic link from public/storage to
storage/app/public.
To create the symbolic link, you should use the storage:link Artisan command:
php artisan storage:link
In form add file input something like:
<input type="file" multiple name="attachments[]">
Then in controller action handle an array of uploaded files like:
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
// Check if there are files uploadeded for the form name attribute.
if ($request->files->has('attachments')) {
// Here we get the uploaded fileBag by attribute.
$fileBag = $request->files->get('attachments');
// Directory path where to upload the files.
$dirPath = 'attachments'
// Loop over each file in the bag to store.
foreach ($fileBag as $uploadedFile) {
// Create unique file name. You can write your logic but recommend
// using uuid to avoid name collision. Make sure to add same
// extension as uploaded file.
$fileName = (string) Str::uuid() . '.' . $uploadedFile->getClientOriginalExtension();
// Store using Storage Facades.
// Note: storing using request->file->store() will clear files array
// after first store call and since we are looping here that won't
// work.
$path = Storage::putFileAs($dirPath, $uploadedFile, $fileName);
// Now you can use the path and store it in the DB table or any way
// you want.
}
}
For more configuration refer: Laravel 7.x File Storage, Symfony File Bag, Symfony Uploaded File

Validating Image Uploads

Yo! I am working on a form where I attach some image.
Form:
{{ Form::file('attachments[]', array('multiple')) }}
Validation:
$this->validate($response, array(
'attachments' => 'required | mimes:jpeg,jpg,png',
));
I have also tried 'image' as validator rule but whenever I post the form with jpg image I get back errors:
The attachments must be a file of type: jpeg, jpg, png.
Working with Laravel 5.3
Since you defined an input name of attachments[], attachments will be an array containing your file. If you only need to upload one file, you might want to rename your input name to be attachments, without the [] (or attachment would make more sense in that case). If you need to be able to upload multiple, you can build an iterator inside your Request-extending class that returns a set of rules covering each entry inside attachments[]
protected function attachments()
{
$rules = [];
$postedValues = $this->request->get('attachments');
if(null == $postedValues) {
return $rules;
}
// Let's create some rules!
foreach($postedValues as $index => $value) {
$rules["attachments.$index"] = 'required|mimes:jpeg,jpg,png';
}
/* Let's imagine we've uploaded 2 images. $rules would look like this:
[
'attachments.0' => 'required|mimes:jpeg,jpg,png',
'attachments.1' => 'required|mimes:jpeg,jpg,png'
];
*/
return $rules;
}
Then, you can just call that function inside rules() to merge the array returned from attachments with any other rules you might want to specify for that request:
public function rules()
{
return array_merge($this->attachments(), [
// Create any additional rules for your request here...
]);
}
If you do not yet have a dedicated Request-extending class for your form, you can create one with the artisan cli by entering: php artisan make:request MyRequestName. A new request class will be created inside app\Http\Requests. That is the file where you would put the code above in. Next, you may just typehint this class inside the function signature of your controller endpoint:
public function myControllerEndpoint(MyRequestName $request)
{
// Do your logic... (if your code gets here, all rules inside MyRequestName are met, yay!)
}

How to delete file from public folder in laravel 5.1

I want to delete a News from database and when I hit the delete button all data from database deleted but the image is remains in upload folder.
So, how do I this to work.
thanks
This is my function again but does not delete the image from images/news folder of public directory>
public function destroy($id) {
$news = News::findOrFail($id);
$image_path = app_path("images/news/{$news->photo}");
if (File::exists($image_path)) {
//File::delete($image_path);
unlink($image_path);
}
$news->delete();
return redirect('admin/dashboard')->with('message','خبر موفقانه حذف شد');
}
You could use PHP's unlink() method just as #Khan suggested.
But if you want to do it the Laravel way, use the File::delete() method instead.
// Delete a single file
File::delete($filename);
// Delete multiple files
File::delete($file1, $file2, $file3);
// Delete an array of files
$files = array($file1, $file2);
File::delete($files);
And don't forget to add at the top:
use Illuminate\Support\Facades\File;
Use the unlink function of php, just pass exact path to your file to unlink function :
unlink($file_path);
Do not forget to create complete path of your file if it is not stored in the DB. e.g
$file_path = app_path().'/images/news/'.$news->photo;
this method worked for me
First, put the line below at the beginning of your controller:
use File;
below namespace in your php file
Second:
$destinationPath = 'your_path';
File::delete($destinationPath.'/your_file');
$destinationPath --> the folder inside folder public.
This worked at laravel 8
use File;
if (File::exists(public_path('uploads/csv/img.png'))) {
File::delete(public_path('uploads/csv/img.png'));
}
First, you should go to config/filesystems.php and set 'root' => public_path() like so:
'disks' => [
'local' => [
'driver' => 'local',
'root' => public_path(),
],
Then, you can use Storage::delete($filename);
Update working for Laravel 8.x:
Deleting an image for example ->
First of all add the File Facade at the top of the controller:
use Illuminate\Support\Facades\File;
Then use delete function. If the file is in 'public/' you have to specify the path using public_path() function:
File::delete(public_path("images/filename.png"));
Using PHP unlink() function, will have the file deleted
$path = public_path()."/uploads/".$from_db->image_name;
unlink($path);
The above will delete an image returned by $from_db->image_name located at public/uploads folder
Try to use:
unlink('.'.Storage::url($news->photo));
Look the dot and concatenation before the call of facade Storage.
public function destroy($id) {
$news = News::findOrFail($id);
$image_path = app_path("images/news/".$news->photo);
if(file_exists($image_path)){
//File::delete($image_path);
File::delete( $image_path);
}
$news->delete();
return redirect('admin/dashboard')->with('message','خبر موفقانه حذف شد');
}
First make sure the file exist by building the path
if($request->hasFile('image')){
$path = storage_path().'/app/public/YOUR_FOLDER/'.$db->image;
if(File::exists($path)){
unlink($path);
}
Try it :Laravel 5.5
public function destroy($id){
$data = User::FindOrFail($id);
if(file_exists('backend_assets/uploads/userPhoto/'.$data->photo) AND !empty($data->photo)){
unlink('backend_assets/uploads/userPhoto/'.$data->photo);
}
try{
$data->delete();
$bug = 0;
}
catch(\Exception $e){
$bug = $e->errorInfo[1];
}
if($bug==0){
echo "success";
}else{
echo 'error';
}
}
the easiest way for you to delete the image of the news is using the model event like below
and the model delete the image if the news deleted
at first you should import this in top of the model class use Illuminate\Support\Facades\Storage
after that in the model class News you should do this
public static function boot(){
parent::boot();
static::deleting(function ($news) {
Storage::disk('public')->delete("{$news->image}");
})
}
or you can delete the image in your controller with
this command
Storage::disk('public')->delete("images/news/{$news->file_name}");
but you should know that the default disk is public but if you create folder in the public folder and put the image on that you should set the folder name before $news->file_name
This is the way I upload the file and save it into database and public folder and also the method I delete file from database and public folder.
this may help you and student to get complete source code to get the task done.
uploading file
at the first if you save file into database by giving path public_path() once it not need to used in delete method again
public function store_file(Request $request)
{
if($request->hasFile('file'))
{
$fileExtention = $request->file('file')->getClientOriginalExtension();
$name = time().rand(999,9999).$request->filename.'.'.$fileExtention;
$filePath = $request->file('file')->move(public_path().'/videos',$name);
$video = new Video_Model;
$video->file_path = $filePath;
$video->filename = $request->filename;
$video->save();
}
return redirect()->back();
}
deleting file
from database and public folder as you saved
public function delete_file(Request $request)
{
$file = Video_Model::find($request->id);
$file_path = $file->file_path;
if(file_exists($file_path))
{
unlink($file_path);
Video_Model::destroy($request->id);
}
return redirect()->back();
}
Its a very old thread, but I don't see that the solution is here or the this thread is marked as solved. I have also stuck into the same problem I solved it like this
$path = public_path('../storage/YOUR_FOLDER_NAME/YOUR_FILE_NAME');
if (!File::exists($path))
{
File::delete(public_path('storage/YOUR_FOLDER_NAME/YOUR_FILE_NAME'));
}
The key is that you need to remove '..' from the delete method. Keep in mind that this goes true if you are using Storage as well, whether you are using Storage of File don't for get to use them like
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use File; // For File
use Storage; // For Storage
Hope that it will help someone.
For Delete files from the public folders, we can use the File::delete function into the Laravel. For use File need to use File into the controller OR We can use \File. This consider the root of the file.
// Delete a single file
File::delete($filename);
For delete Multiple files
// Delete multiple files
File::delete($file1, $file2, $file3);
Delete an array of Files
// Delete an array of files
$files = array($file1, $file2);
File::delete($files);
Two ways to delete the image from public folder without changing laravel filesystems config file or messing with pure php unlink function:
Using the default local storage you need to specify public subfolder:
Storage::delete('public/'.$image_path);
Use public storage directly:
Storage::disk('public')->delete($image_path);
I would suggest second way as the best one.
Hope this help other people.
If you store your image in folder public, try this steps:
For example your image is sample.jpg and your path is public/img/sample.jpg
so this codes will delete your image
use Illuminate\Support\Facades\File;
.
.
.
public function deleteImage(){
$imgWillDelete = public_path() . '/img/sample.jpg';
File::delete($imgWillDelete);
}
Follow the steps carefully to get the image first=>
$img = DB::table('students')->where('id',$id)->first();
$image_path = $img->photo;
unlink($image_path);
DB::table('students')->where('id',$id)->delete();
For delete file from folder you can use unlink and if you want to delete data from database you can use delete() in laravel
Delete file from folder
unlink($image_path);
For delete record from database
$flight = Flight::find(1);
$flight->delete();
public function update(Request $request, $id)
{
$article = Article::find($id);
if($request->hasFile('image')) {
$oldImage = public_path($article->image);
File::delete($oldImage);
$fileName = time().'.'.$request->image->extension();
$request->image->move(public_path('uploads/'), $fileName);
$image ='uploads/'.$fileName;
$article->update([
'image' => $image,
]);
}
$article->update([
'title' => $request->title,
'description' => $request->description,
]);
return redirect()->route('admin.article.index');
}
you can delete file and its record like wise:
public function destroy($id)
{
$items = Reports::find($id); //Reports is my model
$file_path = public_path('pdf_uploads').'/'.$items->file; // if your file is in some folder in public directory.
// $file_path = public_path().'/'.$items->file; use incase you didn't store your files in any folder inside public folder.
if(File::exists($file_path)){
File::delete($file_path); //for deleting only file try this
$items->delete(); //for deleting record and file try both
}
return redirect()->back()->with('message','client code: '.$items->receipt_no.' report details has been successfully deleted along with files');
}
note: items->file is my attribute in database where I have stored the name of the file like so,
I have stored the file in the directory "public/pdf_uploads/filename"
Don't forget to add this in your header
use Illuminate\Support\Facades\File;
For File Syntax:
if(File::exists(public_path('upload/test.png'))){
File::delete(public_path('upload/test.png'));
}else{
dd('File does not exists.');
}
For Storage Syntax:
if(Storage::exists('upload/test.png')){
Storage::delete('upload/test.png');
/*
Delete Multiple File like this way
Storage::delete(['upload/test.png', 'upload/test2.png']);
*/
}else{
dd('File does not exists.');
}
Common Method is :
#unlink('/public/admin/pro-services/'.$task->pro_service_image);

Resources