requiring a file throws unexpected error - include

I have a class that I included into another class, but I get an error:
PHP Parse error: syntax error, unexpected '$path' (T_VARIABLE)
Inside the class the $path variable is actually commented out.
Here is part of the class I'm including:
class SQLite {
private $name = '';
private $dir = '';
private $handle = '';
public function __construct($class) {
if(!$class) exit('Must supply class name.');
$name = $this->name = preg_replace('/[^a-zA-Z0-9]+/', '_', $class);
$dir = $this->dir = str_replace(basename(__DIR__), 'db', __DIR__);
#$path = $this->dir . DIRECTORY_SEPARATOR . $this->name . '.sqlite3';
I can't figure out why I'm getting this error. I've tried using require(), include(), require_once() etc, but to no avail. I'm sure the files are being saved. I've tried extending my new class via the one I'm including, and I've also tried creating an instance of it in the new class's constructor.

had new PDO('sqlite:' $path) instead of new PDO('sqlite:' . $path);

Related

Laravel Class 'Iyzipay\Options' not found

<?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) );

Laravel 8 how to use setter method to upload an image

I was using the old Laravel setter method in the model to upload an icon image when creating a new Category
class Category extends Model
{
public function setIconAttribute($value)
{
$fileName = 'Icon_'. $this->name .'_'.rand(11111,99999).'.'.$value->getClientOriginalExtension();
$destinationPath = public_path('images/Icon');
$value->move($destinationPath, $fileName); // uploading file to given path
$this->attributes['icon'] = $fileName;
}
}
and it was working fine ,
now I am upgrading to the new laravel 8.77 setter
protected function icon(): Attribute
{
return Attribute::make (
get: fn ($value) => asset('images/Icon/'.$value),
set: function ($value) {
$fileName = 'Icon_'.rand(11111,99999).'.'.$value->getClientOriginalExtension(); // renameing image
$destinationPath = public_path('images/Icon');
$value->move($destinationPath, $fileName); // uploading file to given path
return $fileName;
}
);
}
and it gets me an error
The file "Finishe_30574.png" was not uploaded due to an unknown error.
so first how to fix this error
and how to add the attribute name ($this->name) in the file name - in the new Laravel-8 setter like the old way
$fileName = 'Icon_'. $this->name .'_'.rand(11111,99999).'.'.$value->getClientOriginalExtension();
because adding ($this->name) in the new setter way getts an error

How do grant rw access to public folder in laravel

I get this error after i migrated my project from windows to mac.
The "/private/var/folders/6w/zypn4xb120l6x6f1kjx9_nxw0000gn/T/phpwroBVT" file does not exist or is not readable.
here is my code
if($request->hasFile('image')){
$image = $request->file('image');
$image_name = $image->getClientOriginalName();
$destinationPath = public_path('/images/services');
$image->move($destinationPath, $image_name);
$summary = $request->summary;
$body = $request->body;
$title = $request->title;
$service = Service::create([
'title'=> $title,
'summary'=>$summary,
'body'=>$body,
'image'=> $image
]
);
if($service){
return redirect()->back()->with('success','services added');
}
}
the images goes into the public/images/services folder but i get the above error
I faced the similar error and I found out that i was accessing the global variable declared in the controller without "this" pointer reference. For example
class BlogController extends Controller
{
public $attachment_folder_name = "/blogs/";
}
You should access this variable with following syntax.
echo $this->attachment_folder_name
And not like this
echo $attachment_folder_name

Use Dropbox API with CodeIgniter

Trying to reuse the following Dropbox API code within CodeIgniter. The issue is getting it to work within the constraints of class methods & constuctors:
require_once('../dropbox-sdk-1.1.4/Dropbox/autoload.php');
use \Dropbox as dbx;
$accessToken = 'DROPBOX_ACCESSTOKEN';
$dbxClient = new dbx\Client($accessToken, "PHP-Example/1.0");
Needs to be something like the following, but doesn't like the 'use \Dropbox as dbx' line, amongst others:
class Controller_name extends CI_Controller
{
public function __construct()
{
parent::__construct();
require_once('../dropbox-sdk-1.1.4/Dropbox/autoload.php');
use \Dropbox as dbx;
}
public function access_dropbox()
{
$accessToken = 'DROPBOX_ACCESSTOKEN';
$dbxClient = new dbx\Client($accessToken, "PHP-Example/1.0");
$file = 'file.txt';
$f = fopen( $file, "rb" );
$result = $dbxClient->uploadFile( "/$file", dbx\WriteMode::add(), $f);
fclose($f);
}
}
Using the code below I'm getting the following error message:
An uncaught Exception was encountered
Type: Kunnu\Dropbox\Exceptions\DropboxClientException
Message: Error in call to API function "files/upload": HTTP header
"Dropbox-API-Arg": path: 'db_backup' did not match pattern
'(/(.|[\r\n]))|(ns:[0-9]+(/.)?)|(id:.*)'
Filename:
/opt/lampp/htdocs/codeig-smythes/vendor/kunalvarma05/dropbox-php-sdk/src/Dropbox/Http/Clients/DropboxGuzzleHttpClient.php
Line Number: 59
$file_path = 'public/sql_backup/db_backup_' .date("Y-m-d"). '.sql';
require_once('../vendor/autoload.php');
$app = new Kunnu\Dropbox\DropboxApp(
'APP_KEY',
'APP_SECRET',
'ACCESS_TOKEN'
);
$dropbox = new Kunnu\Dropbox\Dropbox($app);
$dropboxFile = new Kunnu\Dropbox\DropboxFile(realpath($file_path));
$file = $dropbox->upload(
$dropboxFile, basename($file_path), array('autorename' => TRUE)
);
Working with the dropbox API is pretty easy. I use a package found on github:
https://github.com/kunalvarma05/dropbox-php-sdk
I am not using CodeIgniter 3's composer autoload feature. Also, my vendor directory is located at FCPATH.
Since it looks like you want to upload a file, I'll show you that example:
$appKey = '77fgftsb77joj77';
$appSecret = 'fw77777tspam5y';
$accessToken = 'PMP7777777AAAAAAADFC_6JI7777777hY8xYhO7777777MJkpCKbBv';
if( is_file( $file_path ) )
{
$file_path = realpath( $file_path );
$file_name = basename( $file_path );
require FCPATH . 'vendor/autoload.php';
$app = new Kunnu\Dropbox\DropboxApp(
$appKey,
$appSecret,
$accessToken
);
$dropbox = new Kunnu\Dropbox\Dropbox($app);
$dropboxFile = new Kunnu\Dropbox\DropboxFile(
$file_path
);
$file = $dropbox->upload(
$dropboxFile,
'/backups/website/' . $file_name,
[
'autorename' => TRUE
]
);
}

In ZF2 how to Escape HTML in Custom View Helpers

Here, I see someone suggesting to extend the Zend\View\Helper\Escaper\AbstractHelper;, resulting in something like this:
namespace Photo\View\Helper;
use Zend\View\Helper\Escaper\AbstractHelper;
class Imghelper extends AbstractHelper
{
protected $_baseUrl = null;
protected $_exists = array();
public function __construct ()
{
$url = PUBLIC_PATH;
$root = '/' . trim($url, '/');
if ($root == "/")
{$root = "";}
$this->_baseUrl = $root . "/";
}
public function escape($value)
{
return $this->getEscaper()->escapeHtml($value);
}
public function __invoke($path, $params = array())
{
$plist = array();
$str = null;
$imgpath = $this->_baseUrl . ltrim($path);
if (!isset($this->_exists[$path]))
{$this->_exists[$path] = file_exists(realpath($imgpath));}
if (!isset($params['alt']))
{$params['alt'] = '';}
foreach ($params as $param => $value)
{$plist[] = $param . '="' . $this->escape($value) . '"';}
$str = " ".join(" ", $plist);
$p = ($this->_exists[$path]) ? $this->_baseUrl.ltrim($path,'/') : null;
if (isset($p))
{return '<img src="'.$p.'" '.$str.' />';}
}
}
However, when I try it in my codes, I get an Error
Strict Standards: Declaration of Application\View\Helper\Imghelper::__invoke() should be compatible with Zend\View\Helper\Escaper\AbstractHelper::__invoke($value, $recurse = 0) in ...
Is it ok to error the error message? Or is there any better ways to do this? Thanks!
I wouldn't recommend extending the Abstract Escape helper since you aren't writing an escape helper. Stick to your original plan and just extend Zend\View\Helper\AbstractHelper. You can then access the escape helpers via. $this->view->escapeHtml(), $this->view->escapeJs() and so on.

Resources