• Welcome to the Chevereto user community!

    Here users from all over the world gather around to learn the latest about Chevereto and contribute with ideas to improve the software.

    Please keep in mind:

    • 😌 This community is user driven. Be polite with other users.
    • 👉 Is required to purchase a Chevereto license to participate in this community (doesn't apply to Pre-sales).
    • 💸 Purchase a Pro Subscription to get access to active software support and faster ticket response times.

Simple file manager

Yeah my bad, I based my code on darkmax and I renamed one of his variables, and well, I forgot to change it everywhere, I updated the code.
 
Alright it worked tyvm dark and danny, but there was 1 image on top and the whole row empty so this is what i did :

if ($i % $config['images_per_row'] == 1) { echo '</tr><td></td><tr>'; }


if ($i % $config['images_per_row'] == 0) { echo '</tr><td></td><tr>'; }


i just changed the 1 to 0.
 
deFiant said:
Alright it worked tyvm dark and danny, but there was 1 image on top and the whole row empty so this is what i did :

if ($i % $config['images_per_row'] == 1) { echo '</tr><td></td><tr>'; }


if ($i % $config['images_per_row'] == 0) { echo '</tr><td></td><tr>'; }


i just changed the 1 to 0.

Updated my post thanks ^^ I dont know why I didnt think about it, modulo something should equals 0 not 1 xD
 
You've specified something wrong then, because the images are not showing. Try hard-refreshing if everythingis specified correctly. (ctrl+f5).
 
Also, thank you so much for this release! I've been dying for something like this for 2.0.18.
THANK YOU!

Also, I've been doing some minor modifications to this. I've fixed the design to look better and such.
 
Works great. Few suggestions, though. Confirmation prompt in case of mis-clicks. Also, is it possible to use POST instead of GET?
 
Loco, here is what you requested and I also added the delete request are sent by ajax, so their is no more page reload necessary each time you delete a file.

Also the confirmation is optional. Their is a setting for it.

PHP:
<?php
session_start();
define('access', 'admin');
 
include('includes/chevereto.php');
 
// SETTINGS
$config = array (
    'password' => 'admin123',
    'image_formats' => 'png,jpg,gif',
    'images_per_row' => 8,
    'confirm' => true
);
 
// FUNCTIONS
function deleteFile($path, $type = 'image')
{
    if(unlink($path))
    {
        showMessage($type.' deleted successfully !', true);
    }
    else
    {
        showMessage('Could not delete '.$type.' ('.__CHV_PATH_IMAGES__.$_GET['delete'].')', false);
    }
}
 
function showMessage($msg, $success = true)
{
    $color = ($success) ? 'green' : 'red';
    print('<p style="color:'.$color.';">'.$msg.'</p>');
}
 
// Variables Declaration
$content = '';
$javascript = '';
 
// Login validation
if (isset($_POST['password']) && $_POST['password'] === $config['password'])
{
    $_SESSION['login'] = true;
}
 
// Admin content
if (isset($_SESSION['login']) && $_SESSION['login'])
{
    // Since we want to stay XHTML Valid…
    ob_start();
 
    // ========== DELETE IMAGE START ========== //
    if(isset($_POST['delete']))
    {
        // delete image
        if(file_exists(__CHV_PATH_IMAGES__.$_POST['delete']))
        {
            deleteFile(__CHV_PATH_IMAGES__.$_POST['delete']);
        }
        else
        {
            showMessage('Image does not exist ('.__CHV_PATH_IMAGES__.$_POST['delete'].')', false);
        }
 
        // delete thumb
        if(file_exists(__CHV_PATH_THUMBS__.$_POST['delete']))
        {
            deleteFile(__CHV_PATH_THUMBS__.$_POST['delete'], 'thumb');
        }
        else
        {
            showMessage('Thumb does not exist ('.__CHV_PATH_IMAGES__.$_POST['delete'].')', false);
        }
    }
    // ========== DELETE IMAGE END ========== //
 
    $images = glob("".__CHV_PATH_IMAGES__."*.{".$config['image_formats']."}", GLOB_BRACE);
 
    echo '<table cellspacing="3" cellpadding="0">';
    echo '<tr>';
 
    for ($i = 0; $i < count($images); $i++)
    {
        $name = basename($images[$i]);
 
        if ($i % $config['images_per_row'] == 0) { echo '</tr><td></td><tr>'; }
 
        echo '<td align="center" style="border:1px solid #cccccc;background:#eeeeee;padding:3px;">';
        echo '<a href="./?v='.$name.'" target="_blank">';
        echo '<img src="'.absolute_to_url(__CHV_PATH_THUMBS__.$name).'" style="margin-bottom:2px;border:0px;"></a><br />';
        echo '<a style="font-size:11px;font-family:Arial;text-decoration:none;" class="_delete" alt="'.$name.'" href="#">Delete</a>';
        echo '</td>';
    }
 
    echo '</tr>';
    echo '</table>';
 
    $content = ob_get_contents();
    ob_end_clean();
    
    $javascript = '';
 
}
else
{    
    $content = '
        <form action="#" method="post">
            <p><span style="font-weight: bold;">Password:</span> <input type="password" name="password" /></p>
            <p><input type="button" value="Login" name="login" /></p>
        </form>
    ';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[url]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd[/url]">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>Chevereto Admin</title>
    </head>
 
    <body>
        <?php print($content); ?>
    </body>
</html>
<script type="text/javascript" src="<?php print(absolute_to_url(__CHV_PATH_SYSTEM_JS__)); ?>jquery.min.js"></script>
<script type="text/javascript">
$(function (){
	$("._delete").click(function() {
		var element = $(this);
	
		<?php if($config['confirm']) { print('if (window.confirm("Are you sure you wish do delete this file?")) {'); } ?>
		
			$.post("admin.php", {delete: $(this).attr('alt')}, function() {
					
			}).success(function() {
				$(element).parent().remove();
			}).error(function() {
				alert('For some obscure reason, we could not delete the file');
			});
		
		<?php if($config['confirm']) { print('}'); } ?>
	});
});
</script>
 
Took a lots of modifications, but here it is.

Added a new config for the order

normal = from the first file uploaded to the last one
inverse = from the latest uploaded file to the first one.

PHP:
<?php
session_start();
define('access', 'admin');
 
include('includes/chevereto.php');
 
// SETTINGS
$config = array (
    'password' => 'admin123',
    'image_formats' => array('gif', 'png', 'jpg', 'jpeg'),
    'images_per_row' => 8,
    'confirm' => true,
    'order' => 'inverse' // normal => (From the oldest to the latest) inverse => (from the latest to the oldest)
);
 
// FUNCTIONS
function deleteFile($path, $type = 'image')
{
    if(unlink($path))
    {
        showMessage($type.' deleted successfully !', true);
    }
    else
    {
        showMessage('Could not delete '.$type.' ('.__CHV_PATH_IMAGES__.$_GET['delete'].')', false);
    }
}
 
function showMessage($msg, $success = true)
{
    $color = ($success) ? 'green' : 'red';
    print('<p style="color:'.$color.';">'.$msg.'</p>');
}

function fileExtension($fileName)
{
    return substr($fileName, strrpos($fileName, '.') + 1);
}

function getFiles($allowedExt)
{
    $array = array();
 
    if ($handle = opendir(__CHV_PATH_IMAGES__))
    {
        while (false !== ($file = readdir($handle)))
        {
        	if ($file != '.' && $file != '..' && in_array(fileExtension($file), $allowedExt))
            {
                $array[] = array('name' => $file, 'time' => filemtime(__CHV_PATH_IMAGES__.$file));
            }
        }
        closedir($handle);
    }
 
    return $array;
}

function sortByTimeNormal($a, $b)
{
	if ($a['time'] == $b['time'])
	{
        return 0;
    }
    
    return ($a['time'] < $b['time']) ? -1 : 1;
}

function sortByTimeInverse($a, $b)
{
	if ($a['time'] == $b['time'])
	{
        return 0;
    }
    
    return ($a['time'] < $b['time']) ? 1 : -1;
}

// Variables Declaration
$content = '';
$javascript = '';
 
// Login validation
if (isset($_POST['password']) && $_POST['password'] === $config['password'])
{
    $_SESSION['login'] = true;
}

// Admin content
if (isset($_SESSION['login']) && $_SESSION['login'])
{
    // Since we want to stay XHTML Valid…
    ob_start();
 
    // ========== DELETE IMAGE START ========== //
    if(isset($_POST['delete']))
    {
        // delete image
        if(file_exists(__CHV_PATH_IMAGES__.$_POST['delete']))
        {
            deleteFile(__CHV_PATH_IMAGES__.$_POST['delete']);
        }
        else
        {
            showMessage('Image does not exist ('.__CHV_PATH_IMAGES__.$_POST['delete'].')', false);
        }
 
        // delete thumb
        if(file_exists(__CHV_PATH_THUMBS__.$_POST['delete']))
        {
            deleteFile(__CHV_PATH_THUMBS__.$_POST['delete'], 'thumb');
        }
        else
        {
            showMessage('Thumb does not exist ('.__CHV_PATH_IMAGES__.$_POST['delete'].')', false);
        }
    }
    // ========== DELETE IMAGE END ========== /
    $images = getFiles($config['image_formats']);
    
    if ($config['order'] == 'normal')
   		usort($images, 'sortByTimeNormal');

   	else
		usort($images, 'sortByTimeInverse');
 
    echo '<table cellspacing="3" cellpadding="0">';
    echo '<tr>';
 
    for ($i = 0; $i < count($images); $i++)
    {
       	$name = basename($images[$i]['name']);
 
        if ($i % $config['images_per_row'] == 0) { echo '</tr><td></td><tr>'; }
 
        echo '<td align="center" style="border:1px solid #cccccc;background:#eeeeee;padding:3px;">';
        echo '<a href="./?v='.$name.'" target="_blank">';
        echo '<img src="'.absolute_to_url(__CHV_PATH_THUMBS__.$name).'" style="margin-bottom:2px;border:0px;"></a><br />';
        echo '<span style="font-size:11px;font-family:Arial;text-decoration:none;">'.date('F d, Y H:i:s', $images[$i]['time']).'</span><br />';
        echo '<a style="font-size:11px;font-family:Arial;text-decoration:none;" class="_delete" alt="'.$name.'" href="#">Delete</a>';
        echo '</td>';
    }
 
    echo '</tr>';
    echo '</table>';
 
    $content = ob_get_contents();
    ob_end_clean();
    
    $javascript = '';
 
}
else
{    
    $content = '
        <form action="#" method="post">
            <p><span style="font-weight: bold;">Password:</span> <input type="password" name="password" /></p>
            <p><input type="button" value="Login" name="login" /></p>
        </form>
    ';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[url]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd[/url]">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>Chevereto Admin</title>
    </head>
 
    <body>
        <?php print($content); ?>
    </body>
</html>
<script type="text/javascript" src="<?php print(absolute_to_url(__CHV_PATH_SYSTEM_JS__)); ?>jquery.min.js"></script>
<script type="text/javascript">
$(function (){
	$("._delete").click(function() {
		var element = $(this);
	
		<?php if($config['confirm']) { print('if (window.confirm("Are you sure you wish do delete this file?")) {'); } ?>
		
			$.post("admin.php", {delete: $(this).attr('alt')}, function() {
					
			}).success(function() {
				$(element).parent().remove();
			}).error(function() {
				alert('For some obscure reason, we could not delete the file');
			});
		
		<?php if($config['confirm']) { print('}'); } ?>
	});
});
</script>
 
And IF IS simple to make logout button, ... cuz i got lil bro which know to go on computer and just clicking (that makes him happy lolxD)

if is not simple please, don't waste time.... Thank you :)
 
Create a file and name it logout.php. Add this code to it:

PHP:
<?php

if (!isset($_SESSION)) session_start();

    session_unset();
    session_destroy();
    session_write_close();
    setcookie(session_name(),'',0,'/');
    session_regenerate_id(true);
    header("Location: index.php");

?>

Then add a link to that file by placing:

Code:
echo "<a href='logout.php'>Logout</a>";

in admin.php somewhere, such as above the following line:

Code:
echo '<table cellspacing="3" cellpadding="0">';

This is untested, but it should work. Let me know if it doesn't.
 
Here with the logout button on the top right corner.

As for the pagination, I don't intend to do it... It would require to recode most of the actual code...

PHP:
<?php
session_start();
define('access', 'admin');
 
include('includes/chevereto.php');
 
// SETTINGS
$config = array (
    'password' => 'admin123',
    'image_formats' => array('gif', 'png', 'jpg', 'jpeg'),
    'images_per_row' => 8,
    'confirm' => true,
    'order' => 'inverse' // normal => (From the oldest to the latest) inverse => (from the latest to the oldest)
);
 
// FUNCTIONS
function deleteFile($path, $type = 'image')
{
    if(unlink($path))
    {
        showMessage($type.' deleted successfully !', true);
    }
    else
    {
        showMessage('Could not delete '.$type.' ('.__CHV_PATH_IMAGES__.$_GET['delete'].')', false);
    }
}
 
function showMessage($msg, $success = true)
{
    $color = ($success) ? 'green' : 'red';
    print('<p style="color:'.$color.';">'.$msg.'</p>');
}

function fileExtension($fileName)
{
    return substr($fileName, strrpos($fileName, '.') + 1);
}

function getFiles($allowedExt)
{
    $array = array();
 
    if ($handle = opendir(__CHV_PATH_IMAGES__))
    {
        while (false !== ($file = readdir($handle)))
        {
        	if ($file != '.' && $file != '..' && in_array(fileExtension($file), $allowedExt))
            {
                $array[] = array('name' => $file, 'time' => filemtime(__CHV_PATH_IMAGES__.$file));
            }
        }
        closedir($handle);
    }
 
    return $array;
}

function sortByTimeNormal($a, $b)
{
	if ($a['time'] == $b['time'])
	{
        return 0;
    }
    
    return ($a['time'] < $b['time']) ? -1 : 1;
}

function sortByTimeInverse($a, $b)
{
	if ($a['time'] == $b['time'])
	{
        return 0;
    }
    
    return ($a['time'] < $b['time']) ? 1 : -1;
}

// Variables Declaration
$content = '';
$javascript = '';
 
// Login validation
if (isset($_POST['password']) && $_POST['password'] === $config['password'])
{
    $_SESSION['login'] = true;
}

if (isset($_SESSION['login']) && $_SESSION['login'] && isset($_POST['logout']))
{
	$_SESSION['login'] = false;
}

// Admin content
if (isset($_SESSION['login']) && $_SESSION['login'])
{
    // Since we want to stay XHTML Valid…
    ob_start();
 
    // ========== DELETE IMAGE START ========== //
    if(isset($_POST['delete']))
    {
        // delete image
        if(file_exists(__CHV_PATH_IMAGES__.$_POST['delete']))
        {
            deleteFile(__CHV_PATH_IMAGES__.$_POST['delete']);
        }
        else
        {
            showMessage('Image does not exist ('.__CHV_PATH_IMAGES__.$_POST['delete'].')', false);
        }
 
        // delete thumb
        if(file_exists(__CHV_PATH_THUMBS__.$_POST['delete']))
        {
            deleteFile(__CHV_PATH_THUMBS__.$_POST['delete'], 'thumb');
        }
        else
        {
            showMessage('Thumb does not exist ('.__CHV_PATH_IMAGES__.$_POST['delete'].')', false);
        }
    }
    // ========== DELETE IMAGE END ========== /
    //$images = glob("".__CHV_PATH_IMAGES__."*.{".$config['image_formats']."}", GLOB_BRACE);
    
    $images = getFiles($config['image_formats']);
    
    if ($config['order'] == 'normal')
   		usort($images, 'sortByTimeNormal');

   	else
		usort($images, 'sortByTimeInverse');
		
		
	// Logout button
	print ('<div style="float: right;"><form action="#" method="post"><input type="submit" name="logout" value="Log Out" /></form></div>');
 
    echo '<table cellspacing="3" cellpadding="0">';
    echo '<tr>';
 
    for ($i = 0; $i < count($images); $i++)
    {
       	$name = basename($images[$i]['name']);
 
        if ($i % $config['images_per_row'] == 0) { echo '</tr><td></td><tr>'; }
 
        echo '<td align="center" style="border:1px solid #cccccc;background:#eeeeee;padding:3px;">';
        echo '<a href="./?v='.$name.'" target="_blank">';
        echo '<img src="'.absolute_to_url(__CHV_PATH_THUMBS__.$name).'" style="margin-bottom:2px;border:0px;"></a><br />';
        echo '<span style="font-size:11px;font-family:Arial;text-decoration:none;">'.date('F d, Y H:i:s', $images[$i]['time']).'</span><br />';
        echo '<a style="font-size:11px;font-family:Arial;text-decoration:none;" class="_delete" alt="'.$name.'" href="#">Delete</a>';
        echo '</td>';
    }
 
    echo '</tr>';
    echo '</table>';
 
    $content = ob_get_contents();
    ob_end_clean();
    
    $javascript = '';
 
}
else
{    
    $content = '
        <form action="#" method="post">
            <p><span style="font-weight: bold;">Password:</span> <input type="password" name="password" /></p>
            <p><input type="button" value="Login" name="login" /></p>
        </form>
    ';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[url]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd[/url]">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>Chevereto Admin</title>
    </head>
 
    <body>
        <?php print($content); ?>
    </body>
</html>
<script type="text/javascript" src="<?php print(absolute_to_url(__CHV_PATH_SYSTEM_JS__)); ?>jquery.min.js"></script>
<script type="text/javascript">
$(function (){
	$("._delete").click(function() {
		var element = $(this);
	
		<?php if($config['confirm']) { print('if (window.confirm("Are you sure you wish do delete this file?")) {'); } ?>
		
			$.post("admin.php", {delete: $(this).attr('alt')}, function() {
					
			}).success(function() {
				$(element).parent().remove();
			}).error(function() {
				alert('For some obscure reason, we could not delete the file');
			});
		
		<?php if($config['confirm']) { print('}'); } ?>
	});
});
</script>
 
Back
Top