#1
  1. Plays with fire
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2003
    Location
    Barsoom
    Posts
    1,146
    Rep Power
    144

    creating and deleting temporary links


    Hi--

    I have a client who wants to sell an mp3 via paypal. Once payment is made, the buyer is given a link to download it. We need to delete the link after it's been downloaded.

    It shouldn't be too hard creating the link (just copying the file to a new randomly-named folder and file, but how can I delete it after it's been used the first time?

    Thanks!
    “Be ashamed to die until you have won some victory for humanity.” -- Horace Mann

    "...all men are created equal." -- US Declaration of Independence
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,342
    Rep Power
    630
    I do something similar for an application. I use MySQL to authenticate users for access to the file for download. Once the user authenticates and downloads the file I remove the user's password.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. Plays with fire
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2003
    Location
    Barsoom
    Posts
    1,146
    Rep Power
    144
    Originally Posted by gw1500se
    I do something similar for an application. I use MySQL to authenticate users for access to the file for download. Once the user authenticates and downloads the file I remove the user's password.
    Funny you should mention authentication. Long story short, that won't work. My client's clients can't handle that. Seriously.

    Cron jobs won't work. Javascript? I could use JS to delete the folder after the link is clicked, but it would probably delete it before the download is complete.
    “Be ashamed to die until you have won some victory for humanity.” -- Horace Mann

    "...all men are created equal." -- US Declaration of Independence
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,342
    Rep Power
    630
    If you don't authenticate, how do you know the user that wants to download the file has paid?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. Plays with fire
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2003
    Location
    Barsoom
    Posts
    1,146
    Rep Power
    144
    Originally Posted by gw1500se
    If you don't authenticate, how do you know the user that wants to download the file has paid?
    When paypal comes back to the site after payment, I'll create a unique url and email it to the buyer. I want to know how to delete the url once the link is clicked.
    “Be ashamed to die until you have won some victory for humanity.” -- Horace Mann

    "...all men are created equal." -- US Declaration of Independence
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,342
    Rep Power
    630
    Is the URL pointing to a unique page just for that user? After the download just delete that page with an unlink.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2015
    Location
    London
    Posts
    1
    Rep Power
    0
    You're going to need to have code involved in the delivery process else you'd have to watch the web server logs to know when the file's been downloaded.

    I'd suggest that the following process would be better suited

    Use a link like '/download.php?order=asdasdasdasdasdas' <- some sort of hashed value to represent the order

    Then in download.php, the process follows these sort of steps.

    1. Look up the order identified by the hash in $_GET['order'] (preferably a database rather than a csv)

    2a. If order is valid ->
    - locate the file
    - open a file handle for reading the file
    - send the appropriate headers (see this stackoverflow answer)
    - send the file
    - mark the order as used

    2b. If the order is invalid or the order has been used
    - send a nice friendly error page

    Comments on this post

    • Frank Grimes agrees : Great idea!
    • Will-O-The-Wisp agrees
  14. #8
  15. Plays with fire
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2003
    Location
    Barsoom
    Posts
    1,146
    Rep Power
    144
    @gw1500se it's the "after the download" part I'm having problems with. I need to know WHEN the download is complete.

    @RavenVelvet That's an interesting idea. And your first post! Thanks! And with the added benefit of tracking exactly when the download began. Very cool. Thanks again!
    “Be ashamed to die until you have won some victory for humanity.” -- Horace Mann

    "...all men are created equal." -- US Declaration of Independence
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,342
    Rep Power
    630
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  18. #10
  19. Plays with fire
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2003
    Location
    Barsoom
    Posts
    1,146
    Rep Power
    144
    Originally Posted by gw1500se
    Thanks, but that's about uploads. I need to check downloads.
    “Be ashamed to die until you have won some victory for humanity.” -- Horace Mann

    "...all men are created equal." -- US Declaration of Independence
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,342
    Rep Power
    630
    Oops, sorry. I guess it was too early for me when I replied. What you are asking is a non-trivial exercise since there is no real feedback from the client side. You can tell when the file transfer has ended by checking connection_aborted. However, what is difficult to determine is if it was because the transfer was successful, the client cancelled the transfer, the network disconnected or the transfer failed for some other reason. You might want to Google "php download complete" to see if there are any more robust solutions around.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  22. #12
  23. Plays with fire
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2003
    Location
    Barsoom
    Posts
    1,146
    Rep Power
    144
    Originally Posted by gw1500se
    Oops, sorry. I guess it was too early for me when I replied. What you are asking is a non-trivial exercise since there is no real feedback from the client side. You can tell when the file transfer has ended by checking connection_aborted. However, what is difficult to determine is if it was because the transfer was successful, the client cancelled the transfer, the network disconnected or the transfer failed for some other reason. You might want to Google "php download complete" to see if there are any more robust solutions around.
    Ha. No big deal.

    I've learned this isn't such an easy thing, as you said. Still, there must be a reliable way since other sites have this feature. I did come across this:

    How to detect if file download completed or cancelled - PHP

    Seems like a very complete script and I'll try it out today. My thinking is if all the bytes are sent, they should be received. I know that's specious logic, but the file I need to transfer is small, thereby reducing the opportunities for mishap.

    I didn't even find a decent JQuery solution for this. People suggest running a cron job to delete temp links and files is the way to go.

    Anyway, thanks again for your help.
    “Be ashamed to die until you have won some victory for humanity.” -- Horace Mann

    "...all men are created equal." -- US Declaration of Independence

IMN logo majestic logo threadwatch logo seochat tools logo