Page 1 of 1
Overwriting exisiting image
Posted: Mon Apr 17, 2006 10:57 pm
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
Posted: Mon Apr 17, 2006 11:21 pm
by feyd
what have you tried?
Re: Overwriting exisiting image
Posted: Tue Apr 18, 2006 12:36 am
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.

Overwriting existing images...
Posted: Wed Apr 19, 2006 7:11 pm
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?
Posted: Thu Apr 20, 2006 7:22 am
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)
?>
Posted: Thu Apr 20, 2006 11:43 am
by shawnlarrabee
Thanks! I'll give this a try. I appreciate the help.
understanding the code.
Posted: Fri Apr 21, 2006 12:51 pm
by shawnlarrabee
feyd | Please use 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
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]