views:

1633

answers:

6

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?

A: 

I don't think this can be done.

Mischa Kroon
+5  A: 

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
if I give the button a name="..." attribute, i can check is it was clicked. The button that gets clicked is sent along with the entire request, so I should be able to find it by using $_REQUEST['buttonName']
Vordreller
@Vordreller - If you use the same form, the upload would still go through before the POST gets processed. Not a good solution.
ceejayoz
A: 

If you did want to test it there are a plethora of free (or very cheap) hosts with PHP running, no need to "buy a server"

A: 

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.

Sherm Pendley
A: 

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?

Daan
A: 

Hi there. 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.

if you need help, feel free to email me: [email protected]