I'm trying to upload file using Kunnu Dropbox API. It works really well in localhost. But, when I try to upload from live server, it always upload empty file. What am i missing ?
Here is the code to upload :
$pathToLocalFile = base_url() . "\\public\\dropbox_file\\" . $data['upload_data']['file_name'];
$dropBox = $this->load->library("DropBox_lib");
$drop_obj = new DropBox_lib();
$drop_obj->set_mode_file(1);
$drop_obj->set_drop_file($pathToLocalFile);
$drop_n = $drop_obj->drop_object;
$file = $drop_n->simpleUpload($drop_obj->drop_file, $path_f . "/" . $data['upload_data']['file_name'], ['autorename' => true]);
Here is the Library of Dropbox_lib.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
use Kunnu\Dropbox\Dropbox;
use Kunnu\Dropbox\DropboxApp;
use Kunnu\Dropbox\DropboxFile;
class DropBox_lib{
public $drop_object;
public $drop_file;
public $drop_mode;
public function __construct()
{
require_once APPPATH.'third_party/DropBox/vendor/autoload.php';
//Configure Dropbox Application
$app = new DropboxApp("xxxxxxxxxxxxxxxxx", "yyyyyyyyyyyyyyyyy", "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz");
//Configure Dropbox service
$this->drop_object = new Dropbox($app);
}
public function set_mode_file($mode){
if($mode == 1){
$mode = DropboxFile::MODE_READ;
$this->drop_mode = $mode;
}else{
$mode = DropboxFile::MODE_READ;
$this->drop_mode = $mode;
}
}
public function set_drop_file($pathlocal){
$dropboxFile = new DropboxFile($pathlocal, $this->drop_mode);
$this->drop_file = $dropboxFile;
}
}
?>
The same codes work really well on localhost.
Related
So far i have tried below:
...
public function upload()
{
$jwplatform_api = new Jwplayer\JwplatformAPI('my_key', 'my_secret');
$target_file = 'upload/vids/course/bede6b9c266b876fc2f0dea7a86cf8bd.mp4';
$params = array();
$params['title'] = 'PHP API Test Upload';
$params['description'] = 'Video description here';
// Create video metadata
$create_response = json_encode($jwplatform_api->call('/videos/create', $params));
$decoded = json_decode(trim($create_response), TRUE);
$upload_link = $decoded['link'];
$upload_response = $jwplatform_api->upload($upload_link, $target_file);
print_r($upload_response);
}
...
But no luck, it says "Class 'Jwplayer\JwplatformAPI' not found".
And yeah, i have put the files I got from https://github.com/jwplayer/jwplatform-php in the ROOT position inside a folder named "jwplatform-php".
Ok since you don't want to use composer - here is a guide
1. Download as Zip
2. Create a folder
In your folder application/third_party/ create a folder called jwplatformapi/
3. Unpack the init.php and the src folder
Unpack from your zip file the init.php and the src folder into your application/third_party/jwplatformapi/ folder
it should looke like
4. Create your library
Create a file called Jwplatform_library.php in your application/libraries/ folder
class Jwplatform_library
{
private $key;
private $secret;
public function __construct($key = 'my_key', $secret = 'my_secret')
{
$this->key = $key;
$this->secret = $secret;
}
public function get()
{
require_once(APPPATH.'third_party/jwplatformapi/init.php');
return new Jwplayer\JwplatformAPI($this->key, $this->secret);
}
}
5. use it in one of your controllers
public function upload()
{
$this->load->library('Jwplatform_library', ['my_key', 'my_secret']);
$obj = $this->jwplatform_library->get();
var_dump($obj);
}
"I'm tring to upload file using custome library in laravel file goes in folder successfully but file isn't upload in database
this is my custom library:-
namespace App\Classes;
use Illuminate\Http\Request;
class Hello
{
static function jai(Request $request)
{
if($request->hasfile('name'))
{
$image=$request->file('name');
$new_image=time().'.'.$image->getClientOriginalName();
$image->move(public_path('image/'),$new_image);
}
}
}
?>
and this is my controller store function :-
Hello::jai($request);
$x=$request->all();
$x['name']=
$j=Hello::jai($request)->new_image;
Cruds::create($x);
The file "[000004].jpg" was not uploaded due to an unknown error.
Hope this will help.
public function upload(Request $request){
if($file = $request->file('file')){
$name = $file->getClientOriginalName();
if($file->move('files',$name)){
$file= new Files();//DB instance modal
$file->url= $name;
$file->save();
return "success";
}
}
You forgot the file extension. See if this help you.
$image = $request->file('your_input_form_file_name');
$image = time() . '_' . $image->getClientOriginalName() . '.' . $file->getClientOriginalExtension();
$image->move(public_path('images/'), $name);
I have try to implement the google cloud vision with API ImageAnnotator using a codeigniter PHP.
I have install the require google cloud vision using a composer to my third party directory in codeigniter.
This is the code looks like in my controller :
defined('BASEPATH') OR exit('No direct script access allowed');
use Google\Auth\ApplicationDefaultCredentials;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
class Manage_center extends CI_Controller {
function __construct() {
parent::__construct();
include APPPATH . 'third_party/vendor/autoload.php';
}
public function index()
{
$this->load->view('index');
}
function upload_ocr_image()
{
//img_data contain image => i just shorten the code.
$img_data = $this->upload->data();
// Authenticating with a keyfile path.
putenv('GOOGLE_APPLICATION_CREDENTIALS='.base_url().'assets/google_cloud_vision/credentials.json');
$scopes = ['https://www.googleapis.com/auth/cloud-vision'];
// create middleware
$middleware = ApplicationDefaultCredentials::getMiddleware($scopes);
$stack = HandlerStack::create();
$stack->push($middleware);
$imageAnnotator = new ImageAnnotatorClient();
# annotate the image
$response = $imageAnnotator->textDetection($img_data['full_path']);
$texts = $response->getTextAnnotations();
printf('%d texts found:' . PHP_EOL, count($texts));
foreach ($texts as $text) {
print($text->getDescription() . PHP_EOL);
# get bounds
$vertices = $text->getBoundingPoly()->getVertices();
$bounds = [];
foreach ($vertices as $vertex) {
$bounds[] = sprintf('(%d,%d)', $vertex->getX(), $vertex->getY());
}
print('Bounds: ' . join(', ',$bounds) . PHP_EOL);
}
$imageAnnotator->close();
}
}
I got the error :
Type: DomainException Message: Unable to read the credential
file specified by GOOGLE_APPLICATION_CREDENTIALS: file
http://localhost/theseeds/assets/google_cloud_vision/credentials.json
does not exist Filename:
D:\xampp\htdocs\theseeds\application\third_party\vendor\google\auth\src\CredentialsLoader.php
Line Number: 74
File:
D:\xampp\htdocs\theseeds\application\controllers\Manage_center.php Line: 3188 Function: getMiddleware
I dont understand why this error occur :
http://localhost/theseeds/assets/google_cloud_vision/credentials.json does not exist
Because when i opened the link the file is there.
And this error :
File:
D:\xampp\htdocs\theseeds\application\controllers\Admin_center.php Line: 3188 Function: getMiddleware
is a line code :
$middleware = ApplicationDefaultCredentials::getMiddleware($scopes);
What is the proper way to use the google cloud vision ImageAnnotatorClient in codeigniter PHP ?
Is there a problem with the authentication to google cloud api ?
Thank You
I found the solution myself.
This is how the right way to use the google cloud ImageAnnotator with service account key.
defined('BASEPATH') OR exit('No direct script access allowed');
use Google\Cloud\Vision\VisionClient;
class Admin_center extends CI_Controller {
function __construct() {
parent::__construct();
include APPPATH . 'third_party/vendor/autoload.php';
}
public function index() {
$this->load->view('index');
}
function upload_ocr_image() {
$img_data = $this->upload->data();
$vision = new VisionClient(['keyFile' => json_decode(file_get_contents('credentials.json'), true)]);
$imageRes = fopen($img_data['full_path'], 'r');
$image = $vision->image($imageRes,['Text_Detection']);
$result = $vision->annotate($image);
print_r($result);
}
}
I want to write a class and save it into application/models/ folder.
The code is as below:-
function writeANewFile()
{
$path = "/LMSV1/application/models/";
$classname = "firstidgenerator";
$this->load->helper('file');
$data = "<?php class ".$classname." extends CI_Model {
function generateId(\$db)
{
\$data['orders'] = 'orders';
\$this->\$db->trans_start();
\$this->\$db->insert('$classname', \$data);
\$insert_id = \$this->\$db->insert_id();
\$this->\$db->trans_complete();
return \$insert_id;
}
} ";
$result = write_file(''.$path.''.$classname.'.php', $data);
echo json_encode($result);
} //end fucntion
If i give the $path = "c:/xampp/htdocs/FrameWorks/LMSV1/application/models/"; then it successfully saves a file in desired folder.
But if if i give $path = "/LMSV1/application/models/"; then it returns false and does not create a file.
The problem lies in setting path and i could not successfully figure out what should be the path to be given as parameter?
Codeigniter has a few path constants that are useful in this case. The constant APPPATH is what you need.
$path = APPPATH . "models/";
I want to be able to add many articles programmatically in Joomla, from the command line using the cli feature in Joomla CMS.
I am basically using Create a Joomla! Article Programmatically but my script closes out after creating just one article with the error line
Error displaying the error page: Application Instantiation
Error:Application Instantiation Error
This is the code that I am running from within the /cli folder in Joomla.
I am using Joomla 3.4
<?php
const _JEXEC = 1;
if (file_exists(dirname(__DIR__) . '/defines.php'))
{
require_once dirname(__DIR__) . '/defines.php';
}
if (!defined('_JDEFINES'))
{
define('JPATH_BASE', dirname(__DIR__));
require_once JPATH_BASE . '/includes/defines.php';
}
require_once JPATH_LIBRARIES . '/import.legacy.php';
require_once JPATH_LIBRARIES . '/cms.php';
require_once JPATH_CONFIGURATION . '/configuration.php';
class AddArticle extends JApplicationCli
{
public function doExecute()
{
$count = 10;
while ($count > 0)
{
$count--;
$jarticle = new stdClass();
$jarticle->title = 'New article added programmatically' . rand();
$jarticle->introtext = '<p>A programmatically created article</p>';
$table = JTable::getInstance('content', 'JTable');
$data = (array)$jarticle;
// Bind data
if (!$table->bind($data))
{
die('bind error');
return false;
}
// Check the data.
if (!$table->check())
{
die('check error');
return false;
}
// Store the data.
if (!$table->store())
{
die('store error');
return false;
}
}
}
}
JApplicationCli::getInstance('AddArticle')->execute();
I was able to find the answer to this as it had been raised as an issue at github, so I am posting that solution here.
https://github.com/joomla/joomla-cms/issues/7028
It is necessary to register the application like this, if the command line app uses JTable:
class MakeSql extends JApplicationCli
{
public function __construct()
{
parent::__construct();
JFactory::$application = $this; // this is necessary if using JTable
}
public function doExecute()
{
$db = JFactory::getDbo();
// ... etc etc ...
I did this and it worked fine.