How to get file array in Symfony controller? - php

I've user Symfony for the my application service. One service call send file array (multiple files) in post. how can i get that?
because when I use below code and dump $uploadedFiles is empty. when send single image, it work, any help?
public function createAction(Request $request) {
$uploadedFiles = $request->files;
}
this not about multiple file upload as Multiple file upload with Symfony2 question. This is about how to get multiple files (file array) in Request

Related

Uploading Files through Silverstripe 4 RestulServer Module

My app uses silverstripe to manage a large number of files for downloading/accessing them and some relational metadata.
One requirement is that the files be accessible externally via an API. I have set up the Restfulserver module (https://packagist.org/packages/silverstripe/restfulserver) to accomplish this.
And have extended the File model to allow restful access:
class FileExtension extends DataExtension
{
...
private static $api_access = true;
...
}
This lets me get and download a file with no problems using a GET:
silverstripe/public/api/v1/Silverstripe-Assets-File/(ID)
Which gives me the necessary data to hit the assets/{hash}/(fileName) and download the file.
But it doesn't seem to give me the means to POST a file. POSTing here simply creates the a File record but with no accompanying file in the assets folder. Manually dropping a file in the folder doesnj't work because it is not referenced by the record and does not have an associated Hash.
So how to I upload files without a page controller?

Laravel GET image as file along with other data

I have an Ionic application that communicates with my Laravel api. I'm trying to get the product details and image from the api and display that data in my Ionic app. I have the images stored on the local disk and also have a db table that contains the path name for each image. But when the Ionic app gets this data back, it just gets a json object that contains the mainImage with a path to the local disk, which of course, I can't use as an img src. My question is, how do I pass the mainImage as a file rather than a json object?
Here is my Laravel function:
public function api_get_non_disliked_for_user($categoryId)
{
$allProducts = Product::with('mainImage')
->where('subSubCategoryId', "=", $categoryId)->orderBy('title')->get();
return $allProducts;
}
In Product Model:
public function mainImage(){
return $this->hasOne('App\MainImage', 'productId');
}
In MainImage Model:
public function product(){
return $this->belongsTo('App\Product', 'productId');
}
The API should provide the url to the image, not the image file, specially when using Ionic as the front end, because then you just use the url in your html markup and the image will get downloaded automatically.

symfony form with new files via Vich Uploader

Iam trying to build a smarter upload structure for my app. until now I had the code for uploading files in the controller and everything was more manual. Now I want to use the VichUploader in Symfony, but I have problems to implement it with multiple files.
First, I dont have an Entity with a file but an Entity that holds multiple File Entities. To be more clearly: The Entity Document has a oneToMany relation to File. So I build a form with a CollectionType:
$builder->add('files', Type\CollectionType::class, [
'entry_type' => Type\FileType::class
])
But because there is not file yet when I add a new Document, no upload field is shown. And even when there are already files (on edit form), there shouldn't be upload fields shown but text fields with file names.
How can I achieve that? Do i still need to add an umapped field files_new() with a multiple FileType? Then the VichUploader automatic stuff would not work.
If you pass an empty file object you should have your html fil input as it is a collection
In your controller (I don't have your code)
$entity = new Entity()
$entity->addFile(new FileEntity())//Add an empty file object
$form = $this->createForm(YourType::class, $entity);
$form->handleRequest($request);

Laravel Dropzonejs file validation in hasmany table

I have a laravel app that uploads files. I need to check in the related table if the file exist. My user model hasmany(assets) The file info lives in the assets table. I am trying to pull the name out of the table and verify it against the file being uploaded in dropzone.
Here is my dropzone script:
Dropzone.options.dropzoneUpload = {
paramName: "asset", // The name that will be used to transfer the file
maxFilesize: 2, // MB
accept: function(file, done) {
if (file.name == "{{$filename}}") {
done("Naha, you don't.");
}
else { done(); }
}
};
My function that generates my view for my upload page looks like this
public function index()
{
$filename = Auth::user()->assets->name;
return view('upload.index')->with('filename', $filename);
}
I get an error returned on the name in the filename variable
Undefined property: Illuminate\Database\Eloquent\Collection::$name
I have no idea how to access the file name from the table and verify it with the script in the view. Each user has multiple file names in the table.
Pre dropzone i was able to use this in my controller
foreach($userAssets as $asset)
{
if($asset->name == $filename)
{
return redirect('upload')->with('errornotice', 'A file with that name already exist');
}
}
but that won't work in the script.
Since you said yourself that your User hasMany Assets, what exactly is assets->name supposed to print? The name of which Asset? Since you have many of those and user->asset is returning a Collection, you could probably iterate that collection, construct a JSON array out of all Asset names and inject it into your JS. Then do a JavaScript for/each to check against all elements of the array. That's weak validation though, any user could edit the JS before submitting. If it's important to you that the filenames do not exist already, consider some kind of server-side validation.

How to make FileUpload Object handle Multiple files in Symfony2?

I'm trying to make a form with a multiple file field. Since the docs are quite vague:
http://symfony.com/doc/current/reference/forms/types/file.html
http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html
I've decided to use W3C FileReader API (Based in the doc urls below), to handle files from the client and manage the underliying data from the view to the entity. Currently supports drag&drop, metadata, and multiple selections on the client.
http://www.html5rocks.com/en/tutorials/file/dndfiles/
http://playground.html5rocks.com/#reading_file_metadata
https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications
But I want to give the UploadedFile object one more chance, and the MAIN problem I have is that I can't make the file property in my entity (FileUpload type) to store more than one file data. My input looks like this:
<input type="file" id="upload_files" name="upload[files][]" required="required" multiple="multiple" />
In theory If I made the name to be an Array, the fileUpload should contain the files but it doesn't. Can UploadedFile object store multiple files data ? or just single ?
http://api.symfony.com/2.2/Symfony/Component/HttpFoundation/File/UploadedFile.html
Also tried to initialize (in the __construct of the entity) the $files property as an array and modify the setFiles() to store new array index's $this->files[] = $file; ... you know.
But then Symfony tells me that exception:
The form's view data is expected to be an instance of class Symfony\Component\HttpFoundation\File\File, but is a(n) array. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) array to an instance of Symfony\Component\HttpFoundation\File\File.
I'm not familiarized with data transformers. And Can't figure it out how can be done now. Or If it could be really useful to get the UploadedFile Object with every file data.
In synthesis... With this given info, and the code below. Could anyone help me to get the FileUpload object the correct number of files and not just the last added ? Thank You
Made a repo on github: https://github.com/jeflopo/fileupload
For brevity, here are the relevant files:
The form:
https://github.com/jeflopo/fileupload/blob/master/src/Acme/DemoBundle/Form/Type/FileUploadType.php
The Entity:
https://github.com/jeflopo/fileupload/blob/master/src/Acme/DemoBundle/Entity/FileUpload.php
The controller (Just see the uploadAction):
https://github.com/jeflopo/fileupload/blob/master/src/Acme/DemoBundle/Controller/DemoController.php
The View:
https://github.com/jeflopo/fileupload/blob/master/src/Acme/DemoBundle/Resources/views/Demo/upload.html.twig
The JavaScript that handles the files on the client (Doesn't affect to files behaviour in the server):
https://github.com/jeflopo/fileupload/blob/master/src/Acme/DemoBundle/Resources/public/js/upload.js
I created a pull request which fixes the error!
What's left to do now is that you create unique filenames for the uploaded files and then use the move method on each file. If you don't move them the files won't get saved!
As I set mapped to false, your entity doesn't contain the files. You have to create an array with the filenames you just created to save the file paths.

Categories