Jump to content

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?

<?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>";
?>
Link to comment
https://forums.phpfreaks.com/topic/4205-delete-file-referenced-by-path/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.