hotwire Posted November 18, 2008 Share Posted November 18, 2008 Hello forum and moderators, I am having a problem generating a predefined directory that will check if the directory exists and if so just add a number and keep checking if exist. For example, the predefined directory is named 'memalb', and I want each new directory to be named, 'memalb1' memalb2, memalb3 etc.. The coding I am currently using makes each new directory, memalb1, memalb12, memalb123, etc.. here is my current coding: $y=0; $xdir="memalb"; $ckd= file_exists($xdir); if ($ckd==1){ while ($ckd==1){ $xdir=$xdir.$y; $ckd=file_exists($xdir); ++$y; } } mkdir($xdir); Thanks for any help on this.. Link to comment https://forums.phpfreaks.com/topic/133186-solved-help-generating-specific-directories/ Share on other sites More sharing options...
JasonLewis Posted November 18, 2008 Share Posted November 18, 2008 Like this? $dir = "memalb"; $check = file_exists($dir); $i = 0; while($check){ $i++; $check = file_exists($dir.$i); } if($i == 0){ //Original folder does not exist! Create it. }else{ $folder = $dir.$i; echo $folder; } Link to comment https://forums.phpfreaks.com/topic/133186-solved-help-generating-specific-directories/#findComment-692675 Share on other sites More sharing options...
hotwire Posted November 18, 2008 Author Share Posted November 18, 2008 Hi ProjectFear, Thanks for the coding, It had 1 minor error I fixed which was the "if (i == 0)" should be "if ($check == 0)".. However it does work.. Thanks Alot for your help, it is much appreciated.. Thank You! hotwire Link to comment https://forums.phpfreaks.com/topic/133186-solved-help-generating-specific-directories/#findComment-692711 Share on other sites More sharing options...
JasonLewis Posted November 19, 2008 Share Posted November 19, 2008 if($i == 0) would work also. Think about it, if the original folder doesn't exist the loop wouldn't even run once, so $i would still be set to 0. Link to comment https://forums.phpfreaks.com/topic/133186-solved-help-generating-specific-directories/#findComment-693106 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.