• 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

Lautaro

👽 Chevereto Freak
Hey guys,

Here's a very simple file manager i just coded. This will get all the images from your images and thumbs folder and you'll be able to delete any with a single click.

When you delete an image it will also delete the thumb. If the image or the thumb doesn't exist, it will delete whatever exists (the image or the thumb) if none exist, well, you will get a message telling you that none exist and could not delete them.

First, create a new php file where your index.php is. I called mine admin.php

Add the following code inside the newly created php file:
PHP:
<?php

// SETTINGS
$config['admin_ip'] = '111.111.111.111';
$config['images_dir'] = 'images/';
$config['thumbs_dir'] = 'images/thumbs/';
$config['image_formats'] = 'png,jpg,gif,bmp';
$config['images_per_row'] = '8';


/* check admin ip */
if($_SERVER['REMOTE_ADDR'] != $config['admin_ip']) { die('you do not have access to this file.'); }


// ========== DELETE IMAGE START ========== //
if(isset($_REQUEST['delete'])) {

/* check if image exists */
if(!file_exists($config['images_dir'].$_REQUEST['delete'])) {
echo '<span style="color:red;">image does not exist ('.$config['images_dir'].$_REQUEST['delete'].')</span><br />';
$chk_image = 1;
}

/* check if image's thumb exists */
if(!file_exists($config['thumbs_dir'].$_REQUEST['delete'])) {
echo '<span style="color:red;">thumb does not exist ('.$config['thumbs_dir'].$_REQUEST['delete'].')</span><br />';
$chk_thumb = 1;
}


/* delete image */
if($chk_image != 1) {
if(!@unlink($config['images_dir'].$_REQUEST['delete'])) {
/* it worked */
echo 'could not delete image ('.$config['images_dir'].$_REQUEST['delete'].')<br />';
} else {
/* could not delete, display error */
echo '<span style="color:green;">image deleted successfully !</span><br />';
}
}

/* delete thumb */
if($chk_thumb != 1) {
if(!@unlink($config['thumbs_dir'].$_REQUEST['delete'])) { 
/* it worked */
echo 'could not delete thumb ('.$config['images_dir'].$_REQUEST['delete'].')<br />';
} else {
/* could not delete, display error */
echo '<span style="color:green;">thumb deleted successfully !</span><br />';
}
}

}
// ========== DELETE IMAGE END ========== //

echo '<table cellspacing="3" cellpadding="0">';
echo '<tr>';
$cnt = 0;
foreach (glob("".$config['images_dir']."*.{".$config['image_formats']."}", GLOB_BRACE) as $filename) 
{
$image_name = explode($config['images_dir'],$filename);

$cnt++;
if($cnt % $config['images_per_row'] == 1) { echo '</tr><td></td><tr>'; }
	
echo '<td align="center" style="border:1px solid #cccccc;background:#eeeeee;padding:3px;">';
echo '<a href="./?v='.$image_name[1].'" target="_blank">';
echo '<img src="'.$config['thumbs_dir'].$image_name[1].'" style="margin-bottom:2px;border:0px;"></a><br />';
echo '<a style="font-size:11px;font-family:Arial;text-decoration:none;" href="?delete='.$image_name[1].'">delete</a>';
echo '</td>';

}
echo '</tr>';
echo '</table>';
?>

Make sure you edit the settings:
PHP:
// SETTINGS
$config['admin_ip'] = '111.111.111.111';
$config['images_dir'] = 'images/';
$config['thumbs_dir'] = 'images/thumbs/';
$config['image_formats'] = 'png,jpg,gif,bmp';
$config['images_per_row'] = '8';

you will only be able to access the file through the IP you set in the settings.

Here's a preview:
hisvo.jpg


If you purchased my login/database MOD this will not remove the images from the database, if you want this to also remove images from the database you need to:

1.- include my db connection file.
PHP:
include('includes/dmx.sql.php');

2.- add the following query below the "/* it worked */" comment:
PHP:
@mysql_query("DELETE FROM uploaded_images WHERE file = '".mysql_real_escape_string($_REQUEST['delete'])."'");

happy valentine's day !
 
Hey, a saw a few errors in your code. I listed them below.

First of all, making your own config for the directory when you can simply include config.php is a little bit repetitive and more complex for the users of your mod.

Then here is some bad coding practices...
  • Using @ before a function is not recommended, you should learn to use the try{} catch() {} exception handler.[/*]
  • MySQL requests using the mysql_... functions is very old, theses functions were made for PHP 4 and MySQL 3, you should try the new oriented-object way of doing it. PDO. It is way more secure, and faster.[/*]
  • Using $_REQUEST when you know what information you will receive ($_GET or $_POST) is then again, not recommended because it become easier for an hacker to inject do malicious works. (Kinda hard in your case, but well, it is a suggestion)[/*]
  • Using a foreach in a loop where you need a counter is again not a good idea. Using a for in your case would be more appropriate.[/*]
  • You often user an int to do the job of a boolean.[/*]

In php a integer of 1 is often considered as a boolean, so when you do

PHP:
if ($chk_image != 1)

it is like saying if value not equals to true, when you could do if not true :

PHP:
if (!$chk_image)

Yet, i'm just making suggestions.


Finally not declaring a variable and starting to use it as an array (like your $config var) ... in some versions of php it will create a warning, not visible by the user, but still logged.

You should definitely correct this, but anyway good job on this mod.

Also, to let you know, chevereto transform every bmp images as an png. So a glob for bmp is not necessary.
 
Just learn that unlink is a old php code and doesnt throw exception... so my first point is not useful...
 
Much respect to both of you guys. Dark for the free mod and Danny for helping a fellow coder out.
 
Here is how I would have done it... Tested and working =P

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
);
 
// 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>');
}

// 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($_GET['delete']))
    {
        // delete image
        if(file_exists(__CHV_PATH_IMAGES__.$_GET['delete']))
        {
            deleteFile(__CHV_PATH_IMAGES__.$_GET['delete']);
        }
        else
        {
            showMessage('Image does not exist ('.__CHV_PATH_IMAGES__.$_GET['delete'].')', false);
        }
 
        // delete thumb
        if(file_exists(__CHV_PATH_THUMBS__.$_GET['delete']))
        {
            deleteFile(__CHV_PATH_THUMBS__.$_GET['delete'], 'thumb');
        }
        else
        {
            showMessage('Thumb does not exist ('.__CHV_PATH_IMAGES__.$_GET['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;" href="?delete='.$name.'">delete</a>';
        echo '</td>';
    }
 
    echo '</tr>';
    echo '</table>';
 
    $content = ob_get_contents();
    ob_end_clean();
 
}
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>
 
damn, its giving me white page... Im not sure where i can find my exact ip adress, its not showing in cmd
+ at dannys code i got this error:

Parse error: syntax error, unexpected T_BOOLEAN_AND, expecting ',' or ')' in /home/egaehdea/public_html/demo/admin.php on line 39
 
Mcmar said:
damn, its giving me white page... Im not sure where i can find my exact ip adress, its not showing in cmd
+ at dannys code i got this error:

Parse error: syntax error, unexpected T_BOOLEAN_AND, expecting ',' or ')' in /home/egaehdea/public_html/demo/admin.php on line 39

Forgot to close a (), fixed. Tell me if their is another problems

for your white page problem thing, you should active debbuging in config.php, otherwise it wont show errors.

and your ip is : http://www.whatismyip.com/
 
Parse error: syntax error, unexpected T_STRING in /home/srkiller/public_html/demo/admin.php on line 105

at yours danny :/, altso where to allow debbuging >_> , im at config, but see no search for Debugging lol, sos im a noob at this
 
PHP:
/**
 * error_reporting
 * Switch for enable/disable the PHP error reporting
 * default: false
 */
$config['error_reporting'] = false; // Values: true|false

change false to true

PHP:
$config['error_reporting'] = true;

Btw, did you add codes into the admin file... because line 105 is pure html...
 
I get the same error.

[15-Feb-2012 18:14:49] PHP Parse error: syntax error, unexpected T_STRING in /home/public_html/sha/admin.php on line 105
 
Omg... I edited it to true, and still give you white page. When i take wrong ip, it give you the wrong ip error, and when i used your script, gives 105 error.
 
All errors has been fixed, and I also tested it =P

You can edit any second parameters that is in the $config => array after the // SETTINGS comment

PHP:
// SETTINGS
$config = array (
    'password' => 'can-edit',
    'image_formats' => 'png,jpg,gif',
    'images_per_row' => 66666666
);
 
deFiant said:
Danny.Domb said:
All errors has been fixed, and I also tested it =P

Confirmed it works,

hey danny how can you make it show downwards instead of sideways ?

I dont understand what you mean by showing it downards instead of sideways?
 
Back
Top