I am working on application that require file uploads, the files can be from 10-150 MB , it is working fine.
But once the file transfer is initiated and in between user leaves the pages and comes back to that pages, we have to start the uploading again.
So is there any way through which file transfer should not stop even in case we leave the page.
I am using, jQuery , bootstrap and PHP for the application.
Nope. You can't send an upload request and leave the page peforming that request. Its the opposite of a download.
Most sites use a call to "window.open" on a button titled "upload" to open new window where the user can select the file to upload. The page in this window would be solely used for the upload process. When this page is submitted with ajax, a spinner of sorts would appear until that upload request is complete.
On the other hand (avoiding window.open), I assume you're using a form, so you could start the upload on another tab or something with target="_blank" on your form, but it may be a challenge to give information on what is occurring (the upload) in this new tab...
<form target="_blank" action="upload.php">
<input type="file" name="upload1" ></input>
<input type="submit" name="" value="">
</form>
Related
I'm trying to find a way to input an image directly onto the same page, but I can't figure it out.
The image doesn't need to be saved when navigating away from the page.
I've tried:
<form action="#" method="post">
but I still can't figure out how to actually put it where I want it.
This might be really simple and I'm just overthinking it, but I've been googling for hours with no result.
You need to leave empty form action and specify enctype
Example:
<form action="" method="POST" enctype="multipart/form-data">
<input type="file">
<input type="submit">
</form>
This will push image into $_FILES array in php. For uploading images you need to view http://www.w3schools.com/php/php_file_upload.asp
If I understand you correctly, basically what you are trying to do is impossible. (without ajax)
The basic way a web-site works is:
browser asks for a page
server sends back page.
If you're trying to change the page after it has been sent to the browser (i.e. add an image), then you need to do some work behind the scenes.
browser asks for a page
server sends back page with some javascript in it that allows fetching additional data from the server.
javascript asks for additional data from the server.
additional data gets added to the page.
Since javascript has no access to the local machine outside the browser sandbox you'll have to get the user/javascript to send the image to the server, and the server then has to send it back to your javascript (which can then embed it in your page)
jquery or similar is probably the way to go.
You'll need a page/script on your server to handle the upload and serving of the image file.
Alternative
A simpler method might be to have a file input field on a form in your initial page with no image, when the page is submitted, check the $_FILES variable (see move_uploaded_file) and now serve the same page this time with the uploaded file displayed. You could then delete the image file from your server if you need it only once.
I have a form that posts a file to a site that I don't have control and that site responds with a dynamically generated text file based on the uploaded file. That response is downloaded by the client after the form is submitted. My question is: how can I grab this text file and copy it to my server before it gets to the user?
Possible solution 1: is there a way to change this file's headers and put its content to some hidden div or iframe?
Possible solution 2: I think the best way is that I create a script that gets the form data and then reposts it to the external site and then gets the response and writes this response to my site in a txt file. Unfortunately all my attempts to get this working have failed.
Example of solution 2:
<form action="mysite.com/respostScriptThatGrabsResponse.php" method="post">
<input type="file" name="filename"/>
<input type="submit" value="submit"/>
</from>
Theory #2 would be the way to go. You could use cURL to post the data to the external site. The most likely obstacle preventing success would be the external site requiring cookie data that only the client would have.
I'm new in php, now I'm working on a website in which there is one problem occuring in email form.
I want to attach more than one file as attachments (for this task i got required code).
I have one browse button, but I'm not getting what code I have to write there for invoking required file/directory/folder,(when I click on browse button one window is open from which I take single file at a time, like gmail compose email form).
I think you understand what I want to tell you (I'm not having well communication).
Thanking You in Advance
Amol, simple procedure to accomplish this is to provide multiple file upload controls on your form like:
<input type="file" name="file1">
<input type="file" name="file2">
<input type="file" name="file3">
.
.
.
<input type="file" name="file10">
On server side you can iterate over all $_FILES to see how many files were actually uploaded and process them accordingly.
Advanced methods include using an ajax or flash uploader to upload multiple files asynchronously; storing them in a temporary folder and storing list of uploaded files in SESSION. When the send button is clicked, you can process that SESSION variable to determine how many files were attached to the email being composed and process accordingly.
I am sure whatever file attachment script you're using supports attaching more than one file.
I want to have a user select a file and then have php put the contents in db. Now the last part (processing the file in php) is easy. But is there a way I can process a user selected file whithout a new page load?
If I use the following:
<FORM ACTION="upload.php" METHOD="post" ENCTYPE="multipart/form-data">
<INPUT TYPE="file" NAME="somefile"><BR />
<INPUT TYPE="submit" NAME="submit" VALUE="Upload">
</FORM>
Page upload.php automaticaly loads after which I can insert the uploaded file in a database.
I would like to use a combination of javascript, php and xajax to process the file. I don't think something like this is possible:
<FORM ACTION="javascript:xajax_proces_file()" METHOD="post" ENCTYPE="multipart/form-data">
<INPUT TYPE="file" NAME="somefile"><BR />
<INPUT TYPE="submit" NAME="submit" VALUE="Upload">
</FORM>
Because the file is not uploaded when function xajax_process_file() is called. Or is it? I think I do not fully grasp the principle of uploads with javascript, html and php.
Any help and or clarification is much appreciated.
It may help to think of this as a two step process.
First, the user fills in the form and submits it - step one.
Second ( which is the default action ) the specified target file takes the input from the form and uses it to do whatever. You can almost think of a form "action" as a link - the default action of a link click is to display the result of the link. The same goes for a form action - display the result of a form action.
Now, it's possible via JavaScript to disable the default action of an element for a particular event. It is also possible via JavaScript to access a browsers HTTP mechanism to send/receive HTTP request (which is what every page request is - whether from your URL bar or a page link or a Google search result).
And that is what AJAX in simple terms is - using JavaScript to use a browsers HTTP mechanism to send requests to a web server and possible receive a response back without the use of a traditional click event. You then combine this with the use of JavaScript to "turn off" default actions and instead follow the action specified by you to get information from a server and add it to the page without ever having to refresh the page.
Many times to prevent the defualt action from taking place for a certain element, you return false in your code. The same goes for your form. Using javascript:
form.onSubmit = function() {
blah blah blah.....Use ajax to send the information to the form handler
return false; //Prevents the defualt action of the submit event
}
If you are really new to AJAX, I suggest you check out this tutorial and then this one. Lastly, I would recommend using a Javascript framework like jQuery to help you - it is awesome and does alot of great stuff, but also has easy and built in functionality for AJAX.
Here is another tutorial to do a form submit with no page refresh (uses jquery).
an alternative is to make the form directs the action to an iframe, after processing the query in the iframe, proceed by JS to clear the form of the father
I'm using a small site to experiment with the uploading of pictures and displaying them.
When someone clicks "add a picture", they get taken to a page with a form on it. They can select a file and click the submit button.
But what I want to do now is this: put a second submit button labeled "Cancel" next to the normal confirmation button. If someone then chooses to upload, selects and hits submit, if they press the cancel button before the file is fully uploaded, PHP should stop the uploading of the file and delete it. And then just go back to the overview.
No Javascript used whatsoever.
I only have localhost, so testing this in kindof impossible, since I just copy a file the millisecond I press the submit button. There's no upload-time with a localhost and I'm not going to buy a server somewhere just for this.
Basically what's happening now is that the PHP detects which button was sent. If the submit button was sent, the file is uploaded, if the cancel button is sent, it just goes back to the overview.
But PHP does its tasks one in a row. So I don't think this will work. How do I tell PHP to stop doing the upload?
You can't look at the button with php - the whole request will have been sent before PHP gets that information.
What you could do is put the cancel button in a different <form>, like this:
<form> <input type="file> <input type="submit" value="upload"> </form>
<form> <input type="submit" value="cancel"> </form>
(This is just an example of course - there's bits missing).
When they click cancel, the browser should abandon its request and start a new one, cancelling the upload.
Greg has the right idea there.
In stead of following the majority, or looking at how things are done, look at smart alternatives, like Greg's explanation above.
Any upload cannot function without a valid link between the client and the server; hence if you close your browser window, or re-direct it to some place else, the upload is killed.
use an iframe and name it eg.: "myframe". you can easily hide it in a div. have a form with your action="somefile.php" and target="myframe". add a lil Javascript to your form's "file" field: onFocus="uploadlistener()". you can name this function anything you like, but use it to check if the person "opened" anything. the browser will automatically swap focus, whether, the user clicked "browse", or if he then opened a file. difference is, after a file has been "selected", the input field receives the focus again, but remember you have a listener on the "onFocus()" event. so: if the field is not empty, call: document.uploadform.submit()
Using this method, you don't even need a submit button. if you want to skin your "browse" button, just make it transparent using CSS, eg:
input name="myfile" type="file" style="width: 40px; -moz-opacity: 0; filter: alpha(opacity=0)" onFocus="somefunction()"
Well to cancel upload just redirect the iframe to some other place, eg:
input type="button" value="cancel" onClick="document.uploadform.action='blank.php'; document.uploadform.submit()"
Pardon if the html above spans across multiple lines, it's the first time I'm posting here.
To track progress, you need to add a server side listener, either with PHP-5.2, or some Perl script to check how many bytes have been loaded thus far of the total file size checked before upload was started. this check interval can be achived with some AJAX, or if you don't know the term, look up: HTTP-REQUEST. this runs server side code in the background and outputs a response like you would see in the browser, but here you can capture it in a variable, and do your stuff. I hope this was helpful to anyone.
There's no way for PHP to stop a file upload in progress. Your PHP code isn't even started until the file upload is done, so by the time it's running, it's too late.
If it indeed turns out that cancelling the upload is not possible, you might consider implementing an easy to use undo or delete function, so that users can immediately undo the uploading of the image (i.e. the image is deleted). This may not be useful in your experiment, but perhaps you can use it in a production application?