File manger - create directory not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
merlin89
Forum Newbie
Posts: 17
Joined: Thu Nov 10, 2005 7:57 am

File manger - create directory not working

Post by merlin89 »

Hi im using two files to create a file manager at the moment createdir.php which resides in a root directory called filemanager and an index.php which resides in root->files. Problem I have is that if I just have files in the directory filemanager->files they are displayed fine , but when I create a directory only a single directory can be created and it gets named 1: Then when I go to display all the files and the new directory it shows two errors

'Notice: Undefined index: extension in C:\filemanager\files\index.php on line 109' and it also displays
below the name of the newly created directory 'Array ( [0] => 1 ) '



The index.php file is

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



<html>

<head>

<title>File Manager</title>

<link rel="stylesheet" type="text/css" href="../layout.css" />

</head>



<body>

<div id="filemanager">

<h1>Your files</h1>



<form method="post" action="index.php">



<?php
global $aDir;
function copyName($i, $filename, $path) {



		if (file_exists($path . $i . "_" . $filename)) {

			$i++;

			return copyName($i, $filename, $path);

		} else {

			return $path . $i . "_" . $filename;

		}

	}



$path = dirname($_SERVER['PATH_TRANSLATED']) . "/";

$dir_handle = @opendir($path) or die("Unable to open $path");



if (isset($_POST["btnDelete"])  && isset($_POST["filename"] )) {

	if (file_exists($path . $_POST["filename"])) {

		unlink($path . $_POST["filename"]);

		echo "<p>The file has now been deleted.</p>\n";

	}



} elseif (isset($_POST["btnCopy"])  && isset($_POST['filename']) ) {



	if (file_exists($path . $_POST['filename'])) {

		$newfile = copyName(1, $_POST['filename'], $path);



		copy($path . $_POST['filename'], $newfile);

		echo "<p>The file has now been copied.</p>\n";

	}



} elseif (isset($_POST['btnMove'] ) && isset($_POST['filename']) && isset($_POST["move-directory"] )) {

	$newpath = $path .  $_POST["move-directory"] . "/";



	if(file_exists($newpath . $_POST['filename'])) {

		$newfile = copyName(1, $_POST['filename'], $newpath);

	} else {

		$newfile = $newpath . $_POST["filename"];

	}

	rename($path . $_POST["filename"], $newfile);

}

echo "<ul id=\"files\">";





while ($file = readdir($dir_handle)) {

	$path_parts = pathinfo("$path/" . $file);

	$ext = $path_parts["extension"];



	if($file == "." || $file == ".." || $file == "index.php" )

	continue;





	if ($ext == "txt") {

		$icon = "txt.gif";

	} elseif ($ext == "php" || $ext == "html" || $ext == "htm") {

		$icon = "web.gif";

	} elseif ($ext == "doc") {

		$icon = "word.gif";

	} elseif ($ext == "jpg" || $ext == "gif") {

		$icon = "img.gif";

	} elseif ($ext == "pdf") {

		$icon = "pdf.gif";

	} elseif ($ext == "") {

		$icon = "dir.gif";

	} else {

		$icon = "txt.gif";

	}



	if ($icon == "dir.gif") {

	    $dir = $path_parts["basename"];

		$aDir[]=$dir;

	    echo "<li><img src=\"../img/" . $icon . "\" alt=\"\" /> <a href=\"" . $dir . "\">" . $dir . "</a></li>\n";

	} else {

    	echo "<li><input type=\"radio\" name=\"filename\" value=\"" . $file . "\" /> <img src=\"../img/" . $icon . "\" alt=\"\" /> <a href=\"" . $file . "\">" . $file . " </a></li>\n";

    }



}



closedir($dir_handle);

echo "</ul>";



print_r($aDir);

?>



<p><input type="submit" name="btnDelete" value="Delete file" /> &nbsp; <input type="submit" name="btnCopy" value="Copy file" /> &nbsp; <input type="submit" name="btnMove" value="Move to" />

 <select name="move-directory">



 <option value="">Select directory</option>

  <?php

  for ($i=0; $i < count($aDir); $i++) {

  	echo "<option value=\"" . $aDir[$i] . "\">" . $aDir[$i] . "</option>";

  }

  ?>

 </select></p>



</form>



<p class="nav"><a href="../index.php">Main menu</a> | <a href="../upload.php">Upload a file</a> | <a href="../createdir.php">Create a new directory</a></p>

</div>

</body>

</html>

and the createdir.php is

Code: Select all

<?php


global $dirfail;
if (isset($_POST["btnCreate"])) {
//echo 'button clicked';
	$newDir = isset($_POST['dirname']);

	$dirpath = "/filemanager/files/" . $newDir;

	if (! @mkdir($dirpath, 0777)) {

		$dirfail = true;

	} else {

		copy("/filemanager/tmp/index.php", $dirpath . "/index.php");



		header("Location: files/index.php");

	}

}

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



<html>

<head>

<title>File Manager - Create a new directory</title>

<link rel="stylesheet" type="text/css" href="layout.css" />

</head>



<body>

<div id="filemanager">



<h1>Create a new directory</h1>

<?php if ($dirfail == 'true') {?>

<p><strong>There was a problem creating this directory please try again.</strong></p>

<?php } ?>

<form method="post" action="createdir.php">

<p><label for="dirname">Name your directory</label>

<br /><input type="text" name="dirname" id="dirname" /></p>



<p><input type="submit" name="btnCreate" id="btnCreate" value="Create Directory" /></p>





</form>



</div>



</body>

</html>
any help you can offer is greatly appreciated, thanks in advance
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

Try removing the @ infront of mkdir(). It's supressing error messages. See what you get when you remove it.

Code: Select all

mkdir($dirpath, 0777);
merlin89
Forum Newbie
Posts: 17
Joined: Thu Nov 10, 2005 7:57 am

new error message

Post by merlin89 »

after removing the @ character I now get this message so I've got a directory structure problem i cant resolve also
plus it is still creating a new directory of 1

Warning: mkdir(/filemanager/files/1): No such file or directory in e:\domains\m\maccms.co.uk\user\htdocs\filemanager\createdir.php on line 8
many thanks
Ian
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

use realpath() to generate the absolute path to the parent of the dir you want to create, i.e. you need an absolute path including drive letter on windows, as '/' is not a valid Win32 path.

Code: Select all

<?php

$path = realpath('.') . $newdir;
mkdir($path, 0644);

?>
Where $newdir is the name of your desired folder.
merlin89
Forum Newbie
Posts: 17
Joined: Thu Nov 10, 2005 7:57 am

nearly there

Post by merlin89 »

thanks tried it nearly got there its just not appending the new directory to the end of th e path so the error I get now is
Warning: mkdir(e:\domains\m\maccms.co.uk\user\htdocs\filemanager): File exists in e:\domains\m\maccms.co.uk\user\htdocs\filemanager\createdir.php on line 7
any ideas
thanks for the help by the way
Ian
merlin89
Forum Newbie
Posts: 17
Joined: Thu Nov 10, 2005 7:57 am

Eureka -got it but new issue with directory listing denied

Post by merlin89 »

Hi thanks for help finally got it new code for createdir.php is as below

Code: Select all

<?php


global $dirfail;
if (isset($_POST["btnCreate"])) {

	$newDir = $_POST['dirname'];

	$dirpath = realpath('.') . '\\files\\'. $newDir;
echo $dirpath;
	if (! mkdir($dirpath, 0644)) {

		$dirfail = true;

	} else {

		//copy("/filemanager/tmp/index.php", $dirpath . "/index.php");



		//header("Location: files/index.php");

	}

}

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



<html>

<head>

<title>File Manager - Create a new directory</title>

<link rel="stylesheet" type="text/css" href="layout.css" />

</head>



<body>

<div id="filemanager">



<h1>Create a new directory</h1>

<?php if ($dirfail == true) {?>

<p><strong>There was a problem creating this directory please try again.</strong></p>

<?php } ?>

<form method="post" action="createdir.php">

<p><label for="dirname">Name your directory</label>

<br /><input type="text" name="dirname" id="dirname" /></p>



<p><input type="submit" name="btnCreate" id="btnCreate" value="Create Directory" /></p>





</form>



</div>



</body>

</html>
Everything is fine I can create new directory and I can move and delete files form that new directory, however when I try to list its contents I get this reply:
Directory Listing Denied
This Virtual Directory does not allow contents to be listed.
is it something to do with the server and is there a way around this?

Many thanks for all your help Ive nearly got the whole thing working
merlin89
Forum Newbie
Posts: 17
Joined: Thu Nov 10, 2005 7:57 am

cracked it

Post by merlin89 »

Many thanks for all your help have now sorted the problem all works fine will post the completed code soon for evaluation
Post Reply