upload videos file with jwplatform [PHP] - php

so here is my problem:
I want to use jwplatform-php to upload my video files.
here is my php script: (this is more or less the sample script provided)
<?php
use Jwplayer\JwplatformClient;
header_remove( 'X-Powered-By' );
chdir(dirname(__DIR__));
require 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
$secret = "XXXXXXXXXXXXXXXXXXX";
$site_id = "XXXXX";
$jwplatform_api = new JwplatformClient($secret);
$target_file = "test.mp4";
$params = [];
$params['metadata'] = [];
$params['metadata']['title'] = 'PHP API Test Upload';
$params['metadata']['description'] = 'Video description here';
$params['upload'] = [];
$params['upload']['method'] = 'direct';
// Create the example media
$create_response = json_encode($jwplatform_api->Media->create($site_id, $params));
print_r($create_response);
print("\n");
$decoded = json_decode(json_decode(trim($create_response), true), true);
$upload_link = $decoded['upload_link'];
// Upload the media file
$upload_response = $jwplatform_api->Media->upload($target_file, $upload_link);
print_r($upload_response);
print("\n");
And so the error occurs at $create_response he return false.
I think the problem is $target_file = "test.mp4"; that he can't solve even by putting the relative path $target_file = dirname(__FILE__) . DIRECTORY_SEPARATOR ."test.mp4"; he returns to me false.
I don't know what to do and in addition the doc jwplatform-php is almost non-existent
I thank you already for all the help provided and sorry for my approximate English...

Related

HTML2PDF 504 Gateway Timeout

I am trying to convert HTML 2 PDF with this library and its converting fine as per my expectations.
However, When I convert with more data, its giving me an error saying 504 Gateway Timeout. Here is the error screenshot what I am getting.
In local server its working fine. I have the same server in my local and my live (Linux). The only problem is I am getting when I try to produce PDF with long data on live server.
I researched and found out that to increase php execution time and other settings. Hence I try to put below code in my .php file.
ini_set('max_execution_time', 60000);
ini_set('post_max_size','128M');
ini_set('upload_max_filesize','128M');
I even try to set max_execution_time to 0 and -1 but yet its not working for me. After setting this values, I even printed the updated values with phpinfo(), the values are overwriting but I am having the same 502 Gateway timeout error. Here is small chunk of code just in case you want to see.
<?php
ini_set('max_execution_time', 60000);
ini_set('post_max_size','20M');
ini_set('upload_max_filesize','8M');
require_once dirname(__FILE__) . '/vendor/autoload.php';
require_once dirname(__FILE__) . '/templateInfo.php';
use Spipu\Html2Pdf\Html2Pdf;
use Spipu\Html2Pdf\Exception\Html2PdfException;
use Spipu\Html2Pdf\Exception\ExceptionFormatter;
use technobrave\brochuresettings\Models\Brochuresettings as BS;
use Technobrave\Transactions\Models\Transactions as TR;
use Technobrave\Offices\Models\Offices;
use technobrave\themesettings\Models\ThemeSetting as TS;
use Technobrave\Team\Models\Team;
class generateTemplate {
public $theme = "";
public $theme_settings = array();
public function __construct($templateId, $resolution , $theme ,$pdf_sections = array(),$openFile = false, $finalPdfFile = null) {
$this->getBrochureTransactionData = BS::first();
$this->getPdfSection = $pdf_sections;
$this->theme_settings = TS::first();
$this->theme = $theme;
$this->baseUrl = url(Config::get('cms'));
$this->teamPageName = $this->baseUrl . '/our-team';
$this->capabilitiesPageName = $this->baseUrl . '/capabilities';
$this->getFooterText = $this->getFooterText();
$getTeamId = (isset($_GET['teamId']) && !empty($_GET['teamId'])) ? $_GET['teamId'] : "";
$this->uniquePath = __DIR__ . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR;
$templatePath = __DIR__ . DIRECTORY_SEPARATOR . 'regency_template' . DIRECTORY_SEPARATOR . $getTeamId . DIRECTORY_SEPARATOR . $templateId . '.php';
$templateInfoText = new templateInfo($templateId, $this->uniquePath, $getTeamId);
$this->customImagePath = $this->uniquePath;
foreach ($templateInfoText->defaultValues as $key => $value) {
$this->{$key} = $value;
}
$template = file_get_contents($templatePath);
try
{
$html2pdf = new Html2Pdf('L','A4', 'en', true, 'UTF-8', array(0, 0, 0, 0));
$html2pdf->Addfont('perpetua');
$html2pdf->Addfont('montserratbold');
$html2pdf->Addfont('montserratmedium');
$html2pdf->Addfont('montserratregular');
$html2pdf->Addfont('montserratsembold');
$html2pdf->Addfont('montserratitalic');
$html2pdf->writeHTML($template, false);
$html2pdf->Output('regency_corporate_brochure.pdf', 'D');
} catch (Html2PdfException $e) {
$formatter = new ExceptionFormater($e);
echo $formatter->getHtmlMessage();
}
}
}
I tried to preview how my HTML is generating and its generating without any error.
$html2pdf->writeHTML($template, true);
So basically I am facing server issue here as per my understanding so far.
Can someone guide me what should I do from here on to solve this issue.
After a hard debugging I found that my problem was that I was including an external image in the PDF and the server can't reach the server image (server access only trough white-list IP).

Class 'google\appengine\CreateUploadURLRequest' not found

i am using google cloud using CORE PHP to upload file using simple HTML Form But i am stuck on CloudStorageTools Class
it throws continuous following error
Fatal error: Class
'google\appengine\api\cloud_storage\CloudStorageTools' not found
For Solve that i Included
include_once __DIR__ . '/vendor/autoload.php';
//Include class
require_once "google/appengine/api/cloud_storage/CloudStorageTools.php";
use google\appengine\api\cloud_storage\CloudStorageTools;
But than it throws second error. where am i going wrong in google cloud setup.
My full code looks like
$bucket = 'bucketname';
$root_path = 'gs://' . $bucket . '/';
$options = ['gs_bucket_name' => $bucket];
$upload_url = CloudStorageTools::createUploadUrl('/upload/handler', $options);
$_url = '';
if(isset($_POST['submit']))
{
if(isset($_FILES['userfile']))
{
$name = $_FILES['userfile']['name'];
$file_size =$_FILES['userfile']['size'];
$file_tmp =$_FILES['userfile']['tmp_name'];
$original = $root_path .$name;
move_uploaded_file($file_tmp, $original);
$_url=CloudStorageTools::getImageServingUrl($original);
}
}

unlink is showing error that is not present

I am trying to delete image from server but unlink is showing error that is not a error at all. My code is given below:
private function delete_image($ad_id){
$this->load->helper('file');
$sql = "SELECT image1,image2,image3 from ads where AdId = ?";
$query = $this->db->query($sql,array($ad_id));
//for fetching result
$result = $query->result_array();
//to make a array of all images
$img = [$result[0]['image1'],$result[0]['image2'],$result[0]['image3']];
$i = 0;
while ($img[$i]!= "edubuylogo.png" && $i<3){
unlink('./uploads/'.$img[$i]);
$i++;
}
}
and the link to image is http://localhost/edubuy/uploads/IMG_20180120_210433.jpg
And image is perfectly loading.
The error is Message:
unlink(/uploads/IMG_20180120_210433.jpg): No such file or directory
It seems that your computer is confused, and you should specify an absolute path. In CodeIgniter you can use the FCPATH constant, so do like this:
unlink( FCPATH . 'uploads/' . $img[$i] );
That assumes your index.php file is inside edubuy. If it's not then:
unlink( FCPATH . 'edubuy/uploads/' . $img[$i] );

Create Folder and Insert if Not Present

I wonder whether someone may be able to help me please.
I'm using Aurigma's Image uploader software to allow users to upload images for locations they visit. The information is saved via the script shown below:
<?php
//This variable specifies relative path to the folder, where the gallery with uploaded files is located.
//Do not forget about the slash in the end of the folder name.
$galleryPath = 'UploadedFiles/';
require_once 'Includes/gallery_helper.php';
require_once 'ImageUploaderPHP/UploadHandler.class.php';
/**
* FileUploaded callback function
* #param $uploadedFile UploadedFile
*/
function onFileUploaded($uploadedFile) {
$packageFields = $uploadedFile->getPackage()->getPackageFields();
$username=$packageFields["username"];
$locationid=$packageFields["locationid"];
global $galleryPath;
$absGalleryPath = realpath($galleryPath) . DIRECTORY_SEPARATOR;
$absThumbnailsPath = $absGalleryPath . 'Thumbnails' . DIRECTORY_SEPARATOR;
if ($uploadedFile->getPackage()->getPackageIndex() == 0 && $uploadedFile->getIndex() == 0) {
initGallery($absGalleryPath, $absThumbnailsPath, FALSE);
}
$locationfolder = $_POST['locationid'];
$locationfolder = preg_replace('/[^a-z0-9_\-\.()\[\]{}]/i', '_', $locationfolder);
if (!is_dir($absGalleryPath . $locationfolder)) {
mkdir($absGalleryPath . $locationfolder, 0777);
}
$dirName = $_POST['folder'];
$dirName = preg_replace('/[^a-z0-9_\-\.()\[\]{}]/i', '_', $dirName);
if (!is_dir($absGalleryPath . $dirName)) {
mkdir($absGalleryPath . $dirName, 0777);
}
$path = rtrim($dirName, '/\\') . '/';
$originalFileName = $uploadedFile->getSourceName();
$files = $uploadedFile->getConvertedFiles();
// save converter 1
$sourceFileName = getSafeFileName($absGalleryPath, $originalFileName);
$sourceFile = $files[0];
/* #var $sourceFile ConvertedFile */
if ($sourceFile) {
$sourceFile->moveTo($absGalleryPath . $sourceFileName);
}
// save converter 2
$thumbnailFileName = getSafeFileName($absThumbnailsPath, $originalFileName);
$thumbnailFile = $files[1];
/* #var $thumbnailFile ConvertedFile */
if ($thumbnailFile) {
$thumbnailFile->moveTo($absThumbnailsPath . $thumbnailFileName);
}
//Load XML file which will keep information about files (image dimensions, description, etc).
//XML is used solely for brevity. In real-life application most likely you will use database instead.
$descriptions = new DOMDocument('1.0', 'utf-8');
$descriptions->load($absGalleryPath . 'files.xml');
//Save file info.
$xmlFile = $descriptions->createElement('file');
$xmlFile->setAttribute('name', $_POST['folder'] . '/' . $originalFileName);
$xmlFile->setAttribute('source', $sourceFileName);
$xmlFile->setAttribute('size', $uploadedFile->getSourceSize());
$xmlFile->setAttribute('originalname', $originalFileName);
$xmlFile->setAttribute('thumbnail', $thumbnailFileName);
$xmlFile->setAttribute('description', $uploadedFile->getDescription());
//Add additional fields
$xmlFile->setAttribute('username', $username);
$xmlFile->setAttribute('locationid', $locationid);
$xmlFile->setAttribute('folder', $dirName);
$descriptions->documentElement->appendChild($xmlFile);
$descriptions->save($absGalleryPath . 'files.xml');
}
$uh = new UploadHandler();
$uh->setFileUploadedCallback('onFileUploaded');
$uh->processRequest();
?>
In additon to the original script I've added code that creates a folder, with it's name based on the current 'locationid'. This is shown below.
$locationfolder = $_POST['locationid'];
$locationfolder = preg_replace('/[^a-z0-9_\-\.()\[\]{}]/i', '_', $locationfolder);
if (!is_dir($absGalleryPath . $locationfolder)) {
mkdir($absGalleryPath . $locationfolder, 0777);
}
What I like to incorporate, is a check that looks to see whether there is a folder already setup with the current 'locationid' value, if not create the folder. I'm ceratianly no expert in PHP, but I know that to check to see whether a file exists, the if(file exists....) can be used, but I just wondered whether someone could tell me please how I can implement this check for the folder name?
Many thanks
Chris
I think is_dir() is what you are looking for.
UPDATE:
The code you have:
if (!is_dir($absGalleryPath . $locationfolder)) {
mkdir($absGalleryPath . $locationfolder, 0777);
}
Does exactly what you want. It checks for the folder and if it does not exist then it creates one for you (with CHMOD 777). Don't see what your question is then...

Save 'Username' as Filename

I wonder whether someone could help me please.
I'm using Image Uploader from Aurigma, and to save the uploaded images, I've put this script together.
<?php
//This variable specifies relative path to the folder, where the gallery with uploaded files is located.
//Do not forget about the slash in the end of the folder name.
$galleryPath = 'UploadedFiles/';
require_once 'Includes/gallery_helper.php';
require_once 'ImageUploaderPHP/UploadHandler.class.php';
/**
* FileUploaded callback function
* #param $uploadedFile UploadedFile
*/
function onFileUploaded($uploadedFile) {
$packageFields = $uploadedFile->getPackage()->getPackageFields();
$userid = $packageFields["userid"];
$locationid= $packageFields["locationid"];
global $galleryPath;
$absGalleryPath = realpath($galleryPath) . DIRECTORY_SEPARATOR;
$absThumbnailsPath = $absGalleryPath . 'Thumbnails' . DIRECTORY_SEPARATOR;
if ($uploadedFile->getPackage()->getPackageIndex() == 0 && $uploadedFile->getIndex() == 0) {
initGallery($absGalleryPath, $absThumbnailsPath, FALSE);
}
$dirName = $_POST['folder'];
$dirName = preg_replace('/[^a-z0-9_\-\.()\[\]{}]/i', '_', $dirName);
if (!is_dir($absGalleryPath . $dirName)) {
mkdir($absGalleryPath . $dirName, 0777);
}
$path = rtrim($dirName, '/\\') . '/';
$originalFileName = $uploadedFile->getSourceName();
$files = $uploadedFile->getConvertedFiles();
// save converter 1
$sourceFileName = getSafeFileName($absGalleryPath, $originalFileName);
$sourceFile = $files[0];
/* #var $sourceFile ConvertedFile */
if ($sourceFile) {
$sourceFile->moveTo($absGalleryPath . $sourceFileName);
}
// save converter 2
$thumbnailFileName = getSafeFileName($absThumbnailsPath, $originalFileName);
$thumbnailFile = $files[1];
/* #var $thumbnailFile ConvertedFile */
if ($thumbnailFile) {
$thumbnailFile->moveTo($absThumbnailsPath . $thumbnailFileName);
}
//Load XML file which will keep information about files (image dimensions, description, etc).
//XML is used solely for brevity. In real-life application most likely you will use database instead.
$descriptions = new DOMDocument('1.0', 'utf-8');
$descriptions->load($absGalleryPath . 'files.xml');
//Save file info.
$xmlFile = $descriptions->createElement('file');
$xmlFile->setAttribute('name', $_POST['folder'] . '/' . $originalFileName);
$xmlFile->setAttribute('source', $sourceFileName);
$xmlFile->setAttribute('size', $uploadedFile->getSourceSize());
$xmlFile->setAttribute('originalname', $originalFileName);
$xmlFile->setAttribute('thumbnail', $thumbnailFileName);
$xmlFile->setAttribute('description', $uploadedFile->getDescription());
//Add additional fields
$xmlFile->setAttribute('userid', $userid);
$xmlFile->setAttribute('locationid', $locationid);
$xmlFile->setAttribute('folder', $dirName);
$descriptions->documentElement->appendChild($xmlFile);
$descriptions->save($absGalleryPath . 'files.xml');
}
$uh = new UploadHandler();
$uh->setFileUploadedCallback('onFileUploaded');
$uh->processRequest();
?>
What I'd like to do is replace the files element of the filename and replace it with the username, so each saved folder and associated files can be indentified to each user.
I've added a username text field to the form which this script saves from
I think I'm right in saying that this is line that needs to change $descriptions->save($absGalleryPath . 'files.xml');.
So amongst many attempts I've tried changing this to $descriptions->save($absGalleryPath . '$username.xml, $descriptions->save($absGalleryPath . $username '.xml, but none of these have worked, so I'm not quite sure what I need to change.
I just wondered whether someone could perhaps have a look at this please and let me know where I'm going wrong.
Many thanks
'$username.xml' will be interpreted as $username.xml, you need to use "$username.xml". Single quotes "disable" the variable use inside strings.
What you are tryiing can be a bad idea, as you are making so a username can't contain 'special characters' like "/". Perhaps is not a problem if you aready have a rule that stop "/" being part of a username.

Categories