Laravel Class 'Iyzipay\Options' not found - laravel

<?php
namespace App\Http\Controllers\Web;
use Iyzipay\Options;
class IndexController extends Controller
{
.
.
.
public function iyzico(Request $request){
$name = $request->get('name');
$card_no = $request->get('card_no');
$expire_month = $request->get('expire_month');
$expire_year = $request->get('expire_year');
$cvc = $request->get('cvc');
$user = Auth::user();
//options
$options = new Options();
$options->setApiKey("***");
$options->setSecretKey("***");
$options->setBaseUrl("***");
}
.
.
.
}
I'm doing something like this on controller.But I get the error Class 'Iyzipay\Options' not found. I checked the file path and it is correct. No matter what I did I couldn't fix the error

If you want to use iyzipay package, you should add to your composer:
composer require iyzico/iyzipay-php
Edit For Manual Usage:
If you want to use manual you can create Library folder under app directory. And paste iyzipay folder here.
Then create a file such as Iyzipay.php in Library folder.
app
-Library
--iyzipay-php
--Iyzipay.php
And Iyzipay.php (i recommend, don't use transaction process in your controller
<?php
namespace App\Library;
require_once dirname ( __FILE__ ) . '/iyzipay-php/IyzipayBootstrap.php';
class Iyzipay
{
public static function boot ()
{
\IyzipayBootstrap::init ();
}
public static function pay ( $apiInfo, $cartInfo, $price, $shippingTaxPrice = 0 )
{
....
....
}
}
and use in your controller like this:
<?php
...
...
use App\Library\Iyzipay;
...
...
Iyzipay::boot ();
$payment = Iyzipay::pay ( $apiResources, $cartInfo, $price) );

Related

Old File from storage is not being deleted on update Laravel

I am trying to delete the existing image from the storage while the new image is being updated.
But everytime the new image is inserted retaining the previous ones.
When I dd the image from database to unlink, I get full url
i.e.
http://127.0.0.1:8000/teacger-image/1598097262-85508.jpg
While only teacher-image/1598097262-85508.jpg
has been stored in the database.
Function to delete the image
public function deleteImageFromStorage($value)
{
if (!empty($value)) {
File::delete('public/' . $value);
}
}
I have called the method in the controller when there is image posted during update.
update method includes
if ($request->hasFile('image')) {
$teacher->deleteImageFromStorage($teacher->image);
$file = $request->file('image');
$filename = time() . '-' . mt_rand(0, 100000) . '.' . $file->getClientOriginalExtension();
$path = $file->storeAs('teacher-image', $filename);
$teacher->image = $path;
}
Also I have used Storage::delete() and unlink as well but none of them seem to work.
help
This is how I've been deleting files in Laravel.
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class ImagesController extends Controller
{
public function deleteImageFromStorage($value)
{
if ( file_exists( storage_path($value) ) ) {
Storage::delete($value);
}
}
}
Make sure to use Illuminate\Support\Facades\Storage.
Try change your deleteImageFromStorage method to this:
public function deleteImageFromStorage($value)
{
if (!empty($value)) {
File::delete(public_path($value));
}
}
I had to do this to solve my problem.
The url was being taken by the configuration set on filesystems.php under config file.
public function deleteImageFromStorage($value)
{
$path_explode = explode('/', (parse_url($value))['path']); //breaking the full url
$path_array = [];
array_push($path_array, $path_explode[2], $path_explode[3]); // storing the value of path_explode 2 and 3 in path_array array
$old_image = implode('/', $path_array);
if ($old_image) {
Storage::delete($old_image);
}
}
If someone goes through the same problem in the future, this might be helpful.

Copy Folder file to another Folder

How can I copy all the file inside a folder to another folder using laravel
I tried this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use XBase\Table;
use File;
class WorkController extends Controller
{
//
public function index()
{
$dir = public_path('dbf');
// $new_path = public_path('new_files');
$files1 = scandir($dir);
$count=count($files1);
for($i=1;$i<$count;$i++)
{
if($files1[$i]!='.' && $files1[$i]!='..')
{
$file=$files1[$i];
//$myfile = fopen($file, "r");
$table = new Table(public_path('dbf\\') . $file);
$table->move('coped_Data',$file);
while ($record = $table->nextRecord())
{
echo "For".$i."<br>".$record->folio_no;
}
}
}
}
}
The above one is the whole code
its showing me this error
" Call to undefined method XBase\Table::move() "

Creating zip of multiple files and download in laravel

i am using the following codes to make zip and allow user to download the zip
but its not working.it shows the error as ZipArchive::close(): Read error: Bad file descriptor.What might be the problem?i am working with laravel.
public function downloadposts(int $id)
{
$post = Post::find($id);
// Define Dir Folder
$public_dir = public_path() . DIRECTORY_SEPARATOR . 'uploads/post/zip';
$file_path = public_path() . DIRECTORY_SEPARATOR . 'uploads/post';
// Zip File Name
$zipFileName = $post->post_title . '.zip';
// Create ZipArchive Obj
$zip = new ZipArchive();
if ($zip->open($public_dir . DIRECTORY_SEPARATOR . $zipFileName, ZipArchive::CREATE) === TRUE) {
// Add File in ZipArchive
foreach ($post->PostDetails as $postdetails) {
$zip->addFile($file_path, $postdetails->file_name);
}
// Close ZipArchive
$zip->close();
}
// Set Header
$headers = [
'Content-Type' => 'application/octet-stream',
];
$filetopath = $public_dir . '/' . $zipFileName;
dd($filetopath);
// Create Download Response
if (file_exists($filetopath)) {
return response()->download($filetopath, $zipFileName, $headers);
}
return redirect()->back();
}
For Laravel 7.29.3 PHP 7.4.11
Create a GET route in api.php
Route::get('/downloadZip','ZipController#download')->name('download');
Create controller ZipController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use File;
class ZipController extends Controller
{
public function download(Request $request)
{
$zip = new \ZipArchive();
$fileName = 'zipFile.zip';
if ($zip->open(public_path($fileName), \ZipArchive::CREATE)== TRUE)
{
$files = File::files(public_path('myFiles'));
foreach ($files as $key => $value){
$relativeName = basename($value);
$zip->addFile($value, $relativeName);
}
$zip->close();
}
return response()->download(public_path($fileName));
}
}
In the public folder make sure you have a folder myFiles. This snippet will get every file within the folder, create a new zip file and put within the public folder, then when route is called it returns the zip file created.
Only pure php code.
public function makeZipWithFiles(string $zipPathAndName, array $filesAndPaths): void {
$zip = new ZipArchive();
$tempFile = tmpfile();
$tempFileUri = stream_get_meta_data($tempFile)['uri'];
if ($zip->open($tempFileUri, ZipArchive::CREATE) !== TRUE) {
echo 'Could not open ZIP file.';
return;
}
// Add File in ZipArchive
foreach($filesAndPaths as $file)
{
if (! $zip->addFile($file, basename($file))) {
echo 'Could not add file to ZIP: ' . $file;
}
}
// Close ZipArchive
$zip->close();
echo 'Path:' . $zipPathAndName;
rename($tempFileUri, $zipPathAndName);
}
I will suggest you to use Zipper package
Try below code for creating zip of multiple files :
public function downloadZip($id)
{
$headers = ["Content-Type"=>"application/zip"];
$fileName = $id.".zip"; // name of zip
Zipper::make(public_path('/documents/'.$id.'.zip')) //file path for zip file
->add(public_path()."/documents/".$id.'/')->close(); //files to be zipped
return response()
->download(public_path('/documents/'.$fileName),$fileName, $headers);
}
you can use the following code
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\ImgUpload;
use ZipArchive;
use File;
class UserController extends Controller
{
/**
* Function to get all images from DB
*/
public function downloadZip()
{
$data = ImgUpload::all();
foreach($data as $key => $value)
{
$imgarr[] = "storage/image". '/' . $value->image;
}
$ziplink = $this->converToZip($imgarr);
return $ziplink;
}
/**
* Function to covert all DB files to Zip
*/
public function converToZip($imgarr)
{
$zip = new ZipArchive;
$storage_path = 'storage/image';
$timeName = time();
$zipFileName = $storage_path . '/' . $timeName . '.zip';
$zipPath = asset($zipFileName);
if ($zip->open(($zipFileName), ZipArchive::CREATE) === true) {
foreach ($imgarr as $relativName) {
$zip->addFile($relativName,"/".$timeName."/".basename($relativName));
}
$zip->close();
if ($zip->open($zipFileName) === true) {
return $zipPath;
} else {
return false;
}
}
}
}
you can refer this link for more information
The question got answer, but I am posting this solution for those who wants to download dynamically zip some (based on id) files from the same folder, I hope it might help them that how to create dynamic zip file of multiple files/images affiliated with some id.
I would be taking example of multiple images. You can do the same for files.
Assuming the above table the autos_id is foreign key and based on the autos_id, there are multiple images store in the database.
To make zip file of it I will do the following:
public function downloadZip($id)
{
$data = AutoImage::where('autos_id',$id)->get();
$imgarr=[];
foreach($data as $data){
$file = storage_path() . '/app/public/autoImages/'.$data->image_name;
if(\File::exists(public_path('storage/autoImages/'.$data->image_name))){
$imgarr[]= public_path('storage/autoImages/'.$data->image_name);
}
}
$zip = new ZipArchive;
$fileName = 'AutoImages.zip';
/*OVERWRITE will not make a different zip file on server but it will
replace the one which is in the server, this approach will help you to not
make multiple zip files, if you want to creat new you can do it with unique
name of the zip file and adding CREATE instead of OVERWRITE.*/
if ($zip->open(public_path($fileName), ZipArchive::OVERWRITE) === TRUE)
{
$files = $imgarr; //passing the above array
foreach ($files as $key => $value) {
$relativeNameInZipFile = basename($value);
$zip->addFile($value, $relativeNameInZipFile);
}
$zip->close();
}
return response()->download(public_path($fileName));
}
Note: for file storage I used storage and then made a link symlink for storing files.
For further info of file storage: https://laravel.com/docs/9.x/filesystem
This works for me, multiple files zip and download.
public function download_attachment($ticket_no)
{
$zip = new \ZipArchive();
$fileName = $ticket_no.'.zip';
if ($zip->open(public_path($fileName), \ZipArchive::CREATE)== TRUE)
{
$files = File::files(public_path('uploads/tickets/' . $ticket_no));
foreach ($files as $key => $value){
$relativeName = basename($value);
$zip->addFile($value, $relativeName);
}
$zip->close();
}
return response()->download(public_path($fileName));
}

ReflectionException in laravel. Class does not exist

Why does laravel cannot detect my class this time. In my previous tries it worked but this time it does not. Here are my codes :
DataEntry.php
<?php namespace App\Server ;
abstract class DataEntry{
/*This class facilitates the
storing of data to the server...
*/
abstract protected function storeData($data);
abstract protected function updateData($data ,$id);
}
MedicineDosageEntry.php
<?php
namespace App\Server ;
use Image;
use App\Dosage ;
use App\Photo ;
use App\Server\DataEntry ;
use Request ;
use Auth ;
class MedicineDosageEntry extends DataEntry{
public function storeData ($data){
$file = $data['photo'] ;
$fileName = uniqid().$file->getClientOriginalName() ;
if(!file_exists('medicine/images')){
mkdir('medicine/images', 0777, true);
}
$file->move('medicine/images', $fileName);
if(!file_exists('medicine/images/thumbs')){
mkdir('medicine/images/thumbs', 0777, true);
}
$thumb = Image::make('medicine/images/' .$fileName)->resize(150,150)->save('medicine/images/thumbs/' . $fileName,50);
$dosage = new Dosage;
$dosage->dosage_name = $data['dosage_name'];
$dosage->form = $data['form'];
$dosage->medicine_id = $data['medicine_id'];
$dosage->price = $data['price'];
$dosage->save();
$dosage->photo()->create([
'dosage_id' => $data['id'];
'file_name' => $fileName,
'file_size' => $file->getClientSize(),
'file_mime' => $file->getClientMimeType(),
'file_path' => 'medicine/images/thumbs'. $fileName,
'created_by'=> Auth::user()->id,
]);
}
public function updateData($data , $id) {
}
}
MedicineController.php
<?php namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\DB\MedicineRepository;
use App\Http\Requests\MedicineRequest ;
use App\Dosage ;
use App\Medicine;
use Auth ;
use App\Server\MedicineDosageEntry ;
use Flash ;
class MedicineController extends Controller {
public function store(MedicineRequest $request, MedicineDosageEntry $dosage ){
$requestData = $request->all();
$dosage->storeData($requestData);
}
}
Do i have to register this class. Why do I encounter this. It worked out in my previous tries but after doing some refactoring and coding. It crashed..

codeigniter template engine like in Wordpress Or Joomla

Is there a Codeigniter Library or extension which would make possible dynamic templating like in Wordpress or Joomla. What I mean I would like to point my controller to a view which is specified by admin from back.
than I was starting to create by myself but till this point without any success
controller
--main_conroler
here some trying what did not succeed
class MainController extends CI_Controller {
/* Initiate Site
*/
private $method;
private $data;
function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->helper('language');
$this->method = $this->router->fetch_method();
if ($this->method == "index") {
$this->data['view'] = 'templates/appStrapp';
} elseif ($this->method != 'site' && method_exists(__CLASS__, $this->method)) {
$this->data['view'] = $this->method;
}
if (empty($this->data['view'])) {
show_404();
}
}
View
view
--templates
---default
----index.php
than here I would like to route my template parts
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
if (file_exists(dirname(__FILE__) . '/tmpl/' . $view . '.php')) {
include ( dirname(__FILE__) . '/tmpl/header.php');
include ( dirname(__FILE__) . '/tmpl/navigation.php');
$this->load->view('site/tmpl/' . $view);
include ( dirname(__FILE__) . '/tmpl/footer.php');
} else {
var_dump('test');
show_404();
}
?>
I've used the following for templates in CI and it's a nice setup.
https://github.com/philsturgeon/codeigniter-template

Resources