Editing user uploaded files? - php

We have a form where user can submit a message, as well as upload 1-10 files (not displayed in-line in the message, but as links at the bottom of the message).
This is all working fine so far... using two separate forms.
1 x form (MAIN/parent form) is the message details form (name. date, message..etc)
1 x form that is under this form, and if for handling the selected/uploading of user selected files.
This is done through a hidden iFrame and dedicated php script, and returns a list of uploaded file names/paths back to the MAIN/parent form (as hidden fields).. so when the MAIN/parent form is submitted.. all the data get saved to the database at one time, and the image/file uploading event/portion wont cause any loss of message data if there is timeout or other uploading error.
This is all working so far.
What I am not clear about or stuck on... is that for each message post the message 'owner/poster' can go back and edit the message..
This should include letting the user edit the files uploaded/attached to the post as well.
Which I'm not sure how I should be handling? or a nice and easy to do this?
I'm guessing I'll have to do the 'two form' approach I use for the initial posting/uploading of the message?.. but how can I pre-populate the file input fields? DO I just pre-populate the value with the file name/path form the DB.. ?

Related

How to restore input file type field after failling form validation in cakephp [duplicate]

Okay so here's the scenario:
User is presented with form that contains file inputs.
User submits form.
Form does not pass validation/verification for one reason or another.
User is presented form with errors highlighted.
File inputs are now blank.
Question:
Is it possible to re-populate the file inputs with the paths to the files the user originally selected. A PHP solution would be ideal, but I'm also open to JavaScript solutions as well.
I think the short answer here is no. You can't repopulate file upload fields. However, you can work around it.
If a file has been selected and the form submitted, then you've already received the file. What you can do is keep a reference to the file on disk and put that in a hidden field and show a message to indicate to the user you still have their file uploaded so it does not need to be replaced/re-uploaded. When your form gets submitted again without a file, you can check for the hidden field value and use that to get your local copy of the file they uploaded in their last attempt.
The other way to do this is to either submit the form via ajax (using either flash or the iframe method for the file upload) or to do an ajax call to validate the form first, and then only submit as normal if valid (thus no form reload, no loss of file upload field data).
I solved this problem by let user pick upload file as the last field before submit the form ^^

symfony2 - file upload lost upon form error

I have followed tutorial: http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html
but I have problem handling situation when I upload file, but other field does not meet validation criteria and user is directed to fix form errors. Then when user fix problem and hit submit button, he no longer has valid image in form and missing file error is displayed.
I have prepared small example project to illustrate this behavior:
https://github.com/Jaslanin/sf_upload_form_error
Steps to reproduce this behavior:
open web/ dir of project inside browser
in name field put string "123"
set image file for file field
hit submit button
change name field to "123456"
hit submit button
observe that form has missing file error
For now I handled this problem using steps within "How to Handle File Uploads with Doctrine" tutorial, but without using lifecyclecallbacks. I run ->upload manually and in case of error store file path inside session.
It works, but I am looking for cleaner solution for that problem.
When you render the form on your page all input fields are populated with values from an entity or an array. The key point here is that you have no data to pre-populate file input field. So it renders as empty input. And when user hits submit on step 6, there is no file selected/uploaded. Your solution with a session-stored uploaded file path is proper solution in this case.
I also suggest you to show to user, that some file has been already submitted when there is one in the session.

Is it possible ot send the $_FILES array to PHP using jQuery/AJAX?

I have a form our guests use to submit a post to a very simple 'message board'.
Now we want to allow the users to upload 1-10 files, but the concern or requirement is to do this outside of the MAIN form submit.
MAIN FORM consists of:
input field 1
input field 2
text area 1
Submit button
I currently have it so there is an initial browse button.. ( below the text are and to the left of the main submit button) and once a file is picked.. and display a link for the user to add another 'browse' field..
What I would like to do is have an UPLOAD button below all these dynamically created browse/file upload fields... that will send all the file data to an external .php script to upload the files in question, and then just return the file path/name back to the main form (maybe in hidden fields? I dont care).. so that these file path/name string values are submitted when the MAIN FORM is submitted..
hope that make sense.
Is this possible? And if so how do I go about this? The concern is to handle the asset uploading/file handling outside of the main form submission so the users details are not lost if something goes wrong with the file upload portion of things.
You can do this with JQuery and Ajax. There are various plugins are available for this. You can try this - http://plugins.jquery.com/uploadfile/

Don't lose input type="file" value after page reload [duplicate]

This question already has an answer here:
Restoring the value of a input type=file after failed validation
(1 answer)
Closed 8 years ago.
I have a problem with an form on my website. The form contains some text inputs and a file upload input. When some text fields aren't filled correctly, there come's an error and the page is reloaded. But: If I choose a file, don't fill out the other files correctly and the page is reloaded, I must choose my file again, it isn't saved in the form.
Can anybody help me please?
For security reasons, you cannot set the value of a file input at either client or server.
I can think of these possible work-arounds:
This post provides one method of a server-side solution involving putting an ID in the page that refers to the previously uploaded files rather than the actual file inputs themselves (since the files are likely already uploaded).
Many sites that accept file uploads do them in a somewhat separate form. The file uploads are completed and then the rest of the form is filled out and submitted with the server keeping a reference connection between the previously uploaded files and the current action that will be submitted to the server. The popular forum software vBulletin works that way for message attachments. Attachments are uploaded in a separate form/UI.
You can submit your form via ajax, rather than as a form submission. Because the ajax call does not change the current page, if you get errors from the submission via ajax, then the current page and the selected file inputs are still there. You can add your error reports to the current page with javascript and show the users the error that way.
You can also avoid most of this issue (thought not all), by doing as much client-side validation as possible with javascript before submitting your form so you lessen the possibilities of the server ever rejecting the form submission.

How to do File upload validation Server side without refreshing the Page?

I have created a script to Upload a file using PHP. I have also added file validation checking for File size & File type. But the scenario here is, the Form contain 10-12 Text boxes & a File upload box. So whenever someone fill the complete form & submit and they get File upload validation error, they get back to the Form with the validation error message. But the form gets empty as obviously it get posted on server side.
Is there any way to do the file upload validation without reloading/refreshing the page ?
I strongly recommend this plugin for file uploads. http://malsup.com/jquery/form/
It will allow you to verify the file, and if theres an error you can use json to determine whether the file is valid or not. I use it in all my projects, it's fantastic.
EDIT: Also it allows you to submit files without page refresh, which is the most important part.
You can use ajax:
1) when submit an ajax synchrounous call send the form content to the php server page
2) the php page validates the input
3) if return is "false" (error in validation) the javascript that executed the call sympli insert into the html of the page a proper message. The form stands filled in with the values of the user
You can use $_POST array for the posted values from the form . You should have write form method as POST
Also the same way you can ajax form this will not loose any of your data and page will not refresh as well

Categories