Is it possible to upload a file in Symfony2 without referring to entity's or using Doctrine? - symfony

I'm building a web application that displays specific data to the customers of my client. The client wants to populate the application with data via a CSV file. So he needs to be able to upload the file to the server, and the application places the CSV data in to the database.
I am using Ddeboer/DataImport bundle and have managed to get it to work with a CSV file already placed on the server. The trick now is to get the CSV file on to the server in the first place.
Because I want the file to be directly sent to the server, and that it won't be associated to any other records held on the database, I feel there is absolutely no need to use Doctrine at all. However, the documentation that I've encountered suggest that you should only upload files to the server via Doctrine/Database. Surely this can't be the case?
Is there a simple way of just uploading the file to a designated folder on the server? No bells or whistles, just a pure file upload in Symfony2.

Of course it is. The cookbook only contains a doctrine based entry but it has a link to the file form type.
http://symfony.com/doc/current/reference/forms/types/file.html

Related

ASP.Net web application cannot read a file within folder

In my asp.net web application, I read the xml file for obtaining a key. If file is not present I show a form to enter the key details and then create the file.
First problem: My app does not recognize the file even if its there.
Second problem: I am running application on the server. When writing, rather overwriting the file, browser shows the username, password prompt before writing the file. If I enter admin credentials it allows to create a file.
I have checked all possible combinations of permissions on the file / folders, but could not resolve the problem.
Any ideas, what I could be missing here?
You read the xml file but is it as a part of your solution? If yes, are you reading it through relative path i.e. are you using Server.MapPath to read it like Server.MapPath("~/Files.test.xml")? Once you use relative path, I don't think it will ask you credentials as it still is in your project directory.
It should work. I am also reading and writing files in my web application.
If it still does not work, please tell me the way you are reading file.
Thanks,

In order to create an access mdb file to download do you have to have a phsyical location for the db?

I would like to create an mdb access file and populate it all in memory and spit it back out to the UI for users to download locally, but I can't see anyway around first having to specify a physical location for the mdb when trying to create it. Is it possible? I am able to create recordsets in memory but I can't seem to create an mdb access file from that record. Maybe converting to XML first?
You will have to save it somewhere as a physical file.
If it only contains data, you can - of course - also export these to any non-native file format you will be able to let your users import.

read wordpress user data into a local ms access database

I'm a MS Access developer who has a client with a wordpress website. I need a way of reading any new users who are added into the wordpress site into a microsoft access database that resides on the users local machine.
I only need to add any new records and don't need to write anything back to the site.
What would be the easiest way of doing something like this? I would sooner do it from the Access side as I'm not very familiar with wordpress, also I would prefer it to be automated so the user doesn't have to manually export from wordpress then import the data every time.
Any advice appreciated.
Thanks
Justin
There's no way to have WordPress connect to an Access DB because Access isn't persistent, it's on demand. The only way I can imagine handling it would depend on Access more than anything. Assuming Access can using the ODBC connectors installed on the windows system it's running on, you could do the following:
Setup the MySQL ODBC Connector on any systems which would run the Access DB you're setting up. I assume the WordPress install is using MySQL.
Setup a new ODBC connection using that connector within Access to the Wordpress
database (you can use the same connection information stored in the
WordPress root directory in a file called wp_config.php).
If you're connecting successfully, you can then read the wp_users table using properly formatted SQL commands (select * from wp_users, for instance).
The function within Access that manages this can either be scheduled to do it periodically while Access is open or just when Access is initialized. Depending on your needs.
You'll have to compare it to a local table of users to find differences if you're interested in all changes, though the user_login is static through normal channels so it's a good key. There's also a "user_registered" date/time column in wp_users so you could just look for users who registered since your last update/change to the local access table.
I'm not familiar with Access beyond a cursory understanding of it as a data source and some minor development functions, so there may be a much easier way to do it, but this is how I'd do it in any system that needed the user information from Wordpress.
Because Wordpress and Ms-Access live in very different environments, a trick is needed to "marry" the two. Another way we can use:
Use IIS as a webserver, so as not to collide with the port used by wordpress, so don't use port 80. Just use any port.
Use ASP to access Access Database (of course there is a block of ASP code to activate the connector to the Access file, then create a recordset via that connector for record2 and display it to the browser). Call it with the file name: recordshower.asp
The recordshower.asp file must be able to be called by the browser.
After that, go back to a page in wordpress, insert <iframe src=recordshower.asp width:.... ></iframe>
So, a wordpress page can display the contents of an Access table, with the help of ASP
I export the WordPress entries to a .CSV, I then run an Excel macro, which is stored in my personal workbook, to open the file from my downloads directory, convert the .CSV to .XLS, and write it to an XLS file in a known location. I then push a button in my MS Access program to read the entries from the .XLS file (which is externally linked to my MS Access as a Excel spreadsheet) and update my MS Access tables with the data.

Creating a new file without using a ServletContext

Assume I want to write to a new file created within the space of my webapp.
One way would be use getServletContext().getRealPath("/") and use that String to create a new file on the server. However, often I come across advice like not using getServletContext().getRealPath("/").
Can someone please let me know of another way to write a new file within my webapp?
Many thanks.
Have some configuration property containing the absolute path of a directory outside of the webapp and web server path, read this path from the configuration property, and write to this directory.
To serve files from this directory, write a servlet that takes the name or ID of this file as parameter, reads the file from the directory, and sends its content to the response.
This will
work even if the app is deployed as a war file and never unzipped to the file system
allow you to redeploy the next version of the app or server without deleting all the uploaded/created files
allow you to add whatever control you want on the uploaded/created files, instead of making them available to everyone
In short, treat these files as data, stored in a database which happens to be the file system instead of a SQL database.

How to make files available as a download link in asp.net?

In my asp.net website, users can upload multiple files, which reside in 'upload' folder on server's hard drive. I am saving the files by renaming them with username + original file name
Example : if I upload file 'user.text' and my code is 1000, then the file will be saved in upload folder with name '1000_user.Text'. This is for identification of file against a particular user.
Now, when admin logs in application and selects a user, he should be able to see all files uploaded by him/her. Files should be in downloadable format to him.
How can I achieve the same? I have totally no idea, how to go for it ?
This is not how you ask a question on SO. Anyway already a piece of advice :
Store your files OUTSIDE of the webroot, and use a script in your website to get the files and render it to the user. This is for security reason : if someone manages to upload a malicious file, you don't want him to be able to execute it from the web.
Store file info and upload info in a database : who uploaded what and when ? Relying on file name isn't a good idea. You could also store the file in the database eventually (as a BLOB), but I prefer the good old filesystem
I can suggest you couple of ways based on feasibility. Approach 1 - If you can make database changes, when user is uploading a file you should save dynamically generated filename against the user in database. This way when admin logs in and wants to see all the files uploaded by user, you just need to make a database query and show them in a grid. When user clicks on the file name, you can fetch the file from harddisk and allow it to download to admin.
If database change is not feasible, your task will be more difficult. You will have to query in your directory with username in all the files. You can make use of LINQ to make this task little easier to you. Once you have got all the list, show it to admin.
I would recommend you to save metadata about the file on the database (like original file name and username). Then you could use this metadata to find the files that the user have uploaded. Give the files a file name on disk that is based on a unique ID in the database table. That way a user can upload several files with the same name. Make sure that this folder is not located within you application so that they are accessible with a url for security reasons.
The most recommended way to achieve this is to use a database, it is same as a module of Gallery where a user has albums and some pictures in albums. Admin can view each user's album and pictures in the album. you have to introduce two tables in your database. I having the category information of files being uploaded (master table) and other table having the info about the files itself (details table).
The other way is to create a hierarchal folder directory for each user for file categories in your application and then recursively read the directories and fields and then list them up to show them to application users. this would be costly i guess when number of files will increase.

Resources