"Path cannot be empty" laravel - laravel

I want to save a pdf file in the storage folder but when I insert a file in my form and I click on the button, it displays me the error "Path cannot be empty". However, the path is not empty.
Here is my function that allows me to do this :
public function getFilenametostore(Request $request): string
{
$filenamewithextension = $request->file('profile_pdf')->getClientOriginalName();
$filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);
//get file extension
$extension = $request->file('profile_pdf')->getClientOriginalExtension();
$time = time();
//filename to store
$filenametostore = $filename . '_' . $time . '.' . $extension;
//The problem is here
$request->file('profile_pdf')->storeAs('profile_pdfs', $filenametostore);
return $filenametostore;
}
in storage, there is: storage/app/public/profile_pdfs/
My Form :
<form action="{{url('poste')}}" method="post" enctype="multipart/form-data">
#csrf
<div class="mt-2">
<label class=" block text-sm text-gray-600" for="cus_email">Upload</label>
<input class="px-2 py-2 text-gray-700 rounded" type="file" name="profile_pdf" id="exampleInputFile">
</div>
<div class="mt-4">
<button class="px-4 py-1 text-white font-light tracking-wider bg-gray-900 rounded" type="submit">Publier</button>
</div>
</form>
My controller :
if($files = $request->hasFile('profile_pdf'))
{
$filenametostore = $this->getFilenametostore($request);
$p = new Poste;
$p->pdf = $filenametostore;
$p->save();
return redirect()->route('poste.index');
}

Go to php.ini file uncomment upload_tmp_dir and set the value c:\windows\temp
;upload_tmp_dir =
Replace like this
upload_tmp_dir = c:\windows\temp
if you use windows

I just solved this problem. If your website is hosted on windows server go to your temp folder (C:\Windows\Temp) and right click -> properties -> edit your user's permissions to full control like this.
after that if you get an alert just click ok.
Don't forget to login with your administrator user to able to edit permissions.

You can try one of these method
$fileName = $file.'.'.$ext;
$request->file->storeAs($path, $fileName);
// or
Storage::disk('local')->put($path . '/' . $fileName , $request->file);

Related

Laravels - Cannot Show Image after Upload and Save in Storage

I will show the upload image but it shown a thumbnail,
here is my Controller Code Upload a image file
$file = $request->file('images')->getClientOriginalName();
$filename = time().$file;
$path = base_path('/Uploads');
if($request->hasFile('images')){
$file = $request->file('images');
$file->move($path, $filename);
DB::table('books')->insert($filename);
return redirect('home')->with('status', 'insert succesfully');
}
else {
dd('Request Hash No File!');
}
Here is my view
#foreach ($booksDTL as $deTailsB)
<div class="col-md-4">
<img src="{{ asset('Uploads/'.$deTailsB->images) }}" width="100%" height="100%" title="{{ $deTailsB->images }}"/>
</div>
<div class="col-md-6">
<H1>{{ ucfirst(trans($deTailsB->name)) }}</H1>
<p>{{ strtoupper($deTailsB->author) }}</p>
</div>
#endforeach
Here is the controller to show data
public function detailBook($id)
{
$booksdetail = DB::table('books')->where('id', $id)->get();
return view ('details', ['booksDTL'=>$booksdetail]);
}
and here is when i run the code
enter image description here
The upload file save into folder 'Uploads' the folder is in the out of public folder, I already run the php artisan storage:link. But the upload picture still shown in thumbnail. Do you have any suggestion to this?

unisharp laravel-filemanager Thumbnail url

Hi I'm using unisharp laravel-filemanager for user to upload their products photos
when the image uploaded
this address will save in database
/photos/44/606074649c651.jpg
and I can use it by
{{asset('storage/'.$product->cover_img)}}
and Thumbnail saved in this address
/photos/44/thumbs/606074649c651.jpg
How can I get the address of thumbnail and how can I use it in blade?
This is the answer
#php($lastSlash = strrpos($product->cover_img,"/"))
src="{{asset('storage/'.substr_replace($product->cover_img, 'thumbs/', $lastSlash+1) .'' .substr($product->cover_img ,$lastSlash+1))}}"
One way to save it in the database is to add a hidden type input to the image's thumb, inside the stand-alon-button.js file.
// set or change the preview image src
items.forEach(function (item) {
target_preview.append(
$('<img>').css('height', '5rem').attr('src', item.thumb_url),
$('<input type="hidden" name="thumbnail">').attr('value', item.thumb_url)
);
});
Input:
<div class="input-group">
<span class="input-group-btn">
<a id="lfm" data-input="thumbnail" data-preview="holder" class="btn btn-primary">
<i class="fa fa-picture-o"></i> Choose
</a>
</span>
<input id="thumbnail" class="form-control" type="text" name="image">
</div>
<div id="holder" style="margin-top:15px;max-height:100px;"></div>
you can use a function like this:
function getThumbs($url=""){
$base = basename($url);
if (strpos($url, 'https://') !== false or strpos($url, 'http://') !== false) {
return str_replace($base, "thumbs/".$base, $url);
}else{
$preUrl = "storage/";
$beforeBase = str_replace($base, "",$url);
return $preUrl.$beforeBase.'thumbs/'.$base;
}
}
and in your blade use:
<img src="{{ getThumbs($product->cover_img) }}" .../>

I have uploaded a pdf file and when try to open it on my website it's showing file not found

I am trying to open a pdf file when I click on my website. The PDF is uploaded from the admin, but it's showing not found 404
view
<div class="entry-content">
<a target="_blank" href="{{$post->file}}">
<p style="font-size: 18px;color: #468b10;">
{{ $post->content }}
</p>
</a>
</div>
Controller
if (request('file')) {
$file = request('file');
$file=$file_name::get($file);
$file_name = time() . $file->getClientOriginalName();
$file->move('uploads/posts', $file_name);
$data['file'] = 'uploads/posts/'.$file_name;
}

can't return file name of dropzone after uploaded codeigniter

My controller like this
function uploaded_photo_post(){
$tempFile = $_FILES['file']['tmp_name'];
$targetFile = $_FILES['file']['name'];
$folder_name_member = self::createFolderMember($this->member_id);
$targetFile = UPLOAD_DIR . 'members/'. $folder_name_member . '/temps/'. $targetFile;
move_uploaded_file($tempFile,$targetFile); //send file to path forder
}
My form :
<form id="my-awesome-dropzone" method="post" action="/member/uploaded_photo"class="dropzone">
<div class="fallback"> <!-- this is the fallback if JS isn't working -->
<input name="file" type="file" multiple />
</div>
</form>
So how can I return path of file name to form. Some time if user don't want to choose that image so I want to send all path name to form.
Thanks guys

Cannot upload images in magento

I have a custom module by which i am trying to upload images. However i cant upload images
This is my form file
<?php
<form id="imageform" method="post" action="<?php echo $this->getFormAction(); ?>">
<input type="file" id="chain_Image" value="" name="Image[]" class="input-text">
</form>
note that this is only a small part of my code. actually this form can contain any number images. that is we can upload more than one image at a time.
This is my saveAction() :
<?php
$arr = $this->getRequest()->getPost('Image');
foreach($arr as $image)
{
if(isset($_FILES[$image]['name']) and (file_exists($_FILES[$image]['tmp_name'])))
{
try
{
$path = Mage::getBaseDir('media') . DS . 'banner' . DS;
$uploader = new Varien_File_Uploader($image);
$uploader->setAllowedExtensions(array('jpg','png','gif','jpeg'));
$uploader->setAllowRenameFiles(false);
$uploader->setFilesDispersion(false);
$destFile = $path.$_FILES[$image]['name'];
filename = $uploader->getNewFileName($destFile);
$uploader->save($path, $filename);
$postData['Image'] = $_FILES[$image]['name'];
}
catch(Exception $e)
{}
}
else
{
if(isset($postData[$image]['delete']) && $postData[$image]['delete'] == 1)
$postData[$image] = '';
else
unset($postData[$image]);
}
}
$bannerModel->addData($postData)->setId($this
->getRequest()->getParam('id'))->save();
However it does not upload my imaged. $_FILES is seem to be empty,when i have printed this. What is the problem in my code. Help me. Thanks in advance !!!
I hope there is a mistake at save function. Here is my example code for saving an image with other details. Let see this may help you.
$newsModel->setId($this->getRequest()->getParam('id'))
->setTitle($postData['title'])
->setImage($_FILES['image']['name'])
->setContent($postData['content'])
->setStatus($postData['status'])
->save();
You have to save the image by calling as setImage($_FILES['image']['name']) Try this..
Remember to put the enctype="multipart/form-data" in form parameters :
<form id="imageform" method="post" action="<?php echo $this->getFormAction(); ?>" enctype="multipart/form-data">
<input type="file" id="chain_Image" value="" name="Image[]" class="input-text">
</form>

Resources