php


Symfony2 API - Best way to import and save a CSV file


I need to create an API that receives via POST a .csv file that in a second moment has to be saved to the DB.
This is how I wrote the function:
/**
* #Route("/importCSV", name = "import_csv")
* #Method("POST")
**/
public function importCSVAction(Request $request) {
if ($this->getRequest()->files) {
$type = "csv";
$filename = date('Ymd').'-'.$type.'-filename';
$directory = $this->container->getParameter('kernel.root_dir') . '/../Documents';
$request = $this->get('request');
$data = $request->getContent();
try {
$fs = new Filesystem();
$fs->dumpFile($filename, $data);
move_uploaded_file($filename, $directory);
}
catch(IOException $e) {
return new Response ("File not saved!", 500, array('Content-Type' => 'application/json'));
}
return new Response($data);
}
else return new Response("Error!", 500, array('Content-Type' => 'application/json'));
}
1 One problem is that the file is saved under the web folder and not under Documents as I wanted.
Also, how can I check if the file I'm importing is a .csv file?
2 I searched on the web but I did not found many resources on this subject.
I just want to know if this is generally a correct way to save the .csv file.
Thanks!

Related Links

What is the correct JSON format.?
PHPDOM: how to edit style in HTML element
MySQL select multiple rows within a row
tools to find inject malicious code?
Laravel Validator mime-types except certain types
How to get link MP4, FLV from Youtube
how can I use of a class property in the nested function
Allow parallel running of php script
Loop through checkbox form Laravel
Print php array in python
How to convert tag cloud from mysql to mysqli [closed]
mysql conditional “not_in” based on another value to parse array into query
search feature php and mysql view result in current page
How to create drag drop navigation menu manager like wordpress?
convert string array to integer array in php
PHP form loads blank page and nothing happens?

Categories

HOME
firebase
vagrant
clearcase
angular-ui-grid
eclipse-plugin
adfs3.0
opencv-python
barcode-scanner
applescript
writefile
add-on
foaf
odata
apk
iverilog
informatica
azure-mobile-services
google-search-console
save
amazon-emr
buffer
django-rq
closures
data-type-conversion
workday
maven-surefire-plugin
opentext
jstree
macromedia
progid
actframework
sca
cloveretl
xerces-c
numerics
jslint
text-decorations
aws-kinesis-firehose
libreoffice-writer
sony-future-lab-n
winston
iptv
emf-compare
annotatorjs
e
xmlbeans
logical
bilinear-interpolation
nsrunloop
visual-studio-monaco
oci
deferred-rendering
polymerfire
gridbaglayout
gravatar
preferenceactivity
android-expansion-files
replicaset
swagger-maven-plugin
deque
subfolder
nssortdescriptor
directorysearcher
comctl32
jquery-mobile-flipswitch
visual-c++-2012
cpu-time
application-blocks
revmob
sql-view
tnt4j
quickgraph
apportable
statechart
days
chefspec
examine
cyrillic
onconfigurationchanged
story
playorm
arrow
getopt-long
viewpagerindicator
background-repeat
nsrangeexception
upshot
eaccelerator
ninject-interception
rawcontacts
objectinstantiation
remote-working

Resources

Encrypt Message