4 Replies - 737 Views - Last Post: 02 August 2012 - 11:48 AM Rate Topic: ***** 1 Votes

#1 StefanOnRails  Icon User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • Posts: 105
  • Joined: 31-July 12

delete file after user leaves page

Posted 31 July 2012 - 08:03 AM

Hi, I'm new on this community so please be patient with me :)

Ok, here's my problem: I built a website where users can upload their photos, but (I know this will sound weird) I need to delete their photos after they leave the page. Is there any way to this in PHP? I've already tried to use "onbeforeunload()" and then handle the problem using Javascript redirect (by accessing via URL a PHP script), but the problem is Opera seems to not recognize "onbeforeunload()" no matter what I try. So I was thinking to delete the file via PHP immediately after the image is loaded on the page, but no matter how I try to do this it seems the "src" of the "img" is lost. By the way, I echo the "<img />" before deleting the file via "unlink()".

So, any idea?

Is This A Good Question/Topic? 0
  • +

Replies To: delete file after user leaves page

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3047
  • View blog
  • Posts: 4,562
  • Joined: 08-June 10

Re: delete file after user leaves page

Posted 31 July 2012 - 11:45 AM

Hey.

View PostStefanOnRails, on 31 July 2012 - 03:03 PM, said:

I've already tried to use "onbeforeunload()"

This is extremely unreliable. You have no way of knowing whether the user's browser supports that event, or even if it supports Javascript in general. Even if it does, the user may have chosen to disable it.

View PostStefanOnRails, on 31 July 2012 - 03:03 PM, said:

So I was thinking to delete the file via PHP immediately after the image is loaded on the page, but no matter how I try to do this it seems the "src" of the "img" is lost. By the way, I echo the "<img />" before deleting the file via "unlink()".

Is the unlink happening in the same PHP script as he echo? That would not work because: 1) The PHP script may well end before any of the HTML reaches the browser, and 2) An <img> tag is only an instruction for the browser to download an image; there will always be a small delay before the download actually happens. The PHP script will be done deleting the file before it ever has a chance to be downloaded.

View PostStefanOnRails, on 31 July 2012 - 03:03 PM, said:

So, any idea?


I've got two suggestions.

  • Use a PHP script in the <img src=""> attribute that sends the image and then deletes it. That way you can be 100% sure it has been sent before it is deleted.

  • Upload all the images to a temporary location, and set up a cron-job (scheduled script execution) to execute a script that clears it out ever now and then. - The downside here is that there is still a small chance that, due to unfortunate timing, an image is deleted before it is downloaded. (Albeit, a very very small chance.) The images will also linger for a while until the script is executed to delete them.

Was This Post Helpful? 3
  • +
  • -

#3 StefanOnRails  Icon User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • Posts: 105
  • Joined: 31-July 12

Re: delete file after user leaves page

Posted 02 August 2012 - 09:46 AM

That Cron Job worked! Thanks a lot man, you saved me :) (btw I posted this before, but somehow it was removed :mellow: )
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3047
  • View blog
  • Posts: 4,562
  • Joined: 08-June 10

Re: delete file after user leaves page

Posted 02 August 2012 - 10:17 AM

Glad it worked out :)

View PostStefanOnRails, on 02 August 2012 - 04:46 PM, said:

(btw I posted this before, but somehow it was removed :mellow: )

Yea, there was an attack on the site yesterday that forced them to roll back the database a few hours. There is a discussion going on about that here, if you are interested.
Was This Post Helpful? 1
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,533
  • Joined: 08-August 08

Re: delete file after user leaves page

Posted 02 August 2012 - 11:48 AM

I'd put the images all in the same folder using unique (maybe with the date/time in them) names. Then as part of the script that generates these files I'd remove files that are over XX minutes old. That way you'd never have very many hanging around and you don't have to mess with a cron job.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1