Jump to content


Photo

Delete file referenced by path


  • Please log in to reply
No replies to this topic

#1 wpmills

wpmills
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 06 March 2006 - 01:12 PM

I use the following code to upload photos for a talent agency site.
What I want to know is how to alter it to allow me to delete the old file if a new one is added. At the moment all that happens is the new path is written to the database and the new image copied over. The old image remains.

Any suggestions?

<?php
$talent_id = $_GET['talent_id'];
$numoffile = 5;

$file_dir = "../images/uploads/";
if ($_POST) {
for ($i=0;$i<$numoffile;$i++) {
if (trim($_FILES['myfiles']['name'][$i])!="") {
$newfile = $file_dir.md5(rand() * time()).' - '.$_FILES['myfiles']['name'][$i];
move_uploaded_file($_FILES['myfiles']['tmp_name'][$i], $newfile);
$j++;
include './includes/dbconnect.php';
$query = "UPDATE talent_info SET image_path$i=coalesce('$newfile') WHERE talent_id = '$talent_id'";
$result = mysql_query($query) or die(mysql_error());
}
}
}
if (isset($j)&&$j>0) print "Your file(s) has been uploaded. Return to<a href='./add_talent.php'>Add Talent</a>.<br>";
print "<form method='post' enctype='multipart/form-data'>";
for($i=0;$i<$numoffile;$i++) {
print "Image $i<input type='file' name='myfiles[]' size='30'><br>";
}
print "<input type='submit' name='action' value='Upload'>";
print "</form>";
?>




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users