Web Analytics Made Easy -
StatCounter Overwriting image - CodingForum

Announcement

Collapse
No announcement yet.

Overwriting image

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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!

    Comment


    • #3
      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.

      Comment


      • #4
        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.

        Comment


        • #5
          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.

          Comment


          • #6
            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
            CodingForum Supreme Overlord
            All Hail Spookster

            Comment


            • #7
              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.

              Comment


              • #8
                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; Jul 10, 2002, 01: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)

                Comment


                • #9
                  Thanks

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

                  Comment


                  • #10
                    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)

                    Comment


                    • #11
                      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.

                      Comment

                      Working...
                      X