I want my previously uploaded file to be automatically selected while I am editing details which also include file upload.
I have used value attribute as in other types. But this doesn't work.
<input type="file" name="file" value="<?php echo $news['image']; ?> ">
I have to select the required file again while I am editing details. No file is selected as default while editing. I want to edit other details but not the file upload.
What can I do so that I do not have to select it again?
You can display image using
<img src="path/<?php echo $news['image']; ?>">
or if it is a file use the
My File
tag to link.
When you update the DB make sure you update the field image only when the new file is selected.
"I have to select the required file again while I am editing details"
...no you don't. Just write the server-side code so that if no new file is selected, it doesn't delete the existing one when the form is submitted. And you can provide a link to the existing file on the edit form so that the user can see what's already there.
P.S. You can't make it select the same file again in the input box because
a) that's a security problem, your code cannot select files from the user's device - otherwise malicious websites could use hidden file inputs to secretly steal files from a device. The browser enforces that the user must manually select the file, in order to prevent that possibility
and
b) it wouldn't be logical to try and select the same file again anyway - remember that an input file takes files from the local device, not the server. But there's no guarantee that the file still exists in the same location (and your server won't know where that is anyway), and hasn't been moved, renamed or deleted, or that the form is even being used on the same device as previously. And even if you ignore all that, what would be the point of re-uploading a file you've already got on the server? It would just be a waste of bandwidth.
Related
I'm trying to populate a second file input with the selected file from the first.
I've looked into this and, for security reasons, it's not possible to get the full path from one file input - only the file name. This isn't of any help as the other file input won't be able to upload as it won't be able to find the file.
Example:
<input type="file" id="1" /> - User browse the file by clicking on this input
<input type="file" id="2" /> - This input gets the value from the other file input
The reason I want to do this is because I am building a web app in Wordpress that allows users to upload a photo to Facebook but I also what the same photo to be attached to the wp post. I don't want them to have to select the file for each input.
Is there any way to achieve this using PHP and jQuery?
Due to security restrictions, you cannot programatically set the value of file input type fields, it is not possible.
Also, I don't see why you need to upload the same file twice? If you wish to save the same file in multiple locations, you can copy the first file into the second location too, isnt it ?
Please rephrase the question explaining why you want to upload the same file twice so a better answer can be given.
It sounds like you are approaching this the wrong way. Modern browsers prevent you manipulating the value of file inputs for security reasons. What you are suggesting is not possible.
You should be uploading the file to one single location and the sending it to Facebook/WordPress once it is uploaded. If the method of submitting the photo to either must be via POST, then you can use cURL to send that file.
Here is an example: Send a file via POST with cURL
I am building a form right now that returns the user back to the form itself if there is missing fields or fields are entered wrong. One of the inputs is a image file. I was wondering if you want to echo out the image location again, do you use $_FILES[tmp_name]?
e.g. value="$_FILES[tmp_name]" to echo back the location so the user doesn't have to reselect the image again.
its not possible this way.
the $_FILES[tmp_name] reflects the full path of the already uploaded image on the server.
an its not possible to pre-select the upload field in the browser and its also not possible to get the full client-side path of the uploaded file.
so in case of an error, you could copy this temporary file to another location and display the already uploaded image to the user instead of giving him the upload field again.
but you need to make sure to delete this copied image if the user didn't try to fix his invalid fields.
or you seperate the validation of the fields from the image upload part. using some ajax magic or seperate form.
No, you can't use $_FILES['tmp_name'] in this way, as that isn't the name it had on the users computer, but the temporary name that it has on your server in your tmp folder. Not sure if there is a way to do this. I'll look into it and let you know if I find anything, but I doubt there is.
If you used an AJAX file upload, you wouldn't have to worry about this though.
This is not possible, you could try to use a value attribute on a file input to see that it has no effect.
Imagine that if you could do this it would be a serious security problem (ie: hide a file input with a pre-filled value with a common path for an important file and get it the same time with some other details)
If the form is submitted via normal html form submission then you do not have access to the user's file path. Your best bet is to use an AJAX form submission.
I have got a simple form which has few fields like name, address, etc and two fields for uploading images and finally two buttons, one for preview and the other is a final submission.
I want to make the form much more user friendly, so that when the user clicks the preview button, I show a small prview and still if the user has to makes some changes in the fields which he has already entered, I want to the show the form echoing all the values which the user has entered.
In case of text field I am just using this piece of PHP code inside the tag to echo the name
<input type="text" name="name" value="<?php echo (isset($_REQUEST['name']) ? $_REQUEST['name'] : ""); ?>" />
and so for other fields in the form.
My question is that how can I echo/show the uploaded file names even after clicking the preview button, which in my case does upload the two images into the specified directory, that is I can see that the uploaded images are stored in the specified directory. In case if the user wants to upload only one image, then he can see the names of the already uploaded images in the output form, so that the user doesnot need to choose both the fields for uploading the images one more time.
I hope I explained it in a understandable way, in case if something is unclear then please let me know.
P.S. A small example would be good to follow.
Regards
Maks
Unfortunately if you're using the file input type you cannot modify their values in any way. This is done for the safety of the user so that you don't specify a default file to upload from the user's system (you could theoretically upload a sensitive file if they didn't change the field from the default).
You could try using a text input field that's read-only and shows the url to the file the user uploaded. An edit button next to this field could create a file upload field and allow them to upload a new file.
Not really sure where to start on this one and a day of google searches hasn't helped much.
I am working on a submission form where my logged in users can submit data in form fields and attach a number of files. These files should be available to the users across many submissions so as not to clog up my database with duplicate files and for user convenience not having to reupload files many times. The files will be mostly PDFs and word docs.
My question is, what is the most elegant solution to letting my user select a previously submitted file for their current submission and to "attach" or associate that file with multiple submissions? Some drop down box that shows previous uploads seems best and if you agree any code snippets would be super helpful.
I've decided to store the document files as blobs in their own table. I'm running php and MySQL on Linux with godaddy.
Thanks for advice on where to look for code samples in advance. Just haven't been able to find what I'm looking for. Is JavaScript necessary here?
A better way to handle file uploads is to simply store the file-name (which should be generated as unique) in the database and store the actual file in a directory. That way, you won't have this issue of storing it in a BLOB. Is there any reason you are doing that over this method?
Now, if you output a SELECT form element with a list of filenames stored in the database, a user can simply select one and all you need to do is store that filename in the new row. Then, all rows that point to that file name will point to the same file, with no need to upload it.
If you want to allow multiple file uploads or "attachments" in this case, then you are going to need to create a one-to-many relationship in the database. Each submission row will have many rows in another table that point to it, all referencing the corresponding files uploaded. Such that any submission can have zero, 1, 2, or more files attached. If they select files that already exist, great, just copy the username as stated above. IF its a new file, then you should upload that file and add its name to the database.
Javascript isn't required but it would be nice - to add interactivity to your form. If you want to allow for a user to click say, "Add New File", and have a new file upload form element appear.
Start with a a form. Add a select with the list of files associated with that user. Add another form element that allows for file upload. Have all of your other stuff as normal.
On page view, populate the select with the filenames and put the row IDs for that file in the <option value="file_id_xxx">....
In your server script, detect if a file was uploaded; if so, complete the process of storing the file and set the file reference as the associated file for the submission; if not, check that a file was selected from the dropdown and if so, save the reference from the select with previous uploaded files with the submission instead.
Javascript may help in disabling the select or file upload input, depending. But it's not necessary.
I am developing a script in PHP for uploading files to a MySQL database. There various things that need to selected in addition to the actual file that go into the database entry. The script needs to be designed so that it checks to make sure all of the proper selections have been made, and if they are not the script returns to the upload file state with the values the user had selected still populated. I have accomplished this for the select boxes of the from using session variables, however I can not figure out how to get the actual path of the file upload input to post. I can only seem to access the file name and not the actual path from the $_FILE array. I have also tried to do the following:
echo "<input type='hidden' name='MAX_FILE_SIZE' value='8000000'>";
echo "<input type='hidden' name='remote_file_path' value=''>";
echo "<input name='userfile'type='file'
onchange='document.uploadForm.remote_file_path.value=this.value;'>";
Naturally, the form name is "uploadForm". This works, but again when access the value of $_POST['remote_file_path'], I am only receiving the file name and not the path. After some investigation it appears that this is a security feature built into Fire Fox. I am starting to think it can't be done.
Thanks in advance.
You can't populate file select text box for security reasons, just as you discovered. However, you don't really need to populate file text box to retain the uploaded files.
Every time, a file is uploaded to your server, move it to a secure location and also link it with the current session or user. Now, when you redisplay the form because user made some mistake (or wants to edit something), display the filename along side the empty file box. That way, user can see what files they have already uploaded. With some JavaScript you can give user the option to cross off the filename upon which they can fill up the file text box again and submit another file which will be processed in the server. if the file box is empty, previously submitted file would be assumed to be the valid one and processed.
You don't receive complete file path (in some browsers), and can't change an <input type=file> value (through scripting) in any of them, since those actions poses as security problems.
You don't need the hidden fields, PHP will parse the posted data for you, and present it in the $_FILES array:
http://www.php.net/manual/en/features.file-upload.post-method.php
Like many have said, you can't it is a security issue via HTTP. What happens when you upload an image from a local machine is the function fires off and creates the tmp_name to be accessed on the server.
However if you truly want this functionality you can use pure Java via an applet to get a local path. However what are you trying to do, there may be a better way of going about it rather than what you are thinking.