SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Uploading Photos: Overwriting existing ones

    I've written a PHP script to allow users to upload their photos to our servers.

    They're only going to be allowed to upload 3, so I'm going to rename each picture something like pic1, pic2, pic3 for example.

    Therefore if people decide to update their pictures, I'm going to have to overwrite the file that's on the server with the new picture.

    Can someone tell me how I go about doing this please as currently, the original file still exists.

    Thanks alot,
    Simon

  2. #2
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Presumably your keeping a record of files against users in a DB table ??

    The way I'd do it is only allow three pictures at any one time, if they try to upload a forth, display the three and get them to delete one before allowing the upload, that way the user can choose which picture they want to replace.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  3. #3
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So does that mean that the only way to "overwite" files is to delete what's there first before uploading the new image?

  4. #4
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    No, not at all, its just the way I'd do it.

    If you upload a file with the same name to your server it should overwrite the previous file I believe.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  5. #5
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mandes View Post
    If you upload a file with the same name to your server it should overwrite the previous file I believe.
    Ah ok - so it looks like the issue is a caching one rather than a coding one.

    I have the image being displayed once it has been uploaded - and if you overwrite an image, the page still displays the old picture unless you refresh it.

    Don't suppose there's any way around that is there?

  6. #6
    SitePoint Guru MikeBigg's Avatar
    Join Date
    Jun 2004
    Location
    Reading, UK
    Posts
    970
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cache busting can be done by giving using a unique name in the html reference to the file (often by appending the time), then using some "magic" to reference the real file.

    The magic can be:

    - serving the image 'through' php;
    - using a symbolic link;
    - some mod-rewrite rules.

    Mike

  7. #7
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    or after the upload either autoe refrsh the page or add a header("Location: ...") call to 'redirect' the user back to where they were.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  8. #8
    SitePoint Guru MikeBigg's Avatar
    Join Date
    Jun 2004
    Location
    Reading, UK
    Posts
    970
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    spikez, wil the auto-refresh clear the images from the browser's cache?

  9. #9
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    it will in refresh the page when the image is uploaded so if it is pulled from the database it *should* display the new picture and not the old one. It wont clear the cache though.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  10. #10
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MikeBigg View Post
    spikez, wil the auto-refresh clear the images from the browser's cache?
    Put the full URL in the refresh command, it'll lessen the chance that the browser will fetch from the cache.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  11. #11
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    good point Mandes
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  12. #12
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I have my moments

    Off Topic:


    Congrats on the promotion Spike, probably happened long ago, but I've not been around lately.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  13. #13
    SitePoint Guru MikeBigg's Avatar
    Join Date
    Jun 2004
    Location
    Reading, UK
    Posts
    970
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think it is the browser cache that is causing problems - the only way I know of getting a browser to definately display the new picture is to rename the file.

    If the pictures don't have to be named pic1, pic2, pic3, you can just add one to the highest number used so far when a picture is replaced, and use that file name, deleting the old one at the same time.

    That would solve the browser cache issue.

    Mike

  14. #14
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Mandes View Post
    Off Topic:


    Congrats on the promotion Spike, probably happened long ago, but I've not been around lately.
    Off Topic:


    Thanks! it was a few months ago!!!
    Thought I hadn't seen you around
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •