I have laravel project and whant to add feature for ziping files. I am using php ZipArchive. When I'm trying to create ZIP file using just PHP, I have luck, but when I'm trying with Laravel, zip files does not been created.
So I have add: use ZipArchive;
And just doing:
$file_path = storage_path("creatives/helloworld.zip");
$zip = new ZipArchive();
$zip->open($file_path, ZipArchive::CREATE);
But there is not error and no zip file. What can you advise me?
It is very late to reply but this may help someone. I too had this same issue but then I opted for using "Process Component" and execute the command to create zip.
If you are only concerned with the zip creation. You can use the following code.
<?php
$projectFolder = $destinationPath;
$zipFile = $nameOftheFile;
$process = new Process("zip -r $zipFile $projectFolder");
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
// throw new ProcessFailedException($process);
$response['msg'] = $process->getOutput();
}else{
$response['msg'] = 'Build zipped';
}
return $response;
Don't forget to install zip extension. Use the below command.
sudo apt-get install zip
Related
Wassup Guys,
I've managed to install the Google Cloud Speech Api and use my .json file as credentials for the speech client.
Now I'm getting the "Streaming calls are not supported while using the REST transport."
error.
Did I miss something? Below is the method of my controller.
AudioController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
use Google\Cloud\Speech\V1\RecognitionConfig;
use Google\Cloud\Speech\V1\StreamingRecognitionConfig;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\Response;
use Google\Cloud\Speech\V1\SpeechClient;
class AudioController extends Controller
{
public function getData(Request $request)
{
//$url = Storage::download('public/track.mp3');
$url = asset('storage/test.flac');
//$file = Storage::disk('public')->get('track.mp3');
//$download = new Response($file, 200);
// dd($request->all(), $url);
return view('welcome')->with('downloadLink', $url);
}
public function transcribedText(Request $request)
{
$recognitionConfig = new RecognitionConfig();
$recognitionConfig->setEncoding(AudioEncoding::FLAC);
$recognitionConfig->setSampleRateHertz(44100);
$recognitionConfig->setLanguageCode('en-US');
$config = new StreamingRecognitionConfig();
$config->setConfig($recognitionConfig);
$auth = Storage::disk('public')->get('auth.json');
$test = Storage::path('public/auth.json');
//dd(file_get_contents($test, true));
//dd(json_decode($auth));
//putenv('GOOGLE_APPLICATION_CREDENTIALS='.$auth);
$speechClient = new SpeechClient([
'credentials' => Storage::path('public/auth.json'),
]);
$file = Storage::disk('public')->get('test.flac');
// $audioResource = fopen('path/to/audio.flac', 'r');
$responses = $speechClient->recognizeAudioStream($config, $file);
foreach ($responses as $element) {
dd($element);
}
}
}
Make sure gRPC for PHP is installed and enabled.
Check your php.ini file for:
extension=grpc.so - Linux.
extension=php_grpc.dll - Windows.
Add the composer dependency:
composer require "grpc/grpc:^1.38"
Install gRPC for PHP
For anyone encountering the same issue:
Laravel Sail uses a regular Dockerfile under Vendor/Sail folder and and also a php.ini in the same drirectory, that gets copied after building.
Just add this for phar:
&& apt-get install autoconf zlib1g-dev php-pear \
RUN curl -O https://pear.php.net/go-pear.phar
RUN php go-pear.phar
RUN pecl install grpc -q
And this to the php.ini
extension=grpc.so
I've installed mjml cli using the following command (as described in the mjml documentation):
npm install mjml --save
now if i did node_modules/.bin/mjml in the command line it will run successfully.
the problem is when i use the symfony process component in php i got the following error (even if it's the right path):
The command "/Users/qoraiche/Documents/my-app/node_modules/.bin/mjml" failed. Exit Code: 127(Command not found) Working directory: /Users/qoraicheOS/Documents/my-app/public Output: ================ Error Output: ================ env: node: No such file or directory
Symfony process code:
$process = new Process(base_path('node_modules/.bin/mjml'));
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
By the way i have installed mjml globally as well and try it with no luck.
Try providing the full path to node.js when calling the MJML binary from a Symfony Process().
$process = new Process('/your/path/to/node ' . base_path('node_modules/.bin/mjml'));
I use this method in my own apps. Of course, you will need to provide the input and output files to actually transpile anything.
I have some script: /home/user/project/deploy.sh
I trying to run with Symfony/Process component from php handler in another directory:
$process = new Process(['./deploy.sh']);
$process->setWorkingDirectory('/home/user/project');
$process->run(function ($type, $buffer) {
echo $buffer;
});
But I got an error:
The provided cwd "/home/user/project" does not exist.
This folder exists and symfony handler ran as correct user which have correct permissions to this folder. What is the correct way to run this script from another folder?
Just for debugging purposes test code below.
$process = new Process(['/home/user/project/bin/console', '--version']);
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
If it does work then try:
new Process(['/home/user/project/deploy.sh']);
new Process(['sh /home/user/project/deploy.sh']);
Process::fromShellCommandline('/home/user/project/deploy.sh');
You may need to use the full path to sh since the webserver uses a different user and a PATH variable.
I would like to extract .tar.gz file into the particular folder. I have used cURL to download the .tar.gz file from MailChimp batch operation. I have used below code to extract tar file.
$phar = new \PharData('upload/test.tar.gz');
$phar->extractTo('upload/',null, true);
It is working on windows environment. But on Linux(Ubuntu), I got below error when to run above code.
Uncaught exception 'PharException' with message 'Extraction from phar "upload/test.tar" failed: Cannot extract ".", internal error'
I have already set default apache user and group permission for upload folder.
sudo chown -R www-data:www-data upload/
PHP Version: 5.6.24
I have attach sample .tar.gz file: http://www.simbanic.com/_projects/test/05c902076e.tar.gz
I had the same problem with MailChimp batch response archive.
Stupid solution, but it works...
$p = new PharData($tarGzArchive, RecursiveDirectoryIterator::SKIP_DOTS);
$p->convertToData(Phar::ZIP);
$zip = new ZipArchive;
$res = $zip->open($createdZipArchive);
if ($res === TRUE) {
$zip->extractTo($destinationPath);
$zip->close();
}
SOLVED, read bottom of post:
I'm trying to install the Sparks package manager on windows by following the official instructions.
Issuing this command:
php -r "$(curl -fsSL http://getsparks.org/go-sparks)"
results in this errormessage:
Parse error: syntax error, unexpected ':' in Command line code on line 1
If I only execute the curl command within the above line, i.e this:
curl -fsSL http://getsparks.org/go-sparks
it echoes out the php script located on the URL. So I think the problem is piping the curl output to PHP somehow fails. I've tried a couple of variants, using diffrent quotes etc but I'm at a loss.
MY SOLUTION
As DaveRandom pointed out, the instruction didn't apply to windows.
But instead of doing it the manual(normal) way, what I did was taking the output from curl, appending php script tags and executing it as a file with the php -f option.
Here is the output:
<?php
$zip = "http://getsparks.org/static/install/spark-manager-0.0.7.zip";
$loader = "http://getsparks.org/static/install/MY_Loader.php.txt";
if(!file_exists("application/core"))
{
echo "Can't find application/core. Currently this script only works with the default instance of Reactor. You may need to try a manual installation..\n";
exit;
}
echo "Pulling down spark manager from $zip ...\n";
copy($zip, "sparks.zip");
echo "Pulling down Loader class core extension from $loader ...\n";
copy($loader, "application/core/MY_Loader.php");
echo "Extracting zip package ...\n";
if(class_exists('ZipArchive'))
{
$zip = new ZipArchive;
if ($zip->open('sparks.zip') === TRUE)
{
$zip->extractTo('./tools');
$zip->close();
} else {
echo "Extraction failed .. exiting.\n";
exit;
}
} elseif(!!#`unzip`) {
`unzip sparks.zip -d ./tools`;
} else
{
echo "It seems you have no PHP zip library or `unzip` in your path. Use the manual installation.\n";
exit;
}
echo "Cleaning up ...\n";
#unlink('sparks.zip');
echo "Spark Manager has been installed successfully!\n";
echo "Try: `php tools/spark help`\n";
The instructions you linked do explicitly state;
In order to use this quick start option, you should be using OSX or some flavor of linux.
You need to follow the Normal Installation instructions for use on Windows.
You should execute the command php -r "$(curl -fsSL http://getsparks.org/go-sparks)" in your root application folder. Maybe you're executing this command at wrong folder.