最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How to delete a file whenever user leaves page? - Stack Overflow

programmeradmin4浏览0评论

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 goes 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

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 goes 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

Share Improve this question asked Nov 18, 2009 at 15:53 user188962user188962 1
  • Why are there two steps to this process? I'm finding it hard to understand, but it would appear that you have a form with the user fills out, it uploads their information + image and then there is another page to verify the advert? – Ben Everard Commented Nov 18, 2009 at 15:57
Add a comment  | 

11 Answers 11

Reset to default 7

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)

  1. 暂无评论