Overwriting exisiting image

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shawnlarrabee
Forum Newbie
Posts: 6
Joined: Mon Apr 17, 2006 10:14 pm

Overwriting exisiting image

Post by shawnlarrabee »

I would like to be able to upload images to my site and display it as a profile pic. I want the new file, however, to overwrite the old so that my pictures don't pile up on my server. Any suggestions? I've got the upload image part working, how bout the overwrite issue?

Shawn
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what have you tried?
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Re: Overwriting exisiting image

Post by jmut »

shawnlarrabee wrote:I would like to be able to upload images to my site and display it as a profile pic. I want the new file, however, to overwrite the old so that my pictures don't pile up on my server. Any suggestions? I've got the upload image part working, how bout the overwrite issue?

Shawn
Assuming that you use some random generator or something to create image names so they are all unique on your server....after uploading the new image you just delete the old one. :roll:
shawnlarrabee
Forum Newbie
Posts: 6
Joined: Mon Apr 17, 2006 10:14 pm

Overwriting existing images...

Post by shawnlarrabee »

Honestly, I'm just getting started with PHP and having gotten much father than creating the upload file page. I can see how it would work, if only I could figure out how to make the PHP save the image to the same file each time. Doing this would keep one picture on the server at a time, unless otherwise coded. I am creating a web-site for a friend who knows nothing about coding, so the page needs to be as self managing as possible. Any suggestions?
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Post by litebearer »

a possible method...

For example purposes the name we want to use is good_name.jpg and the actual name of the new image is new_image.jpg.

1. do your upload
2. use unlink() to delete the file named good_name.jpg
3. use the rename() to rename the file that you uploaded

Code: Select all

<?P

################
# upload and store new_image.jpg
$new_image = "new_image.jpg";
$good_name = "good_name.jpg";

################
# delete the old image

unlink($good_name);

#################
# rename the uploaded file

rename($new_image, $good_name)

?>
shawnlarrabee
Forum Newbie
Posts: 6
Joined: Mon Apr 17, 2006 10:14 pm

Post by shawnlarrabee »

Thanks! I'll give this a try. I appreciate the help.
shawnlarrabee
Forum Newbie
Posts: 6
Joined: Mon Apr 17, 2006 10:14 pm

understanding the code.

Post by shawnlarrabee »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Okay. I am learning this as I go. Below is the code that I've been using to upload files to my server. I found the code online and need help understanding it.  



form.php

[syntax="html"]<html>

<form action="upload.php" method="post" ENCTYPE="multipart/form-data">
File: <input type="file" name="file" size="30"> <input type="submit" value="Upload!">
</form>

</html>
Upload.php[/syntax]

Code: Select all

<head>
<title>Simple file upload script</title>
</head>
<body>

<?php
// ==============
// Configuration
// ==============
$uploaddir = "images"; // Where you want the files to upload to - Important: Make sure this folders permissions is 0777!
// ==============
// Upload Part
// ==============
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
}
print "Your file has been uploaded successfully! Yay!";
?>

</body>
</html>


How can I now incorporate the code posted by litebearer in order to make the new file/image save to the same name?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply