Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts

    Overwriting image

    I'm back with another uploader question. How can I overwrite the image that is already in the users directory? The user can have either gif or jpg image, but are only allowed one image. So if they first uploaded a jpg, then a gif later on, it shows both the images. How can I get the different file formats to overwrite eachother?

    If the code is in the uploader, here's the upoloader script

    PHP Code:
    $new_home_for_image="../users/$username/";
                if(
    $HTTP_POST_FILES['photo']['tmp_name'])
                {
                  
    $path=$new_home_for_image.$HTTP_POST_FILES['photo']['name'];
                  if(!
    copy($HTTP_POST_FILES['photo']['tmp_name'],$path))
                  {
                   
    copy("nophoto.gif","../users/$username/nophoto.gif");
                  }else{
                  
    $type=array('','.gif','.jpg');
                  
    $bits=getimagesize($path);
                  
    $newname=$username.$type[$bits[2]];
                   if(
    rename($path,str_replace($HTTP_POST_FILES['photo']['name'],$newname,$path)))
                    {
                      echo 
    "";
                    }
                  }
                } 

  • #2
    New Coder
    Join Date
    Jun 2002
    Location
    Paris, France
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The best way would be to delete the old image (if it exists). At least thats the only way I can think of but I don't know much about directory and file functions.
    I don't suffer from insanity, I enjoy every single minute of it!

  • #3
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    I don't know anything about them either. I've been thinking that it may be best to rename the image to the extension that's in the direcetory, but I have no idea how to do that either. The uploader has thrown me off coz I have no idea how it works.

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just scan the directory for any file ending .gif, .jpeg or .jpg. If
    there is one - delete it.

    Then just pop the uploaded image in there.

  • #5
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Would you mind showing me how to do that? If it's a lot of work, then you don't have to show me. I'll try and figure out a new way of doing it.

  • #6
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Do you rename their files once they upload them? Also you might want to make sure the new image successfully uploads before deleting the old one .
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #7
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    It's renamed to their username and then put into the users directory. I think that's how it happens, FirePages was the one that gave me the uploader but I can't figure it out myself.

    I came across a problem earlier though I can't overwrite anything in the directory, nor delete, nor rename. Once the file's there, it's there forever, or until my host deletes it

    I guess I gotta find out how to upload to mysql.

  • #8
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,096
    Thanks
    11
    Thanked 101 Times in 99 Posts
    if your script created the image as indeed the uploader does you should be able to unlink it??

    PHP Code:
    <?
    if(file_exists("../users/$username/$user.gif")){
        
    unlink("../users/$username/$user.gif");
        
    clearstatcache();
    }elseif(
    file_exists("../users/$username/$user.jpg")){
        
    unlink("../users/$username/$user.jpg");
        
    clearstatcache();

    }else{
        
    $nofile_flag=1;
    }
    ?>
    and you need to do that before your upload script starts else you may overwrite the newly uploaded image

    EDIT : added the clearstatcache() as alledgedly the call to file_exists() is cached by PHP and could affect other calls to file_exists()
    Last edited by firepages; 07-10-2002 at 06:32 AM.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #9
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Thanks

    Thats weird how thie file thing is working. It's overwriting and deleting the images now, but still won't delete a directory.

  • #10
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,096
    Thanks
    11
    Thanked 101 Times in 99 Posts
    is the directory empty ? PHP cant delete a directory with contents, you can do it via exec() but that did not seem to work when you tried it?
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #11
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just a lil odd note:

    If you upload a .gif with PHP and then name it ***.jpg it becomes
    a jpeg - transparent colours become white. That's with using
    move_uploaded_file($imagefile,"path/newname.jpg");
    Ökii - formerly pootergeist
    teckis - take your time and it'll save you time.


  •  

    Posting Permissions

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