Symfony2: how to configure path for saving file - symfony

how can I setup a file path in a class for saving a file?
I already tried a relative path but this change if I call the class from a controller or from a command container...
thanks

In controller class:
...
$path = $this->get("kernel")->getRootDir();
...
$path variable will be path to app directory of Symfony project.
...
$path = $this->get("kernel")->getRootDir() . PATH_SEPARATOR . ".." . PATH_SEPARATOR;
$path variable will be path do root directory of project.
From command class:
...
$path = $this->getContainer()->get("kernel")->getRootDir();
...

Related

PhpUnit : Error: Unable to access jarfile ./../src/Java/File.jar

When I run the test of my page, I encounter this error message that I do not have on the browser:
Error: Unable to access jarfile ./../src/Java/File.jar
Here is the call to the file in my controller (symfony 4):
exec("java -jar ./../src/Java/File.jar $params",$tab);
Here is my test method :
public function testView(){
$crawler = $this->client->request('GET','/test/1');
if( Response::HTTP_OK !== $this->client->getResponse()->getStatusCode()){
echo $crawler->filter('div.exception-message-wrapper')->text();
echo $crawler->filter('.trace-html-1')->text();
}
static::assertEquals(
Response::HTTP_OK,
$this->client->getResponse()->getStatusCode()
);
}
thanks for your help
In your controller you could try using a path to the jar which will be relative to the __DIR__.
exec("java -jar " . __DIR__ . "../src/Java/File.jar $params", $tab);
To understand, you can read more about __DIR__ here

ZipArchive::close(): Renaming temporary file failed: Invalid argument Phpexcel Laravel

I am working on append excel content into existing excel file and save it in a predefined directory with different name. I am using the Laravel 5.4 framework for my application.
I am using the below code to create excel file and save it on specified location:
$objPHPExcel = \PHPExcel_IOFactory::createReader('Excel2007');
//Load Existing excel file into I want to append
$objPHPExcel = $objPHPExcel->load(getDocumentPath().'/Report_Template_UG.xlsx');
$objPHPExcel->setActiveSheetIndex(0);
//Here I have Written code to insert my data into loaded excel file
$today = date('d-m-Y');
$time = date('H:i:s');
$filename = 'Report_'.$today.' '.$time.'.xlsx';
//welcomed is the root directory name of laravel project
$dir = 'C:\xampp\htdocs\welcomed\storage\documents\';
$filepath = $dir . $filename;
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save($path);
On executing the above code, I am gettign the below error:
ErrorException in Excel2007.php line 377:
ZipArchive::close(): Renaming temporary file failed: Invalid argument
Can anyone please tell me, What I have done wrong in above code or do I have missed something?
Thanks

symfony2 twig FileException: Unable to create the "/voice_mails" directory

$r = $app['request'];
foreach ($r->files as $uploadedFile) {
$name = basename($_FILES["wavFile"]["name"]);
$file = $uploadedFile->move('/voice_mails', $name);
print_r($file);
}
I am trying to upload file using above code , But throwing error like:
FileException: Unable to create the "/voice_mails" directory
Please Help me.
console
mkdir your_path/voice_mails
chmod -R 777 your_path/voice_mails
chown -Rf apache:apache your_path/voice_mails
And check fisical path /var/www/yourproject or use dirname()
FileException is thrown because you're trying to create voice_mails directory in root folder /. You want to create in in your web root folder, so you code should be like:
foreach ($r->files as $uploadedFile) {
//…
$file = $uploadedFile->move($this->get('kernel')->getRootDir() . '/../web/voice_mails', $name);
//…
}

Batch file to find a file, create a copy in the same location and run this on multiple directories

here's what I am trying to do.
I have a few hundred users My Documents folders in which most(not all) have a file(key.shk for shortkeys program).
I need to upgrade the software but doing so makes changes to the original file.
I would like to run a batch file on the server to find the files in each My Docs folder and make a copy of it there called backup.shk
I can then use this for roll back.
The folder structure looks like this
userA\mydocs
userB\mydocs
userC\mydocs
My tools are xcopy, robocopy or powershell
Thanks in advance
This powershell script works... save as .ps1
Function GET-SPLITFILENAME ($FullPathName) {
$PIECES=$FullPathName.split(“\”)
$NUMBEROFPIECES=$PIECES.Count
$FILENAME=$PIECES[$NumberOfPieces-1]
$DIRECTORYPATH=$FullPathName.Trim($FILENAME)
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($_.fullname)
$FILENAME = [System.IO.Path]::GetFileNameWithoutExtension($_.fullname)
return $FILENAME, $DIRECTORYPATH
}
$Directory = "\\PSFS03\MyDocs$\Abbojo\Insight Software"
Get-ChildItem $Directory -Recurse | where{$_.extension -eq ".txt"} | % {
$details = GET-SPLITFILENAME($_.fullname)
$name = $details[0]
$path = $details[1]
copy $_.fullname $path$name"_backup".txt
}

Symfony 2.2.1 rsync deploy - not working on remote server

I'm very new to Symfony and I'm trying to automate the deploy process with rsync, while keeping both the local and remote installs of Symfony working.
What I've done so far:
installed Cygwin on my local machine (Windows 7+Apache2.2+PHP 5.3+MySQL 5.1)
done a basic Symfony install on my local machine from shell with the command
php composer.phar create-project symfony/framework-standard-edition [path]/ 2.2.1
set up a remote LAMP Ubuntu server with php-fpm (fastcgi)
set up two different configuration files for local and remote in the app/config/ dir, parameters.yml and parameters.yml.remote
created an app/config/rsync_exclude.txt file containing a list of files not to rsync to the remote server (as suggested in this page)
created a deploy shell script that I run from Cygwin (see below)
The deploy script issues the commands:
rsync -avz /cygdrive/c/[path]/ user#server:[remote-path]/ --exclude-from=/cygdrive/c/[path]/app/config/rsync_exclude.txt
ssh user#server 'cd [remote-path]/ && php app/console --env=prod cache:clear && php app/console cache:clear'
ssh user#server 'mv [remote-path]/app/config/parameters.yml.remote ~/[remote-path]/app/config/parameters.yml'
The rsync, ssh and mv commands work, but the deployed site shows always a HTTP 500 error (both app.php and app_dev.php).
Looking at server error log the error is:
Fatal error: Class 'Composer\\Autoload\\ClassLoader' not found in /[remote-path]/vendor/composer/autoload_real.php on line 23
Any clue would be more than welcome.
Edit - here is my vendor/composer/autoload_real.php file (sorry for the making the question longer!):
<?php
// autoload_real.php generated by Composer
class ComposerAutoloaderInit9d50f07556e53717271b583e52c7de25
{
private static $loader;
public static function loadClassLoader($class)
{
if ('Composer\Autoload\ClassLoader' === $class) {
require __DIR__ . '/ClassLoader.php';
}
}
public static function getLoader()
{
if (null !== self::$loader) {
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit9d50f07556e53717271b583e52c7de25', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
// ^^^^^^ this is line 23 and gives the error ^^^^^^^^^^^
spl_autoload_unregister(array('ComposerAutoloaderInit9d50f07556e53717271b583e52c7de25', 'loadClassLoader'));
$vendorDir = dirname(__DIR__);
$baseDir = dirname($vendorDir);
$map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
$loader->add($namespace, $path);
}
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
$loader->addClassMap($classMap);
}
$loader->register(true);
require $vendorDir . '/kriswallsmith/assetic/src/functions.php';
require $vendorDir . '/swiftmailer/swiftmailer/lib/swift_required.php';
return $loader;
}
}
If there is an error with the autoloader generated by composer, performing ...
composer update
... will update your dependencies and create a new one.
You should invoke the command with the -o flag if you are deploying to a production system.
This way composer generates a classmap autoloader ( which performs way better ) instead of the classic autoloader.
composer update -o
I guess re-generating the autoloader will solve the issue :)

Resources