Jump to content

Make delete button functional to delete image in said row and sort image name by date uploaded


Recommended Posts

Hello,

I am using a simple custom ShareX image uploader script that has a little dashboard included where I can see the image info that I just screenshotted and uploaded to my host. However, I am looking to make 2 changes to the script but I'm not sure how to approach them.

1. In the screenshot, I would like to make those Delete buttons that I added functional. When I screenshot something, the image name is randomly generated so creating a button to delete a specific filename won't work. I just want to go to a row and hit Delete and it'll delete that image from my host (its designated directory). I've attached my codes below.

2. In the screenshot, the images are being sorted by the first number chronologically making them be out of order under the Image Date column. I am wanting them to be displayed by Image Date based on the most recent one taken at the top. I've attached my codes below.

config.php (config script)

<?php

date_default_timezone_set("America/Chicago");

return array(
    'secure_key' => 'my_key',

    'output_url' => 'http://my_url.com/i/',

    'redirect_url' => 'http://my_url.com/i/',

    'allowed_ips' => array(),

    'page_title' => 'my_page_title',

    'heading_text' => 'my_header_text',
);

upload.php (upload script)

<?php
$config = include('i/config.php');

$key = $config['secure_key'];
$uploadhost = $config['output_url'];
$redirect = $config['redirect_url'];

if ($_SERVER["REQUEST_URI"] == "/robot.txt") { die("User-agent: *\nDisallow: /"); }
 
if (isset($_POST['key'])) {
    if ($_POST['key'] == $key) {
        $parts = explode(".", $_FILES["d"]["name"]);
        $target = getcwd() . "/i/" . $_POST['name'] . "." . end($parts);
        if (move_uploaded_file($_FILES['d']['tmp_name'], $target)) {
            $target_parts = explode("/i/", $target);
            echo $uploadhost . end($target_parts);
        } else {
            echo "Sorry, there was a problem uploading your file. (Ensure your directory has 777 permissions)";
        }
    } else {
        header('Location: '.$redirect);
    }
} else {
    header('Location: '.$redirect);
}
?>

index.php (dashboard page)

<?php $config = include('config.php'); ?>
<html>
	<head>
		<link href="css/main.css" rel="stylesheet" type="text/css"/>
		<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
		<link href="https://cdn.datatables.net/1.10.9/css/dataTables.bootstrap.min.css" rel="stylesheet" type="text/css"/>
		<title><?php echo $config['page_title'];?></title>
	</head>
	<body style="overflow:hidden;">
		<div class="container main_container">
			<h3><b><?php echo $config['heading_text'];?><br></b></h3>

			<?php 
			    $si_prefix = array( 'B', 'KB', 'MB', 'GB', 'TB', 'EB', 'ZB', 'YB' );
			    $base = 1024;

			    $bytes = disk_free_space("/"); 
			    $class = min((int)log($bytes , $base) , count($si_prefix) - 1);
			    echo "Free Space: ";
			    echo sprintf('%1.2f' , $bytes / pow($base,$class)) . ' ' . $si_prefix[$class] . ' / ';

			    $bytes = disk_total_space("/");
			    $class = min((int)log($bytes , $base) , count($si_prefix) - 1);
			    echo sprintf('%1.2f' , $bytes / pow($base,$class)) . ' ' . $si_prefix[$class] . '<br />';
			?>
			<br>
			<?php if(empty($config['allowed_ips']) || in_array($_SERVER['REMOTE_ADDR'], $config['allowed_ips'])){?>
					<?php
					$ignore = Array("index.php", "js", "css", ".", "..", "gallery.php", "img", "upload.php", "config.php");
					$files1 = scandir(".");
					?>
				<br>
				<table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
			        <thead>
			            <tr>
			                <th>Image Name</th>
			                <th>Image Size</th>
			                <th>Image Type</th>
			                <th>Image Date</th>
			                <th>Manage Image</th>
			            </tr>
			        </thead>
			 
			        <tbody>
			        	<?php foreach($files1 as $file){
			        		if(!in_array($file, $ignore)){?>
			            <tr>
			                <td><a target="_blank" href="<?php echo $config['output_url'];?><?php echo($file);?>"><?php echo($file);?></a></td>
			                <td><?php echo filesize($file);?></td>
			                <td><?php echo pathinfo($file, PATHINFO_EXTENSION);?></td>
			                <td><?php echo date ("M d Y h:i A", filemtime($file))?></td>
			                <td><button type="button" class="btn btn-danger">Delete</button></td>
			            </tr>
			            <?php } }?>
			        </tbody>
			    </table>
			<?php }?>
		</div>
		<script src="//code.jquery.com/jquery-1.11.3.min.js" type="text/javascript"></script>
		<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js" type="text/javascript"></script>
		<script src="https://cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js" type="text/javascript"></script>
		<script src="https://cdn.datatables.net/1.10.9/js/dataTables.bootstrap.min.js" type="text/javascript"></script>
		<script src="js/main.js" type="text/javascript"></script>
	</body>
</html>

 

9k93Q9JQ.png

You can add a usort function, which sorts the $files1 array based on the modification time of each file. Something like this:

usort($files1, function($a, $b) {
    return filemtime($b) - filemtime($a);
});

I would also recommend using htmlspecialchars for added security.

Hope this helps.

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.