Jump to content


This topic is now archived and is closed to further replies.


Delete file referenced by path

Recommended Posts

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?

$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);
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>";

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.