开发者

How to delete a file whenever user leaves page?

开发者 https://www.devze.com 2022-12-11 15:39 出处:网络
I have a form with action set to a php-file (verify.php). In the form, I have made a pretty nice picture upload section.

I have a form with action set to a php-file (verify.php). In the form, I have made a pretty nice picture upload section. After submitting the form, the verify.php is opened to verify the form filled in by the user.

The form values are all placed inside another form on the verify.php page. Only the image is uploaded to a folder on the server, and this is my problem. I want the image to be deleted if the user regrets (doesn't verify) putting in an ad, or if the user g开发者_如何转开发oes back to change the ad again.

How would I do this?

One way could be to delete the filename whenever page 'unLoads' (user hits back button). But that wont solve the problem if the user closes the browser on the verify.php page, because that doesnt count as an 'unload' does it?

Solutions?

If you need more input, tell me and I will update this post!

Thanks


Unfortunately this is a pretty tricky thing to do, the easier solution would be to check to see which files are old and delete them the next time someone accesses the page. This could be done completely through PHP and would be very reliable.


Maybe I have misunderstood, but the process could be this:

1 User uploads file (that goes to *nix /tmp folder as normal)

2 You move the file to a second temp folder /upload_unconfirmed

3 When the user ACTUALLY does confirm the action, you then a) move it to your folder to keep it permanently, say, /clientfolder/theirusername.jpg b) then delete theirusername.jpg in /upload_unconfirmed

In the background run a cron to rm everyfile older than say 1 hour in /upload_unconfirmed/*

If each client only ever has one image, then subsequent uploads will overwrite their old file.


Detecting "abandons" like browser closing is essentially impossible. There is no reliable way to track it on the server side in real-time. The easiest way to delete these images is to set up a running process that removes images associated with abandoned singups.


normally, server side script don't know user leaves the page or close the browser, it can only check session timeout, but its normally 30 minutes or more depends on setting.

So if you really like to do it real-time, put ajax timer in the pages, and send hello request every 30 seconds or something like that to server, and then you could delete image file if there is no response more than 1 minute or something like that.

But sending ajax request to server every 30s or less could effect the server performance, if you have many users on it.


Don't move the file to its permanent location until the user explicitly verifies. PHP puts uploaded files into a specific place, usually the /tmp directory (on *nix installations) which gets automatically cleaned out in fifo order as space is required. So if you don't move the file out of this special directory, then it will be autocleaned without your further intervention at an unspecified time in the future.


I would name the image by epoch time (using the time() function) like 1258560055.jpg then build a cron job that goes in and deletes every image that's an hour, two hours or whatever after.


This is how you do it:

jQuery:

$(window).bind('beforeunload',function(){
    if(check){ //whatever check you wish to do
        return 'Are you sure you want to leave?';
    }
});
$(window).unload(function(){
    $.ajax({
        type: 'POST',
        url: 'file.php',
        data: 'value=unload',
        async: false // this will not work if set to 'true'
    });
    alert('done');
});

PHP:

if(isset($_POST['value'])){

    if($_POST['value']=='unload'){

        foreach(glob($targetFolder.'*.*') as $file){
            unlink($file); // remove every file
        }
        rmdir($targetFolder); // remove directory
    }
}

Note: I also have a cron job run once a week, just in case. The php situation is where user gets a specific tmp folder and uploads files (i.e. CV, motivation letter...), if he leaves without submitting, the files and folder are removed.


I would put a time limit on how long a user has to confirm their ad posting. Then, have a monitoring job that will fire at that predetermined time and if the ad has not been confirmed, delete the image from the server.


I don't think it's very easy to detect when a user truly leaves a page. Most solutions I see usually perform some action (such as deleting temporary files) when the session times out.


Setup a database entry for each file with the timestamp. Then when your process is completed, remove that entry. Then, once a day, go through the table, delete all files still there, and remove those records from the table... it's the safest bet.


Couldn't you just simply upload the image after verifying the submitted data?

0

精彩评论

暂无评论...
验证码 换一张
取 消