Fix Delete Image/Video Script PHP
Budget: $10 – $30 USD
I have a PHP script with some javascript...
It displays a list of images that also has videos with the same names... If I select an image, and click go, the script will delete the image and the video file associated with the image.
I have made a change the old directory showed ... "/videos/Needs_Reviewed/*.*" as the file location. I changed it to "/videos/Needs_Reviewed/*/*.*" Because I added a folder between Needs_Reviewed and the Videos/images... the * folder's name changes...
Now it will not delete the image and the video when selected.... This is a simple one page PHP script that has already been developed, this should be a quick change... Here is the code...
<html><head><title>This is a simple PHP script to delete select images.</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<style type="text/css">
.item {
background-color: green;
}
input:checked+.item {
background-color: red;
}
</style>
</head><body bgcolor="red" text="black">
<?php
if (isset($_POST['image_list'])) {
foreach ($_POST['image_list'] as $imagename) {
// delete image by id.
if (file_exists($imagename)) {
unlink($imagename);
/* delete video */
$videoname = substr($imagename, 0, strrpos($imagename, '.'));
unlink($videoname);
/* delete video */
}
}
}
?>
<script type="text/javascript">
function toggle(cb_id) {
var cb = document.getElementById(cb_id);
cb.checked = !cb.checked;
}
</script>
<script type="text/javascript">
$(document).ready(function(){
$('#select_all').on('click',function(){
if(this.checked){
$('.checkbox').each(function(){
this.checked = true;
});
}else{
$('.checkbox').each(function(){
this.checked = false;
});
}
});
$('.checkbox').on('click',function(){
if($('.checkbox:checked').length == $('.checkbox').length){
$('#select_all').prop('checked',true);
}else{
$('#select_all').prop('checked',false);
}
});
});
</script>
<form action="<?PHP $SERVER['PHPSELF'];?>" method="POST"><p>Please select multiple images you want to remove. Please note that the selected images will be removed from server as well.</p><ul class="main"><li><input type="checkbox" id="select_all" /> Select all</li><ul>
<?php
$files = glob("videos/Needs_Reviewed/*/*.*");
for ($i=0; $i<count($files); $i++)
{
$image = $files[$i];
$supported_file = array(
'jpg',
);
$ext = strtolower(pathinfo($image, PATHINFO_EXTENSION));
if (in_array($ext, $supported_file)) {
echo '<li><input type="checkbox" class="checkbox" name="image_list[]" style="margin-right:10px;" id="'.$i.'" value="'.$image.'" /><div class="item">';
echo basename($image)."<br /><br />"; // show only image name if you want to show full path then use this code // echo $image."<br />";
echo '<img src="'.$image .'" style="max-height:600px; max-width: calc(85% - 20px); alt="Random image" onclick="toggle(\''.$i.'\')" /></div></li>'."<br/>";
} else {
continue;
}
}
?>
</ul></ul><input type="submit" name="submit" style="width:300px;height:60px;margin-left:20px;" value="Delete" /></form></body></html>
It displays a list of images that also has videos with the same names... If I select an image, and click go, the script will delete the image and the video file associated with the image.
I have made a change the old directory showed ... "/videos/Needs_Reviewed/*.*" as the file location. I changed it to "/videos/Needs_Reviewed/*/*.*" Because I added a folder between Needs_Reviewed and the Videos/images... the * folder's name changes...
Now it will not delete the image and the video when selected.... This is a simple one page PHP script that has already been developed, this should be a quick change... Here is the code...
<html><head><title>This is a simple PHP script to delete select images.</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<style type="text/css">
.item {
background-color: green;
}
input:checked+.item {
background-color: red;
}
</style>
</head><body bgcolor="red" text="black">
<?php
if (isset($_POST['image_list'])) {
foreach ($_POST['image_list'] as $imagename) {
// delete image by id.
if (file_exists($imagename)) {
unlink($imagename);
/* delete video */
$videoname = substr($imagename, 0, strrpos($imagename, '.'));
unlink($videoname);
/* delete video */
}
}
}
?>
<script type="text/javascript">
function toggle(cb_id) {
var cb = document.getElementById(cb_id);
cb.checked = !cb.checked;
}
</script>
<script type="text/javascript">
$(document).ready(function(){
$('#select_all').on('click',function(){
if(this.checked){
$('.checkbox').each(function(){
this.checked = true;
});
}else{
$('.checkbox').each(function(){
this.checked = false;
});
}
});
$('.checkbox').on('click',function(){
if($('.checkbox:checked').length == $('.checkbox').length){
$('#select_all').prop('checked',true);
}else{
$('#select_all').prop('checked',false);
}
});
});
</script>
<form action="<?PHP $SERVER['PHPSELF'];?>" method="POST"><p>Please select multiple images you want to remove. Please note that the selected images will be removed from server as well.</p><ul class="main"><li><input type="checkbox" id="select_all" /> Select all</li><ul>
<?php
$files = glob("videos/Needs_Reviewed/*/*.*");
for ($i=0; $i<count($files); $i++)
{
$image = $files[$i];
$supported_file = array(
'jpg',
);
$ext = strtolower(pathinfo($image, PATHINFO_EXTENSION));
if (in_array($ext, $supported_file)) {
echo '<li><input type="checkbox" class="checkbox" name="image_list[]" style="margin-right:10px;" id="'.$i.'" value="'.$image.'" /><div class="item">';
echo basename($image)."<br /><br />"; // show only image name if you want to show full path then use this code // echo $image."<br />";
echo '<img src="'.$image .'" style="max-height:600px; max-width: calc(85% - 20px); alt="Random image" onclick="toggle(\''.$i.'\')" /></div></li>'."<br/>";
} else {
continue;
}
}
?>
</ul></ul><input type="submit" name="submit" style="width:300px;height:60px;margin-left:20px;" value="Delete" /></form></body></html>