define ffmpeg binaries path dynamically in php depending on OS - php

I'm trying to define the ffmpeg binaries path dynamically in php depending on OS...
Here's my code:
// Check if OS is Windows
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$OS = 'win';
// 'This is a server using Windows!';
}
require ABSPATH . '/vendor/autoload.php';
if($OS == 'win')
{
$ffmpegpath = ABSPATH . 'FFMpeg/bin/ffmpeg.exe';
$ffprobepath = ABSPATH . 'FFMpeg/bin/ffprobe.exe';
}
$ffmpeg = FFMpeg\FFMpeg::create(array(
'ffmpeg.binaries' => $ffmpegpath,
'ffprobe.binaries' => $ffprobepath,
'timeout' => 3600, // The timeout for the underlying process
'ffmpeg.threads' => 12, // The number of threads that FFMpeg should use
));
For some reason it doesn't work.
If I hard code the same paths within the ffmpeg construct function it does work.
Any ideas?
Thanks.

Nevermind, I found the problem, it was a pesky little GLOBAL var bug in my code.
Apologies for the question, I can't delete it.
Thanks.

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).

Getting error on server due to anonymous functions issue in PHP 5.2

I'm using "google-api-php-client" library which is working fine on local system but it's giving following error on server as it's version is 5.2!
syntax error, unexpected T_FUNCTION, expecting ')'
So I have two questions here, if we can fix this error by doing some changes in code to make it work with this function? Below is the code of autoload.php
spl_autoload_register(
function ($className) {
$classPath = explode('_', $className);
if ($classPath[0] != 'Google') {
return;
}
// Drop 'Google', and maximum class file path depth in this project is 3.
$classPath = array_slice($classPath, 1, 2);
$filePath = dirname(__FILE__) . '/' . implode('/', $classPath) . '.php';
if (file_exists($filePath)) {
require_once($filePath);
}
}
);
but I'm not sure how to change the above to solve this issue and also is there any library which can run on php version 5.2? As if I use this, it might be possible that it start giving error on some other functionality. Thanks!
It seems your php version not knows about anonymous functions or closures. Try to use named one:
function autoloadGoogleApi($className) {
$classPath = explode('_', $className);
if ($classPath[0] != 'Google') {
return;
}
// Drop 'Google', and maximum class file path depth in this project is 3.
$classPath = array_slice($classPath, 1, 2);
$filePath = dirname(__FILE__) . '/' . implode('/', $classPath) . '.php';
if (file_exists($filePath)) {
require_once($filePath);
}
}
spl_autoload_register('autoloadGoogleApi');
Still, I'm also want to point out, that php version you specifying is very old, so I'm suggesting to really consider option of upgrading.
UPD: 3v4l test

Downloading a 2MB File in PHP

I'm writing a function that updates a database file every 30 days. It works great for small files, but for files over 200K or so, it just downloads a partial file. How can I make this work with files up to 2MB?
function lsmi_geoip_update() {
$dir = dirname( __FILE__ );
$localfilev4 = $dir . '/data/GeoIPv4.dat';
$localfilev6 = $dir . '/data/GeoIPv6.dat';
if ( file_exists( $localfilev4 ) ) {
rename($dir . '/data/GeoIPv4.dat', $dir . '/data/OLD_GeoIPv4.dat');
$newfilev4 = file_get_contents('http://gdriv.es/geoipupdate/GeoIPv4.dat');
file_put_contents($dir . '/data/GeoIPv4.dat', $newfilev4);
// unlink($dir . '/data/OLD_GeoIPv4.dat');
}
if ( file_exists( $localfilev6 ) ) {
rename($dir . '/data/GeoIPv6.dat', $dir . '/data/OLD_GeoIPv6.dat');
$newfilev6 = file_get_contents('http://gdriv.es/geoipupdate/GeoIPv6.dat');
file_put_contents($dir . '/data/GeoIPv6.dat', $newfilev6);
// unlink($dir . '/data/OLD_GeoIPv6.dat');
}
}
Here's the output:
you must change the setting in the php.ini upload_max_filesize = 2M
and max_execution_time = X according to your need.
Edit php.ini to allow bigger upload first. Add (or find & edit) upload_max_filesize = 2M to your php.ini.
If that doesn't work, consider file_get_contents (= downloading) failing in the first place.
Your connection could be timing out before it can fetch the whole data. Try this.
(Sorry I can't comment everywhere yet that I had to answer on guessing)
$ctx = stream_context_create(array(
'http' => array(
'timeout' => 120
)
)
);
file_get_contents("http://gdriv.es/geoipupdate/GeoIPv4.dat", 0, $ctx);
Hosting the files on a different server fixed it.

Smarty Compile path returns wrong result due to DIRECTORY_SEPARATOR in windows

Hi I have moved my smarty project from my linux machine to windows machine , Now i am getting a wrong result for the compile path
C:/xampp/htdocs/smarty/templates_c\%%45^45E^45E480CD%%index.tpl.php
I checked it and found an issue in ths part
templates_c\%%45^45E^45E480CD%%index.tpl.php
the directy sperator is \so my path is failing .
I checked in this function in smarty
function _get_auto_filename($auto_base, $auto_source = null, $auto_id = null)
{
$_compile_dir_sep = $this->use_sub_dirs ? DIRECTORY_SEPARATOR : '^';
$_return = $auto_base . DIRECTORY_SEPARATOR;
DIRECTORY_SEPARATOR is returning \ .
Is there any palce to change my DIRECTORY_SEPARATOR globally to / or is there any other solution for this . :)
Thank you in advance .

Get folder up one level

I am using this:
echo dirname(__FILE__);
which gives:
C:\UwAmp\www\myfolder\admin
However I am looking for path until:
C:\UwAmp\www\myfolder\
from current script. How can that be done ?
You could do either:
dirname(__DIR__);
Or:
__DIR__ . '/..';
...but in a web server environment you will probably find that you are already working from current file's working directory, so you can probably just use:
'../'
...to reference the directory above. You can replace __DIR__ with dirname(__FILE__) before PHP 5.3.0.
You should also be aware what __DIR__ and __FILE__ refers to:
The full path and filename of the file. If used inside an include, the name of the included file is returned.
So it may not always point to where you want it to.
You can try
echo realpath(__DIR__ . DIRECTORY_SEPARATOR . '..');
echo dirname(__DIR__);
But note the __DIR__ constant was added in PHP 5.3.0.
Also you can use
dirname(__DIR__, $level)
for access any folding level without traversing
The parent directory of an included file would be
dirname(getcwd())
e.g. the file is
/var/www/html/folder/inc/file.inc.php
which is included in
/var/www/html/folder/index.php
then by calling /file/index.php
getcwd() is /var/www/html/folder
__DIR__ is /var/www/html/folder/inc
so dirname(__DIR__) is /var/www/html/folder
but what we want is /var/www/html which is dirname(getcwd())
To Whom, deailing with share hosting environment and still chance to have Current PHP less than 7.0 Who does not have dirname( __FILE__, 2 ); it is possible to use following.
function dirname_safe($path, $level = 0){
$dir = explode(DIRECTORY_SEPARATOR, $path);
$level = $level * -1;
if($level == 0) $level = count($dir);
array_splice($dir, $level);
return implode($dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
}
print_r(dirname_safe(__DIR__, 2));
Use $_SERVER['DOCUMENT_ROOT']
For example if you want to access a folder above the root directory from anywhere
$_SERVER['DOCUMENT_ROOT']."/../myfolder/spl-auto.php"
or for your example C:\UwAmp\www\myfolder\admin
$admin = $_SERVER['DOCUMENT_ROOT']."/myfolder/admin/"
$myfolder = $_SERVER['DOCUMENT_ROOT']."/myfolder/"

Categories