Jump to content


PHP Create Directory


1 reply to this topic

#1 josiec09

    Member

  • Members
  • PipPip
  • 12 posts

Posted 06 August 2010 - 09:21 PM

Ok this is my script to allow some members of my site to create a directory for my image gallery. I was wondering if there is some way to make it check if the directory exists. Not sure if ""if (!file_exists()) {" will work for this or not.

<?php
 if(isset($_POST['Submit'])) 
 {
//Start Config

$homepath = "/home/saiyanzm/public_html/gallery/albums/";

// End config

$dir = $_POST['dir'];
$userDir = $homepath.str_replace( " "  , "_"  , $dir) ;

//create the new directory
$newDir = mkdir($userDir, 0777);


if (!$newDir) {

//If directory not created, end script and display error message

exit('<center>The directry "'.$dir.'" was not created. Please try again or contact an Administrator.</center>');
}

else {
	
	echo '<center>The directory "'.$dir.'" has been created!</center>';
}
 }

?>
<center><form name="newad" method="post" enctype="multipart/form-data">
 <table width="281">
 <tr>
   <td width="116"><div align="right">Directory Name:</div></td>
   <td width="153"><input type="text" name="dir" id="dir" /></td>
 </tr>
 	<tr><td>&nbsp;</td>
 	  <td><input name="Submit" type="submit" value="Create Directory"></td>
 	  </tr>
 </table>	
</form></center>


#2 josiec09

    Member

  • Members
  • PipPip
  • 12 posts

Posted 06 August 2010 - 09:54 PM

You can forget about this one. I found a way around it. The script will not create the directory if it exists it will just show a php error so i hid the error use this

@$newDir = mkdir($userDir, 0777);


and changed the error for this

if (!$newDir) {

//If directory not created, end script and display error message

exit('<center>The directry "'.$dir.'" was not created. Please try again or contact an Administrator. The directory may already exist on the server.</center>');
}






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users