Skip to content

Instantly share code, notes, and snippets.

@BBonifield
Created January 14, 2011 05:45
Show Gist options
  • Save BBonifield/779234 to your computer and use it in GitHub Desktop.
Save BBonifield/779234 to your computer and use it in GitHub Desktop.
<?php
if ( isset( $_REQUEST['delete_file'] ) ) {
Manager::factory()->delete_file( $_REQUEST['delete_file'] );
die;
} elseif ( isset( $_REQUEST['as_text'] ) ) {
header('Content-Type: text/plain');
echo file_get_contents( __FILE__ );
die;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<head>
<title>Manage Grabs</title>
<style type="text/css">
* {
font-family: Tahoma, Verdana, Arial, sans-serif;
}
#images {
margin: 0 auto 30px auto;
width: 950px;
}
#images div {
display: inline-block;
width: 304px;
text-align: center;
margin: 5px;
}
#images div img {
max-width: 300px;
max-height: 300px;
border: 2px solid #000;
padding: 2px;
vertical-align: middle;
}
a.delete_file {
display: block;
color: red;
background: #CCCCCC;
padding: 3px;
font-size: 1.1em;
font-weight: bold;
margin-top: 5px;
}
a.load_more_files {
display: block;
width: 950px;
padding: 10px 0;
font-size: 1.5em;
color: blue;
background: #CCCCCC;
text-align: center;
margin: 0 auto;
}
</style>
<link rel="stylesheet" href="/colorbox/colorbox.css" media="screen" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="/colorbox/jquery.colorbox-min.js"></script>
<script type="text/javascript">
$(function(){
$("#images a.colorbox").colorbox();
$("#images a.delete_file").click(function(){
var $link = $(this);
$.get( $link.attr('href'), function(){
$link.closest('div').fadeOut('fast', function(){
$(this).remove();
$("a.load_more_files").attr('href', '?last_file=' + $("#images img:last").attr('src'));
});
});
return false;
});
});
</script>
</head>
<body>
<?php Manager::factory()->display(); ?>
</body>
</html>
<?php
class Manager {
const FILES_PER_PAGE = 21;
public static function factory() {
return new self;
}
public function __construct() {
$this->path = dirname( __FILE__ ) . '/';
}
public function display(){
if ( isset( $_REQUEST['last_file'] ) ) {
$last_file = $_REQUEST['last_file'];
} else {
$last_file = NULL;
}
$files = $this->get_files_to_display( $last_file );
if ( empty( $files ) ) {
echo '<p>No more files bish</p>';
} else {
echo '<div id="images">';
foreach( $files as $file ) {
echo '<div>
<a href="' . $file . '" class="colorbox"><img src="' . $file . '" /></a><br/>
<a href="?delete_file=' . $file . '" class="delete_file">Delete</a>
</div>';
}
echo '</div>';
echo '<a href="?last_file=' . array_pop( $files ) . '" class="load_more_files">Load more files</a></p>';
}
}
private function get_files_to_display( $last_file ){
$cnt = 0;
$add_file_to_array = is_null( $last_file );
$dir = opendir( $this->path );
$files = array();
while( $file = readdir( $dir ) ) {
if ( preg_match( "/\.png$/", $file ) ) {
if ( $add_file_to_array ) {
$files[] = $file;
$cnt++;
if ( $cnt === self::FILES_PER_PAGE ) {
break;
}
} else {
if ( $file === $last_file ) {
$add_file_to_array = TRUE;
}
}
}
}
return $files;
}
public function delete_file( $file ) {
unlink( $this->path . $file );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment