Jump to content

Having trouble with creating a directory


klturi421

Recommended Posts

I have a script that will create a directory just fine but, I found a different script that I am trying to have it create a directory if necessary otherwise it will echo that the directory exists. As of right now I am just getting a 'Warning: mkdir() [function.mkdir]: File exists'

 

My current code:

 
if (!file_exists($folder)) {
mkdir ("../images/upload/upimg/$folder", 0777);
mkdir ("../images/upload/thimg/$folder", 0777);
}else {
echo "Directory previously created, file uploaded successfully.";
}

 

Any help would be appreciated.

Link to comment
Share on other sites

I made the change but it is still returning the same error.

 

 

if (!file_exists('../images/upload/upimg/$folder')) {
$folder = $_POST[''];
mkdir ("../images/upload/upimg/$folder", 0777);
mkdir ("../images/upload/thimg/$folder", 0777);
}else {
echo "Directory previously created, file uploaded successfully.";
}

Link to comment
Share on other sites

It wasn't supposed to have the POST, that was an error on my part.

 


if (!file_exists('../images/upload/upimg/$folder')) {
mkdir ("../images/upload/upimg/$folder", 0777);
mkdir ("../images/upload/thimg/$folder", 0777);
}else {
echo "Directory previously created, file uploaded successfully.";
}

Link to comment
Share on other sites

It wasn't supposed to have the POST, that was an error on my part.

 


if (!file_exists('../images/upload/upimg/$folder')) {
$folder = $_POST[''];
mkdir ("../images/upload/upimg/$folder", 0777);
mkdir ("../images/upload/thimg/$folder", 0777);
}else {
echo "Directory previously created, file uploaded successfully.";
}

 

That code is no different than before.

Link to comment
Share on other sites

So I have now ended up with a slightly different code.

 

if (isset($_POST['submit'])) {
	$name = $_POST['gname'];
	mkdir ("../gallery/images/$name", 0777);
	mkdir ("../gallery/thumbs/$name", 0777);	
} else{
	echo "File not uploaded";
}

 

It works for creating the dirs, but I still get the same error as before. Essentially what I am trying to do is have a textarea where the gallery name is input, if the directory exists it just uploads the image to that directory, else it creates the directory then uploads the image. Any ideas or suggestions?

 

I appreciate all that I have received thus far!

Link to comment
Share on other sites

success.php

 


<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>
<title>Auto Thumbnail generation script from plus2net.com</title>
</head>

<body >
<?
if (isset($_POST['submit'])) {
		$name = $_POST['gname'];

		mkdir ("../gallery/images/$name", 0777);
		mkdir ("../gallery/thumbs/$name", 0777);
	} else{
		echo "File not uploaded";

	}	

$add="../gallery/images/$name/".$_FILES[userfile][name]; // the path with the file name where the file will be stored, upload is the directory name. 
//echo $add;
if(move_uploaded_file ($_FILES[userfile][tmp_name],$add)){
echo "Successfully uploaded ".$_FILES[userfile][name]."<br><br>";
chmod("$add",0777);

}else{echo "Failed to upload file Contact Site admin to fix the problem";
exit;}

///////// Start the thumbnail generation//////////////
$n_width=100;          // Fix the width of the thumb nail images
$n_height=100;         // Fix the height of the thumb nail imaage

$tsrc="../gallery/thumbs/$name/".$_FILES[userfile][name];   // Path where thumb nail image will be stored
//echo $tsrc;
if (!($_FILES[userfile][type] =="image/jpeg" OR $_FILES[userfile][type]=="image/gif")){echo "Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";
exit;}
/////////////////////////////////////////////// Starting of GIF thumb nail creation///////////
if (@$_FILES[userfile][type]=="image/gif")
{
$im=ImageCreateFromGIF($add);
$width=ImageSx($im);              // Original picture width is stored
$height=ImageSy($im);                  // Original picture height is stored
$newimage=imagecreatetruecolor($n_width,$n_height);
imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
if (function_exists("imagegif")) {
Header("Content-type: image/gif");
ImageGIF($newimage,$tsrc);
}
elseif (function_exists("imagejpeg")) {
Header("Content-type: image/jpeg");
ImageJPEG($newimage,$tsrc);
}
chmod("$tsrc",0777);
}////////// end of gif file thumb nail creation//////////

////////////// starting of JPG thumb nail creation//////////
if($_FILES[userfile][type]=="image/jpeg"){
$im=ImageCreateFromJPEG($add); 
$width=ImageSx($im);              // Original picture width is stored
$height=ImageSy($im);             // Original picture height is stored
$newimage=imagecreatetruecolor($n_width,$n_height);                 
imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
ImageJpeg($newimage,$tsrc);
chmod("$tsrc",0777);
}
////////////////  End of JPG thumb nail creation //////////
?>

<FORM ENCTYPE="multipart/form-data" ACTION="success.php" METHOD=POST>
If the directory that you would like to select is not present, type in a directory name, description and click submit. The newly created directory should appear within the list below. Otherwise, select the image and directory, click submit and you will be good to go. 
Name <input type="text" name="gname" id="text" value="">
Upload this file: <INPUT NAME="userfile" TYPE="file">

<INPUT TYPE="submit" name="submit" id="submit" VALUE="Send File">
</FORM>

If a gallery is not available, you may create a new one. Enter then name of the Gallery in the Gallery field and upload an image. Otherwise, enter the existing gallery name into the Gallery field and upload an image.<br><br><br>
List of available folders/galleries:<br>
<?php
function folderlist(){
  $startdir = '../gallery/images/';
  $ignoredDirectory[] = '.'; 
  $ignoredDirectory[] = '..';
   if (is_dir($startdir)){
       if ($dh = opendir($startdir)){
           while (($folder = readdir($dh)) !== false){
               if (!(array_search($folder,$ignoredDirectory) > -1)){
                 if (filetype($startdir . $folder) == "dir"){
                       $directorylist[$startdir . $folder]['name'] = $folder;
                       $directorylist[$startdir . $folder]['path'] = $startdir;
                   }
               }
           }
           closedir($dh);
       }
   }
return($directorylist);
}

$folders = folderlist();
  foreach ($folders as $folder){
    $path = $folder['path'];
    $name = $folder['name'];

    echo '' . $name . '<br />';
  }
?>


</body>

</html>

 

 

 

index.php

<FORM ENCTYPE="multipart/form-data" ACTION="success.php" METHOD=POST>
If the directory that you would like to select is not present, type in a directory name, description and click submit. The newly created directory should appear within the list below. Otherwise, select the image and directory, click submit and you will be good to go. 
Name <input type="text" name="gname" id="text" value="">
Upload this file: <INPUT NAME="userfile" TYPE="file">

<INPUT TYPE="submit" name="submit" id="submit" VALUE="Send File">
</FORM>

If a gallery is not available, you may create a new one. Enter then name of the Gallery in the Gallery field and upload an image. Otherwise, enter the existing gallery name into the Gallery field and upload an image.<br><br><br>
List of available folders/galleries:<br>
<?php
function folderlist(){
  $startdir = '../gallery/images/';
  $ignoredDirectory[] = '.'; 
  $ignoredDirectory[] = '..';
   if (is_dir($startdir)){
       if ($dh = opendir($startdir)){
           while (($folder = readdir($dh)) !== false){
               if (!(array_search($folder,$ignoredDirectory) > -1)){
                 if (filetype($startdir . $folder) == "dir"){
                       $directorylist[$startdir . $folder]['name'] = $folder;
                       $directorylist[$startdir . $folder]['path'] = $startdir;
                   }
               }
           }
           closedir($dh);
       }
   }
return($directorylist);
}

$folders = folderlist();
  foreach ($folders as $folder){
    $path = $folder['path'];
    $name = $folder['name'];

    echo '' . $name . '<br />';
  }
?>

 

 

It's very Frankenstein code. I found some information regarding file_exists but I haven't figured out how to write it into the code.

 

Link to comment
Share on other sites

try the following...

 

<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title>Auto Thumbnail generation script from plus2net.com</title>
</head>
<body>
<?
if (isset($_POST['submit'])) {
$name = $_POST['gname'];
		if ($name != "" && !file_exists('../gallery/images/$name')) {
			mkdir ("../gallery/images/$name", 0777);
			mkdir ("../gallery/thumbs/$name", 0777);
		} else {
			echo "Directory previously created, file uploaded successfully.";
			}
}

$add="../gallery/images/$name/".$_FILES[userfile][name]; // the path with the file name where the file will be stored, upload is the directory name. 
//echo $add;
if(move_uploaded_file ($_FILES[userfile][tmp_name],$add)){
echo "Successfully uploaded ".$_FILES[userfile][name]."<br><br>";
chmod("$add",0777);
}else{echo "Failed to upload file Contact Site admin to fix the problem";
exit;}

///////// Start the thumbnail generation//////////////
$n_width=100;          // Fix the width of the thumb nail images
$n_height=100;         // Fix the height of the thumb nail imaage
$tsrc="../gallery/thumbs/$name/".$_FILES[userfile][name];   // Path where thumb nail image will be stored
//echo $tsrc;
if (!($_FILES[userfile][type] =="image/jpeg" OR $_FILES[userfile][type]=="image/gif")){echo "Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";
exit;}
/////////////////////////////////////////////// Starting of GIF thumb nail creation///////////
if (@$_FILES[userfile][type]=="image/gif")
{
$im=ImageCreateFromGIF($add);
$width=ImageSx($im);              // Original picture width is stored
$height=ImageSy($im);                  // Original picture height is stored
$newimage=imagecreatetruecolor($n_width,$n_height);
imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
if (function_exists("imagegif")) {
Header("Content-type: image/gif");
ImageGIF($newimage,$tsrc);
}
elseif (function_exists("imagejpeg")) {
Header("Content-type: image/jpeg");
ImageJPEG($newimage,$tsrc);
}
chmod("$tsrc",0777);
}////////// end of gif file thumb nail creation//////////

////////////// starting of JPG thumb nail creation//////////
if($_FILES[userfile][type]=="image/jpeg"){
$im=ImageCreateFromJPEG($add); 
$width=ImageSx($im);              // Original picture width is stored
$height=ImageSy($im);             // Original picture height is stored
$newimage=imagecreatetruecolor($n_width,$n_height);                 
imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
ImageJpeg($newimage,$tsrc);
chmod("$tsrc",0777);
}
////////////////  End of JPG thumb nail creation //////////
?>
<FORM ENCTYPE="multipart/form-data" ACTION="success.php" METHOD=POST>
If the directory that you would like to select is not present, type in a directory name, description and click submit. The newly created directory should appear within the list below. Otherwise, select the image and directory, click submit and you will be good to go. 
Name <input type="text" name="gname" id="text" value="">
Upload this file: <INPUT NAME="userfile" TYPE="file">
<INPUT TYPE="submit" name="submit" id="submit" VALUE="Send File">
</FORM>
If a gallery is not available, you may create a new one. Enter then name of the Gallery in the Gallery field and upload an image. Otherwise, enter the existing gallery name into the Gallery field and upload an image.<br><br><br>
List of available folders/galleries:<br>
<?php
function folderlist(){
  $startdir = '../gallery/images/';
  $ignoredDirectory[] = '.'; 
  $ignoredDirectory[] = '..';
   if (is_dir($startdir)){
       if ($dh = opendir($startdir)){
           while (($folder = readdir($dh)) !== false){
               if (!(array_search($folder,$ignoredDirectory) > -1)){
                 if (filetype($startdir . $folder) == "dir"){
                       $directorylist[$startdir . $folder]['name'] = $folder;
                       $directorylist[$startdir . $folder]['path'] = $startdir;
                   }
               }
           }
           closedir($dh);
       }
   }
return($directorylist);
}

$folders = folderlist();
  foreach ($folders as $folder){
    $path = $folder['path'];
    $name = $folder['name'];
    echo '' . $name . '<br />';
  }
?>
</body>
</html>

Link to comment
Share on other sites

I appreciate the help from everyone. With jasonc's code I was able to go a little bit further. I found on another forum that the mkdir should have @  in front

 

Original (truncated);

mkdir($name);
mkdir($name);

 

New:

@mkdir($name);
@mkdir($name);

 

Once I changed the code around the warning about file exists went away. I have verified that the script uploads the image to existing folders as well as creates new folders.

 

Again I really appreciate the help. Im not sure how to close the thread but I would say at this point my issue has been resolved.  :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.