Reputation: 23
I have tried looking through other posts here but I haven't been able to find a solution to my particular problem.
Issue: After selecting a checkbox or more and submitting nothing happens to the images and all form elements disappear.
Goal: I want the images selected to be delete when submitted to the $_POST array. Additionally, I want the remaining files to be renamed (like an array shift) For example: If I have 6 images, Image-3, and Image-5 to be deleted, image 6 should be renamed to image 5, and etc.
Here is my current code:
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>
Upvotes: 1
Views: 647
Reputation: 12505
I think this is what you are trying to do which is 1) delete any selected file(s), then reset all the names to incrementing. You will have to change the directory names and naming conventions:
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>
Upvotes: 1