SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Strokin' Morango dele454's Avatar
    Join Date
    Oct 2005
    Location
    Cape town, South Africa
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Replacing images with PHP

    Hi,

    I was thinking if i need to replace an image on the server with an uploaded version, does it follow the same fopen+fwrite functions calls like writing/appending a string to a txt file?

    My other option is to delete the existing file and replace it with the uploaded version with the same name but i want to know if one cant simply replace it rather than deleting+replacing.

    I cant seem to find any clues on the internet.

    Thanks

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    if you open the files using fopen be sure to do so using binary mode.
    http://us.php.net/manual/en/function.fopen.php
    $fo = fopen( 'image.jpg', 'br' );

    However, for binary data like an image it is best to just overwrite the old image with the new one.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    Strokin' Morango dele454's Avatar
    Join Date
    Oct 2005
    Location
    Cape town, South Africa
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Logic_Earth

  4. #4
    Strokin' Morango dele454's Avatar
    Join Date
    Oct 2005
    Location
    Cape town, South Africa
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    writing to the jpg file doesnt work as expected. The file size of the replaced image drops to 13bytes and image cannot be previewed.

    I have switched to delete the existing image and replacing with a new one - just seems simpler and less hassling.

    Thanks

  5. #5
    Strokin' Morango dele454's Avatar
    Join Date
    Oct 2005
    Location
    Cape town, South Africa
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just read in the documentation that move_uploaded_file function overwrites the file with the same name if it does exist.

    http://uk.php.net/move_uploaded_file


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
  •