klturi421 Posted December 27, 2011 Share Posted December 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253896-having-trouble-with-creating-a-directory/ Share on other sites More sharing options...
scootstah Posted December 27, 2011 Share Posted December 27, 2011 So why not do if (!file_exists("../images/upload/upimg/$folder")) { so that you make sure you are checking the right place? Quote Link to comment https://forums.phpfreaks.com/topic/253896-having-trouble-with-creating-a-directory/#findComment-1301600 Share on other sites More sharing options...
Pikachu2000 Posted December 27, 2011 Share Posted December 27, 2011 You aren't checking for the directory's existence in the same path as where you're trying to create it. Quote Link to comment https://forums.phpfreaks.com/topic/253896-having-trouble-with-creating-a-directory/#findComment-1301603 Share on other sites More sharing options...
klturi421 Posted December 27, 2011 Author Share Posted December 27, 2011 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."; } Quote Link to comment https://forums.phpfreaks.com/topic/253896-having-trouble-with-creating-a-directory/#findComment-1301606 Share on other sites More sharing options...
Pikachu2000 Posted December 27, 2011 Share Posted December 27, 2011 Look at what you're doing with the $error variable. Why are you assigning a non-existent $_POST element to it after using it in the !file_exists() function? Actually, why are you trying to change it at all after that point? Quote Link to comment https://forums.phpfreaks.com/topic/253896-having-trouble-with-creating-a-directory/#findComment-1301612 Share on other sites More sharing options...
klturi421 Posted December 27, 2011 Author Share Posted December 27, 2011 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."; } Quote Link to comment https://forums.phpfreaks.com/topic/253896-having-trouble-with-creating-a-directory/#findComment-1301615 Share on other sites More sharing options...
Pikachu2000 Posted December 27, 2011 Share Posted December 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253896-having-trouble-with-creating-a-directory/#findComment-1301617 Share on other sites More sharing options...
klturi421 Posted December 27, 2011 Author Share Posted December 27, 2011 Edit: I did it again. I corrected it this time. Even without the post line it is still returning an error. Warning: mkdir() [function.mkdir]: File exists on line 5 Warning: mkdir() [function.mkdir]: File exists on line 6 Quote Link to comment https://forums.phpfreaks.com/topic/253896-having-trouble-with-creating-a-directory/#findComment-1301618 Share on other sites More sharing options...
Pikachu2000 Posted December 27, 2011 Share Posted December 27, 2011 To me, it looks like the $folder variable is actually empty. Echo it and make sure it holds the value you'd expect it to hold. Quote Link to comment https://forums.phpfreaks.com/topic/253896-having-trouble-with-creating-a-directory/#findComment-1301632 Share on other sites More sharing options...
klturi421 Posted December 27, 2011 Author Share Posted December 27, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/253896-having-trouble-with-creating-a-directory/#findComment-1301658 Share on other sites More sharing options...
jasonc Posted December 27, 2011 Share Posted December 27, 2011 could you please post your complete code for this upload page. Quote Link to comment https://forums.phpfreaks.com/topic/253896-having-trouble-with-creating-a-directory/#findComment-1301661 Share on other sites More sharing options...
klturi421 Posted December 27, 2011 Author Share Posted December 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253896-having-trouble-with-creating-a-directory/#findComment-1301669 Share on other sites More sharing options...
jasonc Posted December 27, 2011 Share Posted December 27, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/253896-having-trouble-with-creating-a-directory/#findComment-1301683 Share on other sites More sharing options...
klturi421 Posted December 28, 2011 Author Share Posted December 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253896-having-trouble-with-creating-a-directory/#findComment-1301855 Share on other sites More sharing options...
jasonc Posted December 28, 2011 Share Posted December 28, 2011 The @ is used to suppress any errors that may otherwise show up. Quote Link to comment https://forums.phpfreaks.com/topic/253896-having-trouble-with-creating-a-directory/#findComment-1301858 Share on other sites More sharing options...
klturi421 Posted December 28, 2011 Author Share Posted December 28, 2011 works for me. Quote Link to comment https://forums.phpfreaks.com/topic/253896-having-trouble-with-creating-a-directory/#findComment-1301865 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.