Amazon S3 File Uploads - php

I can upload files from a form using post, but I am trying to find out how to add extra fields to the form i.e File Description, Type and etc.
Also can I upload more than one file at once, I know it says you can't using post in the documentation but are there any work arounds?
Thanks in Advance

In regards to uploading multiple files, are you uploading directly to S3 using POST, or posting to s3 using CURL or a similar lib from your own server?
Why are you adding these extra inputs? If posting directly to S3, you cannot post any inputs that aren't specified as required or optional in the S3 documentation. Any form elements that don't start with "x-ignore-" and aren't required/optional for S3 post upload WILL cause an error to be returned from S3, without uploading your file. If you have elements in the form that can cause this error and they are important to leave in the form before it is submitted (being used as input for an ajax call, etc), then simply append the name of these form elements with "x-ignore-" or delete them from the form.
You have control over a few things, such as the name the file is served with and type by usign the Content-Type and Content-Disposition elements. Take a look at this:
http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1434

You can append your additional information to the "return url" you send alongside your request to Amazon.
Be aware that doing this may expose sensitive information about your app logic to the user in the form of an URL. One thing you can do to avoid/hide this is to capture all GET variables on return, save them, and redirect user to a summary page.
This won't block smarter users form learning your GET variables, but will hide them from 99% of the public.

Related

PHP File Upload Without Form

Is there a way that I can allow a user to upload a file by providing the filename/directory for the file in a get variable (or some way other than using a form)?
Essentially, I'd like the user to be able to do something like this:
http://example.com/upload.php?file=C:/files/myFile.txt
and have upload.php handle the upload.
I've been looking at this to try to figure out how to do this, along with a couple of other resources, but they're all focused around using forms.
I need to be able to allow an app to go to my php page and provide a file to be uploaded without a prompt.
It doesn't have to be in a GET, but I CANNOT PROMPT for a file.
Any thoughts?
Sorry, that's not possible. You can upload file using post request. Why are you trying to send data using get request?

Upload files automatically to an input file from a path

I got a DDBB with some paths from different files that I keep in a table. What I'm trying to do is the following:
You can click a button which gets all that paths and loads all the files to an input file just to send it as attached file via e-mail using a php script.
The thing is, I don't know if it is really necessary to take that step with jquery or is it possible to send it straight away using php?
If I understand correctly, you have file paths in DB, which is Backend, and when you create page view you are getting paths from DB by PHP still Backend, then you will put a hidden field etc. when a person click a button 'Frontend but dummy action' you want to load these files and send to backend again.
Logically;
If Client and Server are not discrete then this wouldn't be a reliable system, because you are not sure files are in the correct path which means will not be renamed, deleted.
If client and server are the same you don't need to make this with a file upload you already know you have the paths when user click the button ex : button submits the form with id/group_id 1, fetch paths by id or group_id and send by email.
But still you want to do something like that, Html5 has an api http://www.w3schools.com/jsref/dom_obj_fileupload.asp for it, beyond that you need to execute an applet to achieve this I think.

Using oneuploader in a form with other fields

I would like to use the OneUploader bundle in a Symfony2 form that also contains other fields. In other words, the uploaded file is handled as an attachment to the rest of the data in the form. But I can only find instructions on how add the uploader as a separate form with it's own controller to which the files are sent. So how do I handle the use case I just described?
Bundle dev here.
Frontend uploaders like jQuery File Upload or Dropzone always forge a seperate request when uploading a file to the server. This means that the upload process takes place before the form handling in your controller. If you really want to upload file along with the main form request, then you should not use such an uploader. Instead, create an Entity Media (or the like) map it to the base entity with a OneToMany or ManyToMany association and add it to the form with an Entity type.
There are some pretty good answers here on StackOverflow, for example this one.
However, if you choose to not use a frontend uploader all your files will be sent to the server simultaneously. Depending on the file size / server configuration this can result in upload errors. Moreover the upload will not be performed asynchronously anymore, which forces the user to wait for the upload to complete, after he submitted the form.
There is general problem when dealing with asynchronous multiple uploads on creation forms. I tried to answer a similiar question here.
The problem with enabling file uploads on the create mask is that you eventually end up with orphaned files. This is because a user is able to trigger the upload without saving the actual entity.
Your problem seems like a good use case for the Orphanage feature of this bundle. It lets you upload files before an actual entity exists to attach these files to. After the main form has been submitted you can retrieve the files and perform some more logic on top of it.
Note: This is by no means a perfect solution. Take a look at the limitations! Summarized can be said; as this feature is based on the session, a user may end up having uploaded a file twice. Be sure to handle this accordingly.
And then there is the possibility that you just want to add some more data to the file upload request: As this is handled in the frontend uploader, it differs from implementation to implementation. For example the jQuery File Uploader just serializes the whole form and sends along all other values, including hidden fields.
Personal Recommendation: I'd not send the file along with the form submit request. Instead use a frontend uploader and either:
Let users upload files not until the entity exists where it should be attached. This is a quite common strategy and in most cases the desired one. A simple second step when creating an entity should be sufficient.
Take a look at the Orphanage feature if you really want to be able to upload files directly on the creation mask.

Implementation of fully functional media uploading in web application

Suppose we have the web application which handle create, read, update and delete articles and each article should have gallery of images. I have to make one to one relation between Article and Gallery and one to many relation between Gallery and Media.
HTML5 gives a lot of features like multiupload, so I want to use this excellent http://blueimp.github.io/jQuery-File-Upload/ plugin for that. The problem is how to handle the file upload "in memory" like other form's data?
For example when we show the page for create new article we should be able to fill in article's data fields and select images to upload, next when we click the save button the images should start upload and after that the form should submit. When validation fails the images should be still displayed on the frontend, but on the server-side nothink should be saved.
One of the solutions is create somethink like "create entity session temporary id" before displaying the entire form and that id can be used to create temporary directory for save uploads, so after success saved form these images can be moved to appropriate directory, but how to make the "create entity session temporary id"?
The other solution I think is the "with the edit id" approach, because we can handle the uploads with previously saved gallery id, but sometimes I can't save new blank article with gallery, cause some of the fields should't be empty in db.
For the Rails I saw https://github.com/thoughtbot/paperclip gem which in the Readme says:
Paperclip is intended as an easy file attachment library for Active Record. The intent behind it was to keep setup as easy as possible and to treat files as much like other attributes as possible. This means they aren't saved to their final locations on disk, nor are they deleted if set to nil, until ActiveRecord::Base#save is called.
My question is how it works?
The problem with enabling file uploads on the create mask is that you eventually end up with orphaned files. This is because a user is able to trigger the upload without saving the actual entity. While creating a very own UploadBundle I thought about this problem for a while and came to the conclusion that there is no truly proper solution.
I ended up implementing it like this:
Given the fact that our problem arise from orphaned files, I created an Orphanage which is in charge of managing these files. Uploaded files will first be stored in a separate directory, along with the session_id. This helps distinguishing files across different users. After submitting the form to create the actual entity, you can retrieve the files from the orphanage by using only your session id. If the form was valid you can move the files from the temporary orphanage directory to the final destination of your files.
This method has some pitfalls:
The orphanage directory itself should be cleaned on a regular basis using a cron job or the like.
If a user will upload files and choose not to submit the form, but instead start over with a new form, the newly uploaded files are going to be moved in the same directory. Therefore you will get both the files uploaded the first time and the second time after getting the uploaded files.
This is not the ultimate solution to this problem but more of a workaround. It is in my opinion however cleaner than using temporary entities or session based storage systems.
The mentioned bundle is available on Github and supports both Orphanage and the jQuery File Uploader plugin.
1up-lab/OneupUploaderBundle
I haven't work with the case personaly, but my co-worker had similar conundrum. She used
punkave/symfony2-file-uploader-bundle
It's a bundle that wrapps jQuery File Upload plugin. It is in the early stages and a lot of things are missing, such as event, but we gave it a shot.
That's what we do: in newAction() we create entity, generate unique dir ID, and store the ID in entity (via regular setDirId()). Than we create the form, which contains hidden field dirId.
We are uploading the files to temp dir on server via ajax, not during the submit. Ajax request requires the ID. It stores files in temp_dir/prefix_ID
Than it's quite simple. Form is sent. If form is valid - move files from temp to dest dir. If not - we have the ID, and are able to show the images.
However, we do not save information about individual files in a separate table in the database. Every time we read the contents of the folder that corresponds to our dirId.
I know it's not the solution You are asking for. It's rather a workaround.

php - uploading custom images on product page via AJAX

I am writing a small e-commerce website and I need to be able to allow users to upload their own custom images (limited to JPG or PNG) before they add to basket so that the images can be included with the order.
My current idea is to upload the files via AJAX and insert the filenames into the database so I can then link the images to a product and a specific order in the database. However, I have read that you cannot easily do this with AJAX as it is not yet supported?
Should I literally use a normal file upload form and process the $_FILES and $_POST requests after the form is submitted. This is already being done when items are being added to the basket so would be possible, although this could take a while to process the uploads.
Am I missing an obvious method that will solve the problem?
You need to post the form to a hidden iframe, output a json response to the iframe and then parse the response via javascript.

Categories