我有一个图像上传表单,可以从数据库和服务器中添加、更新和删除图像文件路径。
目前,当我删除图像条目时,该条目会从数据库和服务器文件夹中正确删除。
但是当我更新一个条目时,会在服务器文件夹中创建一个新的文件名,并且旧的图像文件仍然存在。
当我更新时,我希望在数据库中更新新的图像文件,同时取消与服务器的链接。
这是 crud.php 的摘录:
// If the data is not empty
if ((!empty($image['name']) && !empty($title) && !empty($comma_tags))) {
if(!empty($image)) {
$filename = basename($image['name']);
// Get file extension in lower case
$fileType = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
echo $fileType;
// Generate a unique name for the image to prevent throwing exists error
$unique_image_name = rand(10000, 990000) . '_' . time() . '.' . $fileType;
// The path of the new uploaded image
$targetFilePath = $uploadDir . $unique_image_name;
echo $targetFilePath;
if (in_array($fileType, $allowTypes)) {
// Check to make sure the image is valid
if ((!empty($image['tmp_name']) && getimagesize($image['tmp_name']))) {
if (file_exists($unique_image_name)) {
$statusMsg = 'Image already exists, please choose another or rename that image.';
} else if ($image['size'] > 50000) {
$statusMsg = 'Image file size too large, please choose an image less than 500kb.';
} else {
// Everything checks out now we can move the uploaded image
if (move_uploaded_file($image['tmp_name'], $targetFilePath)){
$imgData['filename'] = $unique_image_name;
$imgData['username'] = $_SESSION['username'];
// Create unique viewkey for image 13 characters long
$newViewkey = uniqid();
$imgData['viewkey'] = $newViewkey;
$statusMsg = 'Success! Your image was uploaded.';
$status = 'success';
} else {
$statusMsg = 'Sorry, there was an error uploading your file.';
}
}
}
} else {
$statusMsg = 'Image file is empty or corrupt.';
}
} else {
$statusMsg = 'Sorry, only JPG, JPEG, PNG && GIF files are allowed to be uploaded.';
}
} else {
$statusMsg = 'Sorry something went wrong.';
}
if (!empty($viewkey)) {
// Previous filename
$conditions['where'] = array('viewkey' => $_GET['viewkey'],);
$conditions['return_type'] = 'single';
$prevData = $imageDB->getRows('image', $conditions);
// Update data
$condition = array('viewkey' => $viewkey);
$update = $imageDB->update($tblName, $imgData, $condition);
if($update) {
//Remove old file from server
if (!empty($imgData['filename'])) {
unlink($uploadDir . $prevData['filename']);
}
$status = 'success';
$statusMsg = 'Image data has been updated successfully.';
$sessData['postData'] = '';
$redirectURL = VIEW . 'manage_uploads.php';
} else {
$statusMsg = 'Some problem occurred, please try again.';
//Set redirect url
$redirectURL .= $viewkeyStr;
}
} elseif (!empty($imgData['filename'])) {
// Insert data
$insert = $imageDB->insert($tblName, $imgData);
if ($insert) {
$status = 'success';
$statusMsg = 'Image has been uploaded successfully.';
$sessData['postData'] = '';
$redirectURL = VIEW . 'manage_uploads.php';
} else {
$statusMsg = 'Some problem occurred, please try again.';
}
} else {
$statusMsg = 'All fields are mandatory, please fill all the fields.';
}
// Status message
$sessData['status']['type'] = $status;
$sessData['status']['msg'] = $statusMsg;
} elseif (($_REQUEST['action_type'] == 'delete') && !empty($_GET['viewkey'])) {
// Previous filename
$conditions['where'] = array(
'viewkey' => $_GET['viewkey'],
);
$conditions['return_type'] = 'single';
$prevData = $imageDB->getRows('image', $conditions);
// Delete data
$condition = array(
'viewkey' => $_GET['viewkey']
);
$delete = $imageDB->delete($tblName, $condition);
if ($delete) {
// Remove old file from server
unlink($uploadDir . $prevData['filename']);