我试过在这里查看其他帖子,但我无法找到解决我的特定问题的方法。
问题:选择一个或多个复选框后,提交图像没有任何反应,所有表单元素都会消失。
目标:我希望在提交到$ _POST数组时删除所选的图像。另外,我希望重命名剩余的文件(如数组移位)例如:如果要删除6个图像,Image-3和Image-5,则应将图像6重命名为图像5,等等。 / p>
这是我目前的代码:
PHP:
if($_POST['delete_img']){
if ($images){
if (!empty($_POST['delete'])){
$delete = $_POST['delete'];
print_r($delete);
for ($i =(count($delete) - 1); $i >= 0; $i--){
// Determine the images to be deleted
$images_to_delete = "profiles/".$user_id."/".$user_id."-".$delete($i).".jpg";
// Delete the images
echo "Deleting image: " . $i;
recursiveDelete($images_to_delete);
for ($j = $delete($i); $j < $images; $j++){
echo "checkpoint";
// Rename the files
rename("profiles/".$user_id."/".$user_id."-".$j+1 . ".jpg");
// Decrement the images number for the db
$new_images_num = $images-1;
// Update the database
$image_info = array($new_image_num,$user_id);
$result = pg_execute($conn, "update_images", $image_info);
}
}
}
else{
$error .= "You must select an image to delete.";
}
}
else{
echo "There are no images to delete";
}
}
HTML:
<form id="uploadform" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div id="profile_images">
<?php
echo $profile_images;
?>
</div>
<br/>
<strong> Select image to upload: </strong>
<input name="uploadfile" type="file" id="uploadfile" />
<input type="submit" value="Upload" name="upload" />
<input type="submit" value="Delete" name="delete_img" />
<input type="submit" value="Save" name="save" />
</form>
答案 0 :(得分:1)
我认为这是你要做的事情,1)删除任何选定的文件,然后重置所有名称以递增。您必须更改目录名称和命名约定:
function getFileList($dir)
{
// Filter out the inevitable dots..
$filter = array(".","..");
// Scan the target directory
$fileList = scandir($dir);
// Just return false if folder empty
if(empty($fileList))
return false;
// Return filtered array
return array_values(array_diff($fileList,$filter));
}
$dir = __DIR__.'/testimg';
if(!empty($_POST['delete'])) {
foreach($_POST['delete'] as $i => $dVal) {
if(!empty($_POST['delete'][$i])) {
$fName = $dir.'/user'.$i.".jpg";
if(is_file($fName)) {
if(unlink($fName))
echo 'Deleted: '.$fName;
}
}
}
// See if any files remain in folder
$files = getFileList($dir);
// Rename any files in the folder
if($files) {
if(count($files) > 0) {
$i = 1;
foreach($files as $name) {
if(rename($dir."/".$name, $dir.'/user'.$i.".jpg"))
$i++;
}
}
}
}
// Check one last time
$files = getFileList($dir);
?>
<form id="uploadform" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div id="profile_images">
<?php
if($files) {
$i = 1;
foreach($files as $key => $value) {
if(is_file($img = $dir."/".$value)) {
echo '<img src="'.str_replace(__DIR__,"",$img).'" style="max-height: 100px;" />IMAGE '.$i.'<input type="checkbox" name="delete['.$i.']" />';
$i++;
}
}
}
?>
</div>
<br/>
<strong> Select image to upload: </strong>
<input name="uploadfile" type="file" id="uploadfile" />
<input type="submit" value="Upload" name="upload" />
<input type="submit" value="Delete" name="delete_img" />
<input type="submit" value="Save" name="save" />
</form>