Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-09-2002, 08:32 PM   PM User | #1
Nightfire
Senior Coder

 
Nightfire's Avatar
 
Join Date: Jun 2002
Posts: 4,266
Thanks: 6
Thanked 48 Times in 48 Posts
Nightfire is on a distinguished road
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 
"";
                }
              }
            } 
__________________
Blue Panda
Website Design | 1 Pound Ads | 'ow much? | Coding Geeks
Nightfire is offline   Reply With Quote
Old 07-09-2002, 09:33 PM   PM User | #2
Flamerule
New Coder

 
Join Date: Jun 2002
Location: Paris, France
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Flamerule is an unknown quantity at this point
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!
Flamerule is offline   Reply With Quote
Old 07-09-2002, 11:52 PM   PM User | #3
Nightfire
Senior Coder

 
Nightfire's Avatar
 
Join Date: Jun 2002
Posts: 4,266
Thanks: 6
Thanked 48 Times in 48 Posts
Nightfire is on a distinguished road
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.
__________________
Blue Panda
Website Design | 1 Pound Ads | 'ow much? | Coding Geeks
Nightfire is offline   Reply With Quote
Old 07-10-2002, 12:24 AM   PM User | #4
Mouldy_Goat
Regular Coder

 
Join Date: Jul 2002
Location: London, UK
Posts: 126
Thanks: 0
Thanked 0 Times in 0 Posts
Mouldy_Goat is an unknown quantity at this point
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.
Mouldy_Goat is offline   Reply With Quote
Old 07-10-2002, 03:08 AM   PM User | #5
Nightfire
Senior Coder

 
Nightfire's Avatar
 
Join Date: Jun 2002
Posts: 4,266
Thanks: 6
Thanked 48 Times in 48 Posts
Nightfire is on a distinguished road
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.
__________________
Blue Panda
Website Design | 1 Pound Ads | 'ow much? | Coding Geeks
Nightfire is offline   Reply With Quote
Old 07-10-2002, 04:44 AM   PM User | #6
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,220
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
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
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Old 07-10-2002, 04:49 AM   PM User | #7
Nightfire
Senior Coder

 
Nightfire's Avatar
 
Join Date: Jun 2002
Posts: 4,266
Thanks: 6
Thanked 48 Times in 48 Posts
Nightfire is on a distinguished road
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.
__________________
Blue Panda
Website Design | 1 Pound Ads | 'ow much? | Coding Geeks
Nightfire is offline   Reply With Quote
Old 07-10-2002, 06:30 AM   PM User | #8
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,890
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
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()
__________________
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)

Last edited by firepages; 07-10-2002 at 06:32 AM..
firepages is offline   Reply With Quote
Old 07-10-2002, 05:31 PM   PM User | #9
Nightfire
Senior Coder

 
Nightfire's Avatar
 
Join Date: Jun 2002
Posts: 4,266
Thanks: 6
Thanked 48 Times in 48 Posts
Nightfire is on a distinguished road
Thanks

Thats weird how thie file thing is working. It's overwriting and deleting the images now, but still won't delete a directory.
__________________
Blue Panda
Website Design | 1 Pound Ads | 'ow much? | Coding Geeks
Nightfire is offline   Reply With Quote
Old 07-10-2002, 07:03 PM   PM User | #10
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,890
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
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)
firepages is offline   Reply With Quote
Old 07-10-2002, 07:40 PM   PM User | #11
Ökii
Regular Coder

 
Join Date: Jun 2002
Location: UK
Posts: 577
Thanks: 0
Thanked 0 Times in 0 Posts
Ökii is an unknown quantity at this point
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.
Ökii is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:37 PM.


Advertisement
Log in to turn off these ads.