Delete files online

Discussion in 'PHP' started by hrdmn, Jun 10, 2007.

  1. #1
    Hi again,
    i wanna do something, but do not know actually how to manage it.

    I've got a DIR listing script running for a page with student materials that helps me and my colleagues to manage true the academic year.

    So far the script allows us to browse and upload new files, but I'd like to add a delete function too.

    <?
    /*
    Directory Listing Script - Version 2
    ====================================
    Script Author: Ash Young <ash@evoluted.net>. www.evoluted.net
    Layout: Manny <manny@tenka.co.uk>. www.tenka.co.uk
    
    REQUIREMENTS
    ============
    This script requires PHP and GD2 if you wish to use the 
    thumbnail functionality.
    
    INSTRUCTIONS
    ============
    1) Unzip all files 
    2) Edit this file, making sure everything is setup as required.
    3) Upload to server
    4) ??????
    5) Profit!
    
    CONFIGURATION
    =============
    Edit the variables in this section to make the script work as
    you require.
    
    Start Directory - To list the files contained within the current 
    directory enter '.', otherwise enter the path to the directory 
    you wish to list. The path must be relative to the current 
    directory.
    */
    $startdir = '.';
    
    /*
    Show Thumbnails? - Set to true if you wish to use the 
    scripts auto-thumbnail generation capabilities.
    This requires that GD2 is installed.
    */
    $showthumbnails = true; 
    
    /*
    Show Directories - Do you want to make subdirectories available?
    If not set this to false
    */
    $showdirs = true;
    
    /* 
    Force downloads - Do you want to force people to download the files
    rather than viewing them in their browser?
    */
    $forcedownloads = false;
    
    /*
    Hide Files - If you wish to hide certain files or directories 
    then enter their details here. The values entered are matched
    against the file/directory names. If any part of the name 
    matches what is entered below then it is now shown.
    */
    $hide = array(
    				'dlf',
    				'pass_rotate',
    				'index.php',
    				'pass_pr.php',
    				'Thumbs',
    				'.htaccess',
    				'.htpasswd',
    				'indexup.php',
    				'upload'
    			);
    			 
    /* 
    Show index files - if an index file is found in a directory
    to you want to display that rather than the listing output 
    from this script?
    */			
    $displayindex = false;
    
    /*
    Allow uploads? - If enabled users will be able to upload 
    files to any viewable directory. You should really only enable
    this if the area this script is in is already password protected.
    */
    $allowuploads = false;
    
    /*
    Overwrite files - If a user uploads a file with the same
    name as an existing file do you want the existing file
    to be overwritten?
    */
    $overwrite = false;
    
    /*
    Index files - The follow array contains all the index files
    that will be used if $displayindex (above) is set to true.
    Feel free to add, delete or alter these
    */
    
    $indexfiles = array (
    				'index.html',
    				'index.htm',
    				'default.htm',
    				'default.html'
    			);
    			
    /*
    File Icons - If you want to add your own special file icons use 
    this section below. Each entry relates to the extension of the 
    given file, in the form <extension> => <filename>. 
    These files must be located within the dlf directory.
    */
    $filetypes = array (
    				'png' => 'jpg.gif',
    				'jpeg' => 'jpg.gif',
    				'bmp' => 'jpg.gif',
    				'jpg' => 'jpg.gif', 
    				'gif' => 'gif.gif',
    				'zip' => 'archive.png',
    				'rar' => 'archive.png',
    				'exe' => 'exe.gif',
    				'setup' => 'setup.gif',
    				'txt' => 'text.png',
    				'htm' => 'html.gif',
    				'html' => 'html.gif',
    				'fla' => 'fla.gif',
    				'swf' => 'swf.gif',
    				'xls' => 'xls.gif',
    				'doc' => 'doc.gif',
    				'sig' => 'sig.gif',
    				'fh10' => 'fh10.gif',
    				'pdf' => 'pdf.gif',
    				'psd' => 'psd.gif',
    				'rm' => 'real.gif',
    				'mpg' => 'video.gif',
    				'mpeg' => 'video.gif',
    				'mov' => 'video2.gif',
    				'avi' => 'video.gif',
    				'eps' => 'eps.gif',
    				'gz' => 'archive.png',
    				'asc' => 'sig.gif',
    				'php' => 'text.png',
    				'mp3' => 'mp3.gif',
    				'ppt' => 'powerpoint.gif',
    			);
    			
    /*
    That's it! You are now ready to upload this script to the server.
    
    Only edit what is below this line if you are sure that you know what you
    are doing!
    */
    error_reporting(0);
    if(!function_exists('imagecreatetruecolor')) $showthumbnails = false;
    $leadon = $startdir;
    if($leadon=='.') $leadon = '';
    if((substr($leadon, -1, 1)!='/') && $leadon!='') $leadon = $leadon . '/';
    $startdir = $leadon;
    
    if($_GET['dir']) {
    	//check this is okay.
    	
    	if(substr($_GET['dir'], -1, 1)!='/') {
    		$_GET['dir'] = $_GET['dir'] . '/';
    	}
    	
    	$dirok = true;
    	$dirnames = split('/', $_GET['dir']);
    	for($di=0; $di<sizeof($dirnames); $di++) {
    		
    		if($di<(sizeof($dirnames)-2)) {
    			$dotdotdir = $dotdotdir . $dirnames[$di] . '/';
    		}
    		
    		if($dirnames[$di] == '..') {
    			$dirok = false;
    		}
    	}
    	
    	if(substr($_GET['dir'], 0, 1)=='/') {
    		$dirok = false;
    	}
    	
    	if($dirok) {
    		 $leadon = $leadon . $_GET['dir'];
    	}
    }
    
    if($_GET['download'] && $forcedownloads) {
    	$file = str_replace('/', '', $_GET['download']);
    	$file = str_replace('..', '', $file);
    
    	if(file_exists($leadon . $file)) {
    		header("Content-type: application/x-download");
    		header("Content-Length: ".filesize($leadon . $file)); 
    		header('Content-Disposition: attachment; filename="'.$file.'"');
    		readfile($leadon . $file);
    		die();
    	}
    }
    
    if($allowuploads && $_FILES['file']) {
    	$upload = true;
    	if(!$overwrite) {
    		if(file_exists($leadon.$_FILES['file']['name'])) {
    			$upload = false;
    		}
    	}
    	
    	if($upload) {
    		move_uploaded_file($_FILES['file']['tmp_name'], $leadon . $_FILES['file']['name']);
    	}
    }
    
    $opendir = $leadon;
    if(!$leadon) $opendir = '.';
    if(!file_exists($opendir)) {
    	$opendir = '.';
    	$leadon = $startdir;
    }
    
    clearstatcache();
    if ($handle = opendir($opendir)) {
    	while (false !== ($file = readdir($handle))) { 
    		//first see if this file is required in the listing
    		if ($file == "." || $file == "..")  continue;
    		$discard = false;
    		for($hi=0;$hi<sizeof($hide);$hi++) {
    			if(strpos($file, $hide[$hi])!==false) {
    				$discard = true;
    			}
    		}
    		
    		if($discard) continue;
    		if (@filetype($leadon.$file) == "dir") {
    			if(!$showdirs) continue;
    		
    			$n++;
    			if($_GET['sort']=="date") {
    				$key = @filemtime($leadon.$file) . ".$n";
    			}
    			else {
    				$key = $n;
    			}
    			$dirs[$key] = $file . "/";
    		}
    		else {
    			$n++;
    			if($_GET['sort']=="date") {
    				$key = @filemtime($leadon.$file) . ".$n";
    			}
    			elseif($_GET['sort']=="size") {
    				$key = @filesize($leadon.$file) . ".$n";
    			}
    			else {
    				$key = $n;
    			}
    			$files[$key] = $file;
    			
    			if($displayindex) {
    				if(in_array(strtolower($file), $indexfiles)) {
    					header("Location: $file");
    					die();
    				}
    			}
    		}
    	}
    	closedir($handle); 
    }
    
    //sort our files
    if($_GET['sort']=="date") {
    	@ksort($dirs, SORT_NUMERIC);
    	@ksort($files, SORT_NUMERIC);
    }
    elseif($_GET['sort']=="size") {
    	@natcasesort($dirs); 
    	@ksort($files, SORT_NUMERIC);
    }
    else {
    	@natcasesort($dirs); 
    	@natcasesort($files);
    }
    
    //order correctly
    if($_GET['order']=="desc" && $_GET['sort']!="size") {$dirs = @array_reverse($dirs);}
    if($_GET['order']=="desc") {$files = @array_reverse($files);}
    $dirs = @array_values($dirs); $files = @array_values($files);
    
    
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1251" />
    <title>Екоикономика</title>
    <link rel="stylesheet" type="text/css" href="dlf/styles.css" />
    <?
    if($showthumbnails) {
    ?>
    <script language="javascript" type="text/javascript">
    <!--
    function o(n, i) {
    	document.images['thumb'+n].src = 'dlf/i.php?f='+i;
    
    }
    
    function f(n) {
    	document.images['thumb'+n].src = 'dlf/trans.gif';
    }
    //-->
    </script>
    
    <script language="javascript" type="text/javascript">
    function addBookmark(title,url) {
    if (window.sidebar) { 
    window.sidebar.addPanel(title, url,""); 
    } else if( document.all ) {
    window.external.AddFavorite( url, title);
    } else if( window.opera && window.print ) {
    return true;
    }
    }
    </script>
    
    <?
    }
    ?>
    </head>
    <body>
    <div id="container">
      <h1>Специалност "Екоикономика", Отрасловоикономически факултет</h1>
      <div id="breadcrumbs">
    
    <a href="<?=$_SERVER['PHP_SELF'];?>"><img src="dlf/home.gif" style="border:0" /></a>&nbsp;
    <a href="#" onmousedown="addBookmark('УНСС - Учебни материали','http://hardman.stringendo.org/unwe/')" onmouseover="return overlib('Bookmark us.')" onmouseout="return  overlib('UNWE',DELAY, 1000);"><img src="dlf/fav.gif" style="border:0" /></a>&nbsp;
    <a href="<?=$_SERVER['PHP_SELF'];?>"><img src="dlf/refresh.gif" style="border:0" /></a>&nbsp;
    <a href="upload.php"><img src="dlf/upload.gif" style="border:0" /></a>
    
      <?
     	 $breadcrumbs = split('/', $leadon);
      	if(($bsize = sizeof($breadcrumbs))>0) {
      		$sofar = '';
      		for($bi=0;$bi<($bsize-1);$bi++) {
    			$sofar = $sofar . $breadcrumbs[$bi] . '/';
    			echo ' &gt; <a href="'.$_SERVER['PHP_SELF'].'?dir='.urlencode($sofar).'">'.$breadcrumbs[$bi].'</a>';
    		}
      	}
      
    	$baseurl = $_SERVER['PHP_SELF'] . '?dir='.$_GET['dir'] . '&amp;';
    	$fileurl = 'sort=name&amp;order=asc';
    	$sizeurl = 'sort=size&amp;order=asc';
    	$dateurl = 'sort=date&amp;order=asc';
    	
    	switch ($_GET['sort']) {
    		case 'name':
    			if($_GET['order']=='asc') $fileurl = 'sort=name&amp;order=desc';
    			break;
    		case 'size':
    			if($_GET['order']=='asc') $sizeurl = 'sort=size&amp;order=desc';
    			break;
    			
    		case 'date':
    			if($_GET['order']=='asc') $dateurl = 'sort=date&amp;order=desc';
    			break;  
    		default:
    			$fileurl = 'sort=name&amp;order=desc';
    			break;
    	}
      ?>
      </div>
      <div id="listingcontainer">
        <div id="listingheader"> 
    	<div id="headerfile"><a href="<?=$baseurl . $fileurl;?>">Папка / Фаил</a></div>
    	<div id="headersize"><a href="<?=$baseurl . $sizeurl;?>">Големина</a></div>
    	<div id="headermodified"><a href="<?=$baseurl . $dateurl;?>">Последна промяна</a></div>
    	</div>
        <div id="listing">
    	<?
    	$class = 'b';
    	if($dirok) {
    	?>
    	<div><a href="<?=$_SERVER['PHP_SELF'].'?dir='.urlencode($dotdotdir);?>" class="<?=$class;?>"><img src="dlf/dirup.png" alt="Folder" /><strong>..</strong> <em>-</em> <?=date ("M d Y h:i:s A", filemtime($dotdotdir));?></a></div>
    	<?
    		if($class=='b') $class='w';
    		else $class = 'b';
    	}
    	$arsize = sizeof($dirs);
    	for($i=0;$i<$arsize;$i++) {
    	?>
    	<div><a href="<?=$_SERVER['PHP_SELF'].'?dir='.urlencode($leadon.$dirs[$i]);?>" class="<?=$class;?>"><img src="dlf/folder.png" alt="<?=$dirs[$i];?>" /><strong><?=$dirs[$i];?></strong> <em>-</em> <?=date ("M d Y h:i:s A", filemtime($leadon.$dirs[$i]));?></a></div>
    	<?
    		if($class=='b') $class='w';
    		else $class = 'b';	
    	}
    	
    	$arsize = sizeof($files);
    	for($i=0;$i<$arsize;$i++) {
    		$icon = 'unknown.png';
    		$ext = strtolower(substr($files[$i], strrpos($files[$i], '.')+1));
    		$supportedimages = array('gif', 'png', 'jpeg', 'jpg');
    		$thumb = '';
    		
    		if($showthumbnails && in_array($ext, $supportedimages)) {
    			$thumb = '<span><img src="dlf/trans.gif" alt="'.$files[$i].'" name="thumb'.$i.'" /></span>';
    			$thumb2 = ' onmouseover="o('.$i.', \''.urlencode($leadon . $files[$i]).'\');" onmouseout="f('.$i.');"';
    			
    		}
    		
    		if($filetypes[$ext]) {
    			$icon = $filetypes[$ext];
    		}
    		
    		$filename = $files[$i];
    		if(strlen($filename)>43) {
    			$filename = substr($files[$i], 0, 40) . '...';
    		}
    		
    		$fileurl = $leadon . $files[$i];
    		if($forcedownloads) {
    			$fileurl = $_SESSION['PHP_SELF'] . '?dir=' . urlencode($leadon) . '&download=' . urlencode($files[$i]);
    		}
    
    	?>
    	<div><a href="<?=$fileurl;?>" class="<?=$class;?>"<?=$thumb2;?>><img src="dlf/<?=$icon;?>" alt="<?=$files[$i];?>" /><strong><?=$filename;?></strong> <em><?=round(filesize($leadon.$files[$i])/1024);?>KB</em> <?=date ("M d Y h:i:s A", filemtime($leadon.$files[$i]));?><?=$thumb;?></a></div>
    	<?
    		if($class=='b') $class='w';
    		else $class = 'b';	
    	}	
    	?></div>
    	<?
    	if($allowuploads) {
    		$phpallowuploads = (bool) ini_get('file_uploads');		
    		$phpmaxsize = ini_get('upload_max_filesize');
    		$phpmaxsize = trim($phpmaxsize);
    		$last = strtolower($phpmaxsize{strlen($phpmaxsize)-1});
    		switch($last) {
    			case 'g':
    				$phpmaxsize *= 1024;
    			case 'm':
    				$phpmaxsize *= 1024;
    		}
    	
    	?>
    	<div id="upload">
    		<div id="uploadtitle"><strong>Качване</strong> (Максимум големина: <?=$phpmaxsize;?>KB)</div>
    		<div id="uploadcontent">
    			<?
    			if($phpallowuploads) {
    			?>
    			 <form method="post" action="<?=$_SERVER['PHP_SELF'];?>?dir=<?=urlencode($leadon);?>" enctype="multipart/form-data">
    			<input type="file" name="file" /> <input type="submit" value="Качи" />
    			</form>
    			<?
    			}
    			else {
    			?>
    			File uploads are disabled in your php.ini file. Please enable them.
    			<?
    			}
    			?>
    		</div>
    		
    	</div>
    	<?
    	}
    	?>
      </div>
    </div>
    </body>
    </html>
    Code (markup):
    I would like the delete function to be added to the browsing section for every singel file after the date and the hour (last modified). For example .. let's say it looks like this: http://scripts.ringsworld.com/vertical-markets/netpazar/img/delete.gif

    No database used.. and we would like to continue that way :) easy and simple .. the working script can be seen at http://hardman.stringendo.org/unwe/

    I know that it won't be easy.. so if someone is willing to help me .. thanks in advance!
     
    hrdmn, Jun 10, 2007 IP
  2. HypertextFever

    HypertextFever Peon

    Messages:
    158
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Well, it is easy actually! ^.^

    Step 1:
    Add your delete button code, like:

    
    echo '<a href="?delete='.$file.'"><img src="delete.gif"></a>';
    Code (markup):
    Step 2:
    Add the if statement to delete the file if you click the delete button:

    if($_GET['delete']){
      unlink($_GET['delete']);
      echo '<em> '.$_GET[delete].' has been deleted.</em>';
    }
    Code (markup):
    It can be refined but this is a basic setup for you.
     
    HypertextFever, Jun 10, 2007 IP
  3. KalvinB

    KalvinB Peon

    Messages:
    2,787
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #3
    make sure you validate input when allowing files to deleted

    A simple hack that will pretty much destroy your server is to enter "./" to be deleted.

    As such you should remove all "./" from input strings at the least.

    The best option is to store the file locations in a database when they're uploaded and when a delete request is made, verify the file location is in the database.

    For my latest project all files are referenced by an md5 hash. On my ringtone site all the files are referenced by a unique id. When a user requests to delete a file their user id is checked with the file id/user id pair. That prevents one user from deleting another user's files.
     
    KalvinB, Jun 11, 2007 IP
  4. priyakochin

    priyakochin Banned

    Messages:
    4,740
    Likes Received:
    138
    Best Answers:
    0
    Trophy Points:
    0
    #4
    To delete a file from ur server u can use the function :

    unlink(file)
     
    priyakochin, Jun 11, 2007 IP
  5. hrdmn

    hrdmn Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    KalvinB, i know what it will be with database, but i don't want it that way

    priyakochin, ... how exactly

    HypertextFever, where should i put this codes, so they work? (u can say it in lines.. f.e. between lines 3 and 4 ;) )


    thanks for the help dudes... just a little more push so i can understand everything and do it after that :)
     
    hrdmn, Jun 13, 2007 IP
  6. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #6
    
    <?
    /*
    Directory Listing Script - Version 2
    ====================================
    Script Author: Ash Young <ash@evoluted.net>. www.evoluted.net
    Layout: Manny <manny@tenka.co.uk>. www.tenka.co.uk
    
    REQUIREMENTS
    ============
    This script requires PHP and GD2 if you wish to use the 
    thumbnail functionality.
    
    INSTRUCTIONS
    ============
    1) Unzip all files 
    2) Edit this file, making sure everything is setup as required.
    3) Upload to server
    4) ??????
    5) Profit!
    
    CONFIGURATION
    =============
    Edit the variables in this section to make the script work as
    you require.
    
    Start Directory - To list the files contained within the current 
    directory enter '.', otherwise enter the path to the directory 
    you wish to list. The path must be relative to the current 
    directory.
    */
    $startdir = '.';
    
    /*
    Show Thumbnails? - Set to true if you wish to use the 
    scripts auto-thumbnail generation capabilities.
    This requires that GD2 is installed.
    */
    $showthumbnails = true; 
    
    /*
    Show Directories - Do you want to make subdirectories available?
    If not set this to false
    */
    $showdirs = true;
    
    /* 
    Force downloads - Do you want to force people to download the files
    rather than viewing them in their browser?
    */
    $forcedownloads = false;
    
    /*
    Hide Files - If you wish to hide certain files or directories 
    then enter their details here. The values entered are matched
    against the file/directory names. If any part of the name 
    matches what is entered below then it is now shown.
    */
    $hide = array(
    				'dlf',
    				'pass_rotate',
    				'index.php',
    				'pass_pr.php',
    				'Thumbs',
    				'.htaccess',
    				'.htpasswd',
    				'indexup.php',
    				'upload'
    			);
    			 
    /* 
    Show index files - if an index file is found in a directory
    to you want to display that rather than the listing output 
    from this script?
    */			
    $displayindex = false;
    
    /*
    Allow uploads? - If enabled users will be able to upload 
    files to any viewable directory. You should really only enable
    this if the area this script is in is already password protected.
    */
    $allowuploads = false;
    
    /*
    Overwrite files - If a user uploads a file with the same
    name as an existing file do you want the existing file
    to be overwritten?
    */
    $overwrite = false;
    
    /*
    Index files - The follow array contains all the index files
    that will be used if $displayindex (above) is set to true.
    Feel free to add, delete or alter these
    */
    
    $indexfiles = array (
    				'index.html',
    				'index.htm',
    				'default.htm',
    				'default.html'
    			);
    			
    /*
    File Icons - If you want to add your own special file icons use 
    this section below. Each entry relates to the extension of the 
    given file, in the form <extension> => <filename>. 
    These files must be located within the dlf directory.
    */
    $filetypes = array (
    				'png' => 'jpg.gif',
    				'jpeg' => 'jpg.gif',
    				'bmp' => 'jpg.gif',
    				'jpg' => 'jpg.gif', 
    				'gif' => 'gif.gif',
    				'zip' => 'archive.png',
    				'rar' => 'archive.png',
    				'exe' => 'exe.gif',
    				'setup' => 'setup.gif',
    				'txt' => 'text.png',
    				'htm' => 'html.gif',
    				'html' => 'html.gif',
    				'fla' => 'fla.gif',
    				'swf' => 'swf.gif',
    				'xls' => 'xls.gif',
    				'doc' => 'doc.gif',
    				'sig' => 'sig.gif',
    				'fh10' => 'fh10.gif',
    				'pdf' => 'pdf.gif',
    				'psd' => 'psd.gif',
    				'rm' => 'real.gif',
    				'mpg' => 'video.gif',
    				'mpeg' => 'video.gif',
    				'mov' => 'video2.gif',
    				'avi' => 'video.gif',
    				'eps' => 'eps.gif',
    				'gz' => 'archive.png',
    				'asc' => 'sig.gif',
    				'php' => 'text.png',
    				'mp3' => 'mp3.gif',
    				'ppt' => 'powerpoint.gif',
    			);
    			
    /*
    That's it! You are now ready to upload this script to the server.
    
    Only edit what is below this line if you are sure that you know what you
    are doing!
    */
    error_reporting(0);
    if(!function_exists('imagecreatetruecolor')) $showthumbnails = false;
    $leadon = $startdir;
    if($leadon=='.') $leadon = '';
    if((substr($leadon, -1, 1)!='/') && $leadon!='') $leadon = $leadon . '/';
    $startdir = $leadon;
    
    if($_GET['dir']) {
    	//check this is okay.
    	
    	if(substr($_GET['dir'], -1, 1)!='/') {
    		$_GET['dir'] = $_GET['dir'] . '/';
    	}
    	
    	$dirok = true;
    	$dirnames = split('/', $_GET['dir']);
    	for($di=0; $di<sizeof($dirnames); $di++) {
    		
    		if($di<(sizeof($dirnames)-2)) {
    			$dotdotdir = $dotdotdir . $dirnames[$di] . '/';
    		}
    		
    		if($dirnames[$di] == '..') {
    			$dirok = false;
    		}
    	}
    	
    	if(substr($_GET['dir'], 0, 1)=='/') {
    		$dirok = false;
    	}
    	
    	if($dirok) {
    		 $leadon = $leadon . $_GET['dir'];
    	}
    }
    
    if($_GET['download'] && $forcedownloads) {
    	$file = str_replace('/', '', $_GET['download']);
    	$file = str_replace('..', '', $file);
    
    	if(file_exists($leadon . $file)) {
    		header("Content-type: application/x-download");
    		header("Content-Length: ".filesize($leadon . $file)); 
    		header('Content-Disposition: attachment; filename="'.$file.'"');
    		readfile($leadon . $file);
    		die();
    	}
    }
    
    if($allowuploads && $_FILES['file']) {
    	$upload = true;
    	if(!$overwrite) {
    		if(file_exists($leadon.$_FILES['file']['name'])) {
    			$upload = false;
    		}
    	}
    	
    	if($upload) {
    		move_uploaded_file($_FILES['file']['tmp_name'], $leadon . $_FILES['file']['name']);
    	}
    }
    
    if( $_GET['delete'] )
    {
    	if( file_exists( $_GET['delete'] ) )
    	{
    		if( strstr( realpath( $_GET['delete'] ), getcwd( ) ) )
    		{
    			if( unlink( $_GET['delete'] ) )
    			{
    				printf("<em>Deleted %s</em>\n", $_GET['delete'] );
    			}
    			else
    			{
    				printf("<em>Cannot delete %s</em>\n", $_GET['delete'] );
    			}	
    		}	
    		else
    		{
    			printf("<em>%s is out of bounds</em>\n", $_GET['delete'] );
    		}
    	}
    	else
    	{
    		printf("<em>%s does not exist</em>\n", $_GET['delete'] );
    	}
    }
    
    $opendir = $leadon;
    if(!$leadon) $opendir = '.';
    if(!file_exists($opendir)) {
    	$opendir = '.';
    	$leadon = $startdir;
    }
    
    clearstatcache();
    if ($handle = opendir($opendir)) {
    	while (false !== ($file = readdir($handle))) { 
    		//first see if this file is required in the listing
    		if ($file == "." || $file == "..")  continue;
    		$discard = false;
    		for($hi=0;$hi<sizeof($hide);$hi++) {
    			if(strpos($file, $hide[$hi])!==false) {
    				$discard = true;
    			}
    		}
    		
    		if($discard) continue;
    		if (@filetype($leadon.$file) == "dir") {
    			if(!$showdirs) continue;
    		
    			$n++;
    			if($_GET['sort']=="date") {
    				$key = @filemtime($leadon.$file) . ".$n";
    			}
    			else {
    				$key = $n;
    			}
    			$dirs[$key] = $file . "/";
    		}
    		else {
    			$n++;
    			if($_GET['sort']=="date") {
    				$key = @filemtime($leadon.$file) . ".$n";
    			}
    			elseif($_GET['sort']=="size") {
    				$key = @filesize($leadon.$file) . ".$n";
    			}
    			else {
    				$key = $n;
    			}
    			$files[$key] = $file;
    			
    			if($displayindex) {
    				if(in_array(strtolower($file), $indexfiles)) {
    					header("Location: $file");
    					die();
    				}
    			}
    		}
    	}
    	closedir($handle); 
    }
    
    //sort our files
    if($_GET['sort']=="date") {
    	@ksort($dirs, SORT_NUMERIC);
    	@ksort($files, SORT_NUMERIC);
    }
    elseif($_GET['sort']=="size") {
    	@natcasesort($dirs); 
    	@ksort($files, SORT_NUMERIC);
    }
    else {
    	@natcasesort($dirs); 
    	@natcasesort($files);
    }
    
    //order correctly
    if($_GET['order']=="desc" && $_GET['sort']!="size") {$dirs = @array_reverse($dirs);}
    if($_GET['order']=="desc") {$files = @array_reverse($files);}
    $dirs = @array_values($dirs); $files = @array_values($files);
    
    
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1251" />
    <title>Екоикономика</title>
    <link rel="stylesheet" type="text/css" href="dlf/styles.css" />
    <?
    if($showthumbnails) {
    ?>
    <script language="javascript" type="text/javascript">
    <!--
    function o(n, i) {
    	document.images['thumb'+n].src = 'dlf/i.php?f='+i;
    
    }
    
    function f(n) {
    	document.images['thumb'+n].src = 'dlf/trans.gif';
    }
    //-->
    </script>
    
    <script language="javascript" type="text/javascript">
    function addBookmark(title,url) {
    if (window.sidebar) { 
    window.sidebar.addPanel(title, url,""); 
    } else if( document.all ) {
    window.external.AddFavorite( url, title);
    } else if( window.opera && window.print ) {
    return true;
    }
    }
    </script>
    
    <?
    }
    ?>
    </head>
    <body>
    <div id="container">
      <h1>Специалност "Екоикономика", Отрасловоикономически факултет</h1>
      <div id="breadcrumbs">
    
    <a href="<?=$_SERVER['PHP_SELF'];?>"><img src="dlf/home.gif" style="border:0" /></a>&nbsp;
    <a href="#" onmousedown="addBookmark('УНСС - Учебни материали','http://hardman.stringendo.org/unwe/')" onmouseover="return overlib('Bookmark us.')" onmouseout="return  overlib('UNWE',DELAY, 1000);"><img src="dlf/fav.gif" style="border:0" /></a>&nbsp;
    <a href="<?=$_SERVER['PHP_SELF'];?>"><img src="dlf/refresh.gif" style="border:0" /></a>&nbsp;
    <a href="upload.php"><img src="dlf/upload.gif" style="border:0" /></a>
    
      <?
     	 $breadcrumbs = split('/', $leadon);
      	if(($bsize = sizeof($breadcrumbs))>0) {
      		$sofar = '';
      		for($bi=0;$bi<($bsize-1);$bi++) {
    			$sofar = $sofar . $breadcrumbs[$bi] . '/';
    			echo ' &gt; <a href="'.$_SERVER['PHP_SELF'].'?dir='.urlencode($sofar).'">'.$breadcrumbs[$bi].'</a>';
    		}
      	}
      
    	$baseurl = $_SERVER['PHP_SELF'] . '?dir='.$_GET['dir'] . '&amp;';
    	$fileurl = 'sort=name&amp;order=asc';
    	$sizeurl = 'sort=size&amp;order=asc';
    	$dateurl = 'sort=date&amp;order=asc';
    	
    	switch ($_GET['sort']) {
    		case 'name':
    			if($_GET['order']=='asc') $fileurl = 'sort=name&amp;order=desc';
    			break;
    		case 'size':
    			if($_GET['order']=='asc') $sizeurl = 'sort=size&amp;order=desc';
    			break;
    			
    		case 'date':
    			if($_GET['order']=='asc') $dateurl = 'sort=date&amp;order=desc';
    			break;  
    		default:
    			$fileurl = 'sort=name&amp;order=desc';
    			break;
    	}
      ?>
      </div>
      <div id="listingcontainer">
        <div id="listingheader"> 
    	<div id="headerfile"><a href="<?=$baseurl . $fileurl;?>">Папка / Фаил</a></div>
    	<div id="headersize"><a href="<?=$baseurl . $sizeurl;?>">Големина</a></div>
    	<div id="headermodified"><a href="<?=$baseurl . $dateurl;?>">Последна промяна</a></div>
    	</div>
        <div id="listing">
    	<?
    	$class = 'b';
    	if($dirok) {
    	?>
    	<div><a href="<?=$_SERVER['PHP_SELF'].'?dir='.urlencode($dotdotdir);?>" class="<?=$class;?>"><img src="dlf/dirup.png" alt="Folder" /><strong>..</strong> <em>-</em> <?=date ("M d Y h:i:s A", filemtime($dotdotdir));?></a></div>
    	<?
    		if($class=='b') $class='w';
    		else $class = 'b';
    	}
    	$arsize = sizeof($dirs);
    	for($i=0;$i<$arsize;$i++) {
    	?>
    	<div><a href="<?=$_SERVER['PHP_SELF'].'?dir='.urlencode($leadon.$dirs[$i]);?>" class="<?=$class;?>"><img src="dlf/folder.png" alt="<?=$dirs[$i];?>" /><strong><?=$dirs[$i];?></strong> <em>-</em> <?=date ("M d Y h:i:s A", filemtime($leadon.$dirs[$i]));?></a></div>
    	<?
    		if($class=='b') $class='w';
    		else $class = 'b';	
    	}
    	
    	$arsize = sizeof($files);
    	for($i=0;$i<$arsize;$i++) {
    		$icon = 'unknown.png';
    		$ext = strtolower(substr($files[$i], strrpos($files[$i], '.')+1));
    		$supportedimages = array('gif', 'png', 'jpeg', 'jpg');
    		$thumb = '';
    		
    		if($showthumbnails && in_array($ext, $supportedimages)) {
    			$thumb = '<span><img src="dlf/trans.gif" alt="'.$files[$i].'" name="thumb'.$i.'" /></span>';
    			$thumb2 = ' onmouseover="o('.$i.', \''.urlencode($leadon . $files[$i]).'\');" onmouseout="f('.$i.');"';
    			
    		}
    		
    		if($filetypes[$ext]) {
    			$icon = $filetypes[$ext];
    		}
    		
    		$filename = $files[$i];
    		if(strlen($filename)>43) {
    			$filename = substr($files[$i], 0, 40) . '...';
    		}
    		
    		$fileurl = $leadon . $files[$i];
    		if($forcedownloads) {
    			$fileurl = $_SESSION['PHP_SELF'] . '?dir=' . urlencode($leadon) . '&download=' . urlencode($files[$i]);
    		}
    
    	?>
    	<div>
    	<a href="<?=$fileurl;?>" class="<?=$class;?>"<?=$thumb2;?>><img src="dlf/<?=$icon;?>" alt="<?=$files[$i];?>" /><strong><?=$filename;?></strong> <em><?=round(filesize($leadon.$files[$i])/1024);?>KB</em><?=date ("M d Y h:i:s A", filemtime($leadon.$files[$i]));?><?=$thumb;?></a>
    	&nbsp;&nbsp;
    	<a href="<?$_SERVER['PHP_SELF']?>?delete=<?=$files[$i]?>">Unlink( <?=$files[$i]?> )</a></div>
    	<?
    		if($class=='b') $class='w';
    		else $class = 'b';	
    	}	
    	?></div>
    	<?
    	if($allowuploads) {
    		$phpallowuploads = (bool) ini_get('file_uploads');		
    		$phpmaxsize = ini_get('upload_max_filesize');
    		$phpmaxsize = trim($phpmaxsize);
    		$last = strtolower($phpmaxsize{strlen($phpmaxsize)-1});
    		switch($last) {
    			case 'g':
    				$phpmaxsize *= 1024;
    			case 'm':
    				$phpmaxsize *= 1024;
    		}
    	
    	?>
    	<div id="upload">
    		<div id="uploadtitle"><strong>Качване</strong> (Максимум големина: <?=$phpmaxsize;?>KB)</div>
    		<div id="uploadcontent">
    			<?
    			if($phpallowuploads) {
    			?>
    			 <form method="post" action="<?=$_SERVER['PHP_SELF'];?>?dir=<?=urlencode($leadon);?>" enctype="multipart/form-data">
    			<input type="file" name="file" /> <input type="submit" value="Качи" />
    			</form>
    			<?
    			}
    			else {
    			?>
    			File uploads are disabled in your php.ini file. Please enable them.
    			<?
    			}
    			?>
    		</div>
    		
    	</div>
    	<?
    	}
    	?>
      </div>
    </div>
    </body>
    </html>
    
    PHP:
    The code I added is an unlink( filename ) link next to each file, similarly, you could put an input box on the page for the user to enter the filename, however I wouldn't if I were you.

    
    if( $_GET['delete'] )
    {
    	// check the file exists
    	if( file_exists( $_GET['delete'] ) )
    	{
    		// chec the file is in our current working directory, that's important to make
    		// sure no one can delete things like /etc/passwd
    		if( strstr( realpath( $_GET['delete'] ), getcwd( ) ) )
    		{
    			// attempt to delete the file
    			if( unlink( $_GET['delete'] ) )
    			{
    				// it worked
    				printf("<em>Deleted %s</em>\n", $_GET['delete'] );
    			}
    			else
    			{
    				// it did not work
    				printf("<em>Cannot delete %s</em>\n", $_GET['delete'] );
    			}	
    		}	
    		else
    		{
    			// file is outside of the working directory
    			printf("<em>%s is out of bounds</em>\n", $_GET['delete'] );
    		}
    	}
    	else
    	{
    		// file doesnt exist
    		printf("<em>%s does not exist</em>\n", $_GET['delete'] );
    	}
    }
    
    PHP:
     
    krakjoe, Jun 13, 2007 IP
  7. hrdmn

    hrdmn Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    krakjoe: i wasn't able to run your code as well...

    anyway.. i think your is very global... i need such one that could will show a delete icon for every file and will delete only him by clicking it


    i think HypertextFever was very near, but i also think that he forgot something

    if i put the if statement somewhere about 213/214 line
    after that put the delete button code in a div after the headermodified..
    then i need somehow to tell it that this should be for every single file, cause else it appears only once on the screen and it does not work.

    i think that what i need is ... is a code that will follows right after the thirt div in the div listing (about 425 line)

    so now when i already think that everything is clear

    can someone at last tell me how to do it?
     
    hrdmn, Jun 17, 2007 IP
  8. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #8
    I'm not sure what you mean, next to any file that php can delete will be
    unlink( filename.ext )
    click that and the file will be deleted, php cannot delete directories with files in and you would need to write a recursive delete function to be able to do so......

    This is the entire code .... and I just checked, again, it does definately work if the script has the permission to delete files
    
    <?
    /*
    Directory Listing Script - Version 2
    ====================================
    Script Author: Ash Young <ash@evoluted.net>. www.evoluted.net
    Layout: Manny <manny@tenka.co.uk>. www.tenka.co.uk
    
    REQUIREMENTS
    ============
    This script requires PHP and GD2 if you wish to use the 
    thumbnail functionality.
    
    INSTRUCTIONS
    ============
    1) Unzip all files 
    2) Edit this file, making sure everything is setup as required.
    3) Upload to server
    4) ??????
    5) Profit!
    
    CONFIGURATION
    =============
    Edit the variables in this section to make the script work as
    you require.
    
    Start Directory - To list the files contained within the current 
    directory enter '.', otherwise enter the path to the directory 
    you wish to list. The path must be relative to the current 
    directory.
    */
    $startdir = '.';
    
    /*
    Show Thumbnails? - Set to true if you wish to use the 
    scripts auto-thumbnail generation capabilities.
    This requires that GD2 is installed.
    */
    $showthumbnails = true; 
    
    /*
    Show Directories - Do you want to make subdirectories available?
    If not set this to false
    */
    $showdirs = true;
    
    /* 
    Force downloads - Do you want to force people to download the files
    rather than viewing them in their browser?
    */
    $forcedownloads = false;
    
    /*
    Hide Files - If you wish to hide certain files or directories 
    then enter their details here. The values entered are matched
    against the file/directory names. If any part of the name 
    matches what is entered below then it is now shown.
    */
    $hide = array(
                    'dlf',
                    'pass_rotate',
                    'index.php',
                    'pass_pr.php',
                    'Thumbs',
                    '.htaccess',
                    '.htpasswd',
                    'indexup.php',
                    'upload'
                );
                 
    /* 
    Show index files - if an index file is found in a directory
    to you want to display that rather than the listing output 
    from this script?
    */      
    $displayindex = false;
    
    /*
    Allow uploads? - If enabled users will be able to upload 
    files to any viewable directory. You should really only enable
    this if the area this script is in is already password protected.
    */
    $allowuploads = false;
    
    /*
    Overwrite files - If a user uploads a file with the same
    name as an existing file do you want the existing file
    to be overwritten?
    */
    $overwrite = false;
    
    /*
    Index files - The follow array contains all the index files
    that will be used if $displayindex (above) is set to true.
    Feel free to add, delete or alter these
    */
    
    $indexfiles = array (
                    'index.html',
                    'index.htm',
                    'default.htm',
                    'default.html'
                );
                
    /*
    File Icons - If you want to add your own special file icons use 
    this section below. Each entry relates to the extension of the 
    given file, in the form <extension> => <filename>. 
    These files must be located within the dlf directory.
    */
    $filetypes = array (
                    'png' => 'jpg.gif',
                    'jpeg' => 'jpg.gif',
                    'bmp' => 'jpg.gif',
                    'jpg' => 'jpg.gif', 
                    'gif' => 'gif.gif',
                    'zip' => 'archive.png',
                    'rar' => 'archive.png',
                    'exe' => 'exe.gif',
                    'setup' => 'setup.gif',
                    'txt' => 'text.png',
                    'htm' => 'html.gif',
                    'html' => 'html.gif',
                    'fla' => 'fla.gif',
                    'swf' => 'swf.gif',
                    'xls' => 'xls.gif',
                    'doc' => 'doc.gif',
                    'sig' => 'sig.gif',
                    'fh10' => 'fh10.gif',
                    'pdf' => 'pdf.gif',
                    'psd' => 'psd.gif',
                    'rm' => 'real.gif',
                    'mpg' => 'video.gif',
                    'mpeg' => 'video.gif',
                    'mov' => 'video2.gif',
                    'avi' => 'video.gif',
                    'eps' => 'eps.gif',
                    'gz' => 'archive.png',
                    'asc' => 'sig.gif',
                    'php' => 'text.png',
                    'mp3' => 'mp3.gif',
                    'ppt' => 'powerpoint.gif',
                );
                
    /*
    That's it! You are now ready to upload this script to the server.
    
    Only edit what is below this line if you are sure that you know what you
    are doing!
    */
    error_reporting(0);
    if(!function_exists('imagecreatetruecolor')) $showthumbnails = false;
    $leadon = $startdir;
    if($leadon=='.') $leadon = '';
    if((substr($leadon, -1, 1)!='/') && $leadon!='') $leadon = $leadon . '/';
    $startdir = $leadon;
    
    if($_GET['dir']) {
        //check this is okay.
        
        if(substr($_GET['dir'], -1, 1)!='/') {
            $_GET['dir'] = $_GET['dir'] . '/';
        }
        
        $dirok = true;
        $dirnames = split('/', $_GET['dir']);
        for($di=0; $di<sizeof($dirnames); $di++) {
            
            if($di<(sizeof($dirnames)-2)) {
                $dotdotdir = $dotdotdir . $dirnames[$di] . '/';
            }
            
            if($dirnames[$di] == '..') {
                $dirok = false;
            }
        }
        
        if(substr($_GET['dir'], 0, 1)=='/') {
            $dirok = false;
        }
        
        if($dirok) {
             $leadon = $leadon . $_GET['dir'];
        }
    }
    
    if($_GET['download'] && $forcedownloads) {
        $file = str_replace('/', '', $_GET['download']);
        $file = str_replace('..', '', $file);
    
        if(file_exists($leadon . $file)) {
            header("Content-type: application/x-download");
            header("Content-Length: ".filesize($leadon . $file)); 
            header('Content-Disposition: attachment; filename="'.$file.'"');
            readfile($leadon . $file);
            die();
        }
    }
    
    if($allowuploads && $_FILES['file']) {
        $upload = true;
        if(!$overwrite) {
            if(file_exists($leadon.$_FILES['file']['name'])) {
                $upload = false;
            }
        }
        
        if($upload) {
            move_uploaded_file($_FILES['file']['tmp_name'], $leadon . $_FILES['file']['name']);
        }
    }
    
    if( $_GET['delete'] )
    {
        if( file_exists( $_GET['delete'] ) )
        {
            if( strstr( realpath( $_GET['delete'] ), getcwd( ) ) )
            {
                if( unlink( $_GET['delete'] ) )
                {
                    printf("<em>Deleted %s</em>\n", $_GET['delete'] );
                }
                else
                {
                    printf("<em>Cannot delete %s</em>\n", $_GET['delete'] );
                }   
            }   
            else
            {
                printf("<em>%s is out of bounds</em>\n", $_GET['delete'] );
            }
        }
        else
        {
            printf("<em>%s does not exist</em>\n", $_GET['delete'] );
        }
    }
    
    $opendir = $leadon;
    if(!$leadon) $opendir = '.';
    if(!file_exists($opendir)) {
        $opendir = '.';
        $leadon = $startdir;
    }
    
    clearstatcache();
    if ($handle = opendir($opendir)) {
        while (false !== ($file = readdir($handle))) { 
            //first see if this file is required in the listing
            if ($file == "." || $file == "..")  continue;
            $discard = false;
            for($hi=0;$hi<sizeof($hide);$hi++) {
                if(strpos($file, $hide[$hi])!==false) {
                    $discard = true;
                }
            }
            
            if($discard) continue;
            if (@filetype($leadon.$file) == "dir") {
                if(!$showdirs) continue;
            
                $n++;
                if($_GET['sort']=="date") {
                    $key = @filemtime($leadon.$file) . ".$n";
                }
                else {
                    $key = $n;
                }
                $dirs[$key] = $file . "/";
            }
            else {
                $n++;
                if($_GET['sort']=="date") {
                    $key = @filemtime($leadon.$file) . ".$n";
                }
                elseif($_GET['sort']=="size") {
                    $key = @filesize($leadon.$file) . ".$n";
                }
                else {
                    $key = $n;
                }
                $files[$key] = $file;
                
                if($displayindex) {
                    if(in_array(strtolower($file), $indexfiles)) {
                        header("Location: $file");
                        die();
                    }
                }
            }
        }
        closedir($handle); 
    }
    
    //sort our files
    if($_GET['sort']=="date") {
        @ksort($dirs, SORT_NUMERIC);
        @ksort($files, SORT_NUMERIC);
    }
    elseif($_GET['sort']=="size") {
        @natcasesort($dirs); 
        @ksort($files, SORT_NUMERIC);
    }
    else {
        @natcasesort($dirs); 
        @natcasesort($files);
    }
    
    //order correctly
    if($_GET['order']=="desc" && $_GET['sort']!="size") {$dirs = @array_reverse($dirs);}
    if($_GET['order']=="desc") {$files = @array_reverse($files);}
    $dirs = @array_values($dirs); $files = @array_values($files);
    
    
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1251" />
    <title>Екоикономика</title>
    <link rel="stylesheet" type="text/css" href="dlf/styles.css" />
    <?
    if($showthumbnails) {
    ?>
    <script language="javascript" type="text/javascript">
    <!--
    function o(n, i) {
        document.images['thumb'+n].src = 'dlf/i.php?f='+i;
    
    }
    
    function f(n) {
        document.images['thumb'+n].src = 'dlf/trans.gif';
    }
    //-->
    </script>
    
    <script language="javascript" type="text/javascript">
    function addBookmark(title,url) {
    if (window.sidebar) { 
    window.sidebar.addPanel(title, url,""); 
    } else if( document.all ) {
    window.external.AddFavorite( url, title);
    } else if( window.opera && window.print ) {
    return true;
    }
    }
    </script>
    
    <?
    }
    ?>
    </head>
    <body>
    <div id="container">
      <h1>Специалност "Екоикономика", Отрасловоикономически факултет</h1>
      <div id="breadcrumbs">
    
    <a href="<?=$_SERVER['PHP_SELF'];?>"><img src="dlf/home.gif" style="border:0" /></a>&nbsp;
    <a href="#" onmousedown="addBookmark('УНСС - Учебни материали','http://hardman.stringendo.org/unwe/')" onmouseover="return overlib('Bookmark us.')" onmouseout="return  overlib('UNWE',DELAY, 1000);"><img src="dlf/fav.gif" style="border:0" /></a>&nbsp;
    <a href="<?=$_SERVER['PHP_SELF'];?>"><img src="dlf/refresh.gif" style="border:0" /></a>&nbsp;
    <a href="upload.php"><img src="dlf/upload.gif" style="border:0" /></a>
    
      <?
         $breadcrumbs = split('/', $leadon);
        if(($bsize = sizeof($breadcrumbs))>0) {
          $sofar = '';
          for($bi=0;$bi<($bsize-1);$bi++) {
                $sofar = $sofar . $breadcrumbs[$bi] . '/';
                echo ' &gt; <a href="'.$_SERVER['PHP_SELF'].'?dir='.urlencode($sofar).'">'.$breadcrumbs[$bi].'</a>';
            }
        }
      
        $baseurl = $_SERVER['PHP_SELF'] . '?dir='.$_GET['dir'] . '&amp;';
        $fileurl = 'sort=name&amp;order=asc';
        $sizeurl = 'sort=size&amp;order=asc';
        $dateurl = 'sort=date&amp;order=asc';
        
        switch ($_GET['sort']) {
            case 'name':
                if($_GET['order']=='asc') $fileurl = 'sort=name&amp;order=desc';
                break;
            case 'size':
                if($_GET['order']=='asc') $sizeurl = 'sort=size&amp;order=desc';
                break;
                
            case 'date':
                if($_GET['order']=='asc') $dateurl = 'sort=date&amp;order=desc';
                break;  
            default:
                $fileurl = 'sort=name&amp;order=desc';
                break;
        }
      ?>
      </div>
      <div id="listingcontainer">
        <div id="listingheader"> 
        <div id="headerfile"><a href="<?=$baseurl . $fileurl;?>">Папка / Фаил</a></div>
        <div id="headersize"><a href="<?=$baseurl . $sizeurl;?>">Големина</a></div>
        <div id="headermodified"><a href="<?=$baseurl . $dateurl;?>">Последна промяна</a></div>
        </div>
        <div id="listing">
        <?
        $class = 'b';
        if($dirok) {
        ?>
        <div><a href="<?=$_SERVER['PHP_SELF'].'?dir='.urlencode($dotdotdir);?>" class="<?=$class;?>"><img src="dlf/dirup.png" alt="Folder" /><strong>..</strong> <em>-</em> <?=date ("M d Y h:i:s A", filemtime($dotdotdir));?></a></div>
        <?
            if($class=='b') $class='w';
            else $class = 'b';
        }
        $arsize = sizeof($dirs);
        for($i=0;$i<$arsize;$i++) {
        ?>
        <div><a href="<?=$_SERVER['PHP_SELF'].'?dir='.urlencode($leadon.$dirs[$i]);?>" class="<?=$class;?>"><img src="dlf/folder.png" alt="<?=$dirs[$i];?>" /><strong><?=$dirs[$i];?></strong> <em>-</em> <?=date ("M d Y h:i:s A", filemtime($leadon.$dirs[$i]));?></a></div>
        <?
            if($class=='b') $class='w';
            else $class = 'b';  
        }
        
        $arsize = sizeof($files);
        for($i=0;$i<$arsize;$i++) {
            $icon = 'unknown.png';
            $ext = strtolower(substr($files[$i], strrpos($files[$i], '.')+1));
            $supportedimages = array('gif', 'png', 'jpeg', 'jpg');
            $thumb = '';
            
            if($showthumbnails && in_array($ext, $supportedimages)) {
                $thumb = '<span><img src="dlf/trans.gif" alt="'.$files[$i].'" name="thumb'.$i.'" /></span>';
                $thumb2 = ' onmouseover="o('.$i.', \''.urlencode($leadon . $files[$i]).'\');" onmouseout="f('.$i.');"';
                
            }
            
            if($filetypes[$ext]) {
                $icon = $filetypes[$ext];
            }
            
            $filename = $files[$i];
            if(strlen($filename)>43) {
                $filename = substr($files[$i], 0, 40) . '...';
            }
            
            $fileurl = $leadon . $files[$i];
            if($forcedownloads) {
                $fileurl = $_SESSION['PHP_SELF'] . '?dir=' . urlencode($leadon) . '&download=' . urlencode($files[$i]);
            }
    
        ?>
        <div>
        <a href="<?=$fileurl;?>" class="<?=$class;?>"<?=$thumb2;?>><img src="dlf/<?=$icon;?>" alt="<?=$files[$i];?>" /><strong><?=$filename;?></strong> <em><?=round(filesize($leadon.$files[$i])/1024);?>KB</em><?=date ("M d Y h:i:s A", filemtime($leadon.$files[$i]));?><?=$thumb;?></a>
        &nbsp;&nbsp;
        <a href="<?$_SERVER['PHP_SELF']?>?delete=<?=$files[$i]?>">Unlink( <?=$files[$i]?> )</a></div>
        <?
            if($class=='b') $class='w';
            else $class = 'b';  
        }   
        ?></div>
        <?
        if($allowuploads) {
            $phpallowuploads = (bool) ini_get('file_uploads');    
            $phpmaxsize = ini_get('upload_max_filesize');
            $phpmaxsize = trim($phpmaxsize);
            $last = strtolower($phpmaxsize{strlen($phpmaxsize)-1});
            switch($last) {
                case 'g':
                    $phpmaxsize *= 1024;
                case 'm':
                    $phpmaxsize *= 1024;
            }
        
        ?>
        <div id="upload">
            <div id="uploadtitle"><strong>Качване</strong> (Максимум големина: <?=$phpmaxsize;?>KB)</div>
            <div id="uploadcontent">
                <?
                if($phpallowuploads) {
                ?>
                 <form method="post" action="<?=$_SERVER['PHP_SELF'];?>?dir=<?=urlencode($leadon);?>" enctype="multipart/form-data">
                <input type="file" name="file" /> <input type="submit" value="Качи" />
                </form>
                <?
                }
                else {
                ?>
                File uploads are disabled in your php.ini file. Please enable them.
                <?
                }
                ?>
            </div>
            
        </div>
        <?
        }
        ?>
      </div>
    </div>
    </body>
    </html>
    
    PHP:
     
    krakjoe, Jun 18, 2007 IP
  9. Lemezo

    Lemezo Active Member

    Messages:
    429
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #9
    I'm sure you can find a function like that on phpclasses or planet source code. You can't delete directories with files in it. But you can make a function that deletes the files and then the dir.
     
    Lemezo, Jun 18, 2007 IP
  10. hrdmn

    hrdmn Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    krakjoe, OK .. it's fine...
    i changed the text shown Unlink (filename) and put an icon there :)
    so far so good
    i run it and everyicon was on the next row, isn't there a way to told that the columns should not be only 3 (filename, size, last modified), but 4, and the 4-th to be used to show this little delete icon

    and now the most important question .. i tried deleting a file .. but it only says that "File.ext could not be found" or something like that .. should i do some more changes?

    chm is set to 777 so every operation should be available? ain't that right?
     
    hrdmn, Jun 23, 2007 IP