I want to parse an ini file placed under the vendor directory.
How can I get the path to this file?
For Example:
In src/Acme/TestBundle/Repository/UserRepository.php I have
$file = parse_ini_file(FILE_PATH);
How do I set FILE_PATH to reach vendor/test/lib/conf.ini?
The other solution is correct but it can be even shorter (plus this should be the way to go in the current symfony version):
public function setup()
{
$rootDir = $this->container->getParameter('kernel.root_dir');
$filePath = $rootDir.'/../vendor/test/lib/conf.ini';
$file = parse_ini_file($filePath);
...
}
You didn't provided info what kind of test do you use, but you need to get the container and get kernel.root_dir from there
For WebTestCase:
public function setUp()
{
$kernel = static::createKernel();
$kernel->boot();
$rootDirectory = $kernel->getContainer()->getParameter('kernel.root_dir');
$filePath = $rootDirectory.'/../vendor/test/lib/conf.ini;
...
}
Related
I use WebP Express plugin (https://wordpress.org/plugins/webp-express/).
Coould you tell me how to get a path to webp image.
I mean an analogue to this:
wp_get_attachment_image_src($attachment_id = 1, $size = 'medium_large');
And the result should be something like
http://galina/wp-content/webp-express/webp-images/uploads/2019/12/sports-4-768x512.jpg.webp
I wrote a class in order to fix this.
use \WebPExpress\AlterHtmlHelper;
class WebpExpressExtended
{
public static function background($imgId, $imgSize = 'full')
{
$img = wp_get_attachment_image_src($imgId, $imgSize)[0];
if (!$imgId || !$img) {
return false;
}
return AlterHtmlHelper::getWebPUrl($img, null);
}
}
Paste it on functions.php or in some file required there.
To use it just call the static method background($imageID, $imageSize);
echo WebpExpressExtended::background(309, 'thumbnail');
im trying to upload diffrent types of files in symfony
$uploadedFile = $request->files->get('image');
Works good for handling with images, However i cannot use it with diffrent files than
$uploadedFile = $request->files->get('file');
dd($uploadedFile);
Whatever i send using this, dd method shows me null.
How can I upload files for example pdfs, docx etc. (diffrent than images)
I use vue on the frontend.
You should use createForm.
https://symfony.com/doc/current/controller/upload_file.html
$form = $this->createForm(ProductType::class, $product);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** #var UploadedFile $brochureFile */
$brochureFile = $form->get('brochure')->getData();
// this condition is needed because the 'brochure' field is not required
// so the PDF file must be processed only when a file is uploaded
if ($brochureFile) {
$originalFilename = pathinfo($brochureFile->getClientOriginalName(), PATHINFO_FILENAME);
// this is needed to safely include the file name as part of the URL
$safeFilename = transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()', $originalFilename);
$newFilename = $safeFilename.'-'.uniqid().'.'.$brochureFile->guessExtension();
// Move the file to the directory where brochures are stored
try {
$brochureFile->move(
$this->getParameter('brochures_directory'),
$newFilename
);
} catch (FileException $e) {
// ... handle exception if something happens during file upload
}
// updates the 'brochureFilename' property to store the PDF file name
// instead of its contents
$product->setBrochureFilename($newFilename);
}
It's evidently a little more complicated to create a file dynamically in SS4
$folder = Folder::find_or_make('Cards');
$filename = 'myimage.jpg';
$contents = file_get_contents('http://example.com/image.jpg');
$pathToFile = Controller::join_links(Director::baseFolder(), ASSETS_DIR, $folder->Title, $filename);
file_put_contents($pathToFile, $contents);
$image = Image::create();
$image->ParentID = $folder->ID;
$image->Title = "My title";
$image->Name = $filename;
$image->FileFilename = 'Cards/' . $filename;
$image->write();
Member::actAs(Member::get()->first(), function() use ($image, $folder) {
if (!$image->isPublished()) {
$image->publishFile();
$image->publishSingle();
}
if (!$folder->isPublished()) {
$folder->publishSingle();
}
});
The above, creates the file as expected in /assets/Cards/myimage.jpg and publishes it fine
However all previews are blank, so it's obviously not finding the file:
Any idea what I missed in creating the Image object?
This should work:
$folder = Folder::find_or_make('Cards');
$contents = file_get_contents('http://example.com/image.jpg');
$img = Image::create();
$img->setFromString($contents, 'image.jpg');
$img->ParentID = $parent->ID;
$img->write();
// This is needed to build the thumbnails
\SilverStripe\AssetAdmin\Controller\AssetAdmin::create()->generateThumbnails($img);
$img->publishSingle();
FYI: $img->Filename no longer exists. An Image or File object have a File property, which is a composite field of type DBFile. This composite fields contains the filename, hash and a variant…
So you should use the composite field to address these fields.
I try to attach a file object to a mail object.
I have included the following in the form of my view:
$f = new Concrete\Core\Application\Service\FileManager();
//...
echo $f->file('file', 'test', 'pls choose');
Then I submit my form back to the controller. There (BTW all other form fields arrive in the controller as expected) I do:
$files = $this->post('test');
$file = \File::getByID($files);
which should return a File object. When I do
$file = \File::getRelativePathFromID($files);
it gives me the correct path to the chosen file.
So far so good. BUT when I try to send a mail with exactly that file object attached:
$mail = Loader::helper('mail');
$mail->setTesting(false);
$mail->setSubject('test-subject');
$mail->to($this->post('uEmail'));
//...
$attach = $mail->addAttachment($file);
$attach->filename = 'tttt';
$mail->sendMail();
the following error occurs:
call_user_func_array() expects parameter 1 to be a valid callback,
class 'Concrete\Core\File\Version' does not have a method 'getPath'
which apparently comes from this class method (API):
namespace Concrete\Core\Mail;
//...
class Service {
//...
/**
* Add attachment to send with an email.
*
* Sample Code:
* $attachment = $mailHelper->addAttachment($fileObject);
* $attachment->filename = "CustomFilename";
* $mailHelper->send();
*
* #param File $fob File to attach
* #return StdClass Pointer to the attachment
*/
public function addAttachment(\Concrete\Core\File\File $fob)
{
// #TODO make this work with the File Storage Locations
$fv = $fob->getVersion();
$path = $fob->getPath();
$name = $fv->getFileName();
//...
}
//...
}
which apparently wants a file object as param, which I think I passed, weren't I?
Why my file object becomes a FileVersion object, which, as I see by myself, hasn't got a method getPath().
My other trials so far:
$fv = $f->getApprovedVersion();
$fv->setFile($f);
$fv->getFile();
$fv = $f->getRecentVersion();
$fv->setFile($f);
$fv->getFile();
How do I get the correct file object, which I have to, maybe (??) , take out of the last/approved Version of this file?
This was a bug that has been fixed in the upstream, you'll have to either patch this yourself or wait until version 7.4 lands.
the question is how can i create new folder in my filesystem ?
I know how to add file, but how to create an empty folder in specified path ?
I'm using the Amazon S3 adaptor, and am able to create a directory using the following:
use Gaufrette\Filesystem;
use Gaufrette\Adapter\AwsS3;
use Aws\S3\S3Client;
$s3Service = S3Client::factory(array("key" => "Your Key Here", "secret" => "Your AWS Secret Here" ));
$adapter = new AwsS3($s3Service,"yourBucketNameHere");
$filesystem = new Filesystem($adapter);
$filesystem->write("new-directory-here/", "");
Then you call write apapter ensure that directory exist. For example Ftp
public function write($key, $content)
{
$this->ensureDirectoryExists($this->directory, $this->create);
$path = $this->computePath($key);
$directory = dirname($path);
$this->ensureDirectoryExists($directory, true);
...
}
/**
* Ensures the specified directory exists. If it does not, and the create
* parameter is set to TRUE, it tries to create it
*/
protected function ensureDirectoryExists($directory, $create = false)
{
...
}
Simply pass true to the Local adapter:
use Gaufrette\Adapter\Local as LocalAdapter;
...
$adapter = new LocalAdapter(__DIR__ . '/your-new-dir', true);
$filesystem = new Filesystem($adapter);