Overwriting exisiting image
Moderator: General Moderators
-
shawnlarrabee
- Forum Newbie
- Posts: 6
- Joined: Mon Apr 17, 2006 10:14 pm
Overwriting exisiting image
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
Shawn
Re: Overwriting exisiting image
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.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
-
shawnlarrabee
- Forum Newbie
- Posts: 6
- Joined: Mon Apr 17, 2006 10:14 pm
Overwriting existing images...
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
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
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
-
shawnlarrabee
- Forum Newbie
- Posts: 6
- Joined: Mon Apr 17, 2006 10:14 pm
understanding the code.
feyd | Please use
Upload.php[/syntax]
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]
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>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]