renfley Posted December 15, 2013 Share Posted December 15, 2013 Ok so guys im a little stuck on some code here and could use some help.. ok so i have 3 rows in column and created 3 folder to match them /ticket/ ->folder1 ->folder2 ->folder3 Now at the end of day i might have more folders, folder4,5,6,7 ect... I want to be able to check and create missing folders based on whats missing but i also want it to catch any folders that might have been deleted. here is my code, the reason it doesnt work is that as soon as it finds a folder that doesn't exists, it creates 1 folder but wont create more then that because the confition was met. and the code stops. I have tried many different alternative any input would be awesome //Query DB $sql = "SELECT DISTINCT tn from tic"; $result = mysql_query($sql) or die(mysql_error()); $loc = "../tickets/"; while ($row = mysql_fetch_assoc($result)){ $dir = $loc . $row['tn']; if(!is_dir($dir)){ mkdir($dir); } elseif(is_dir($dir)){ } } exit(); Quote Link to comment Share on other sites More sharing options...
Solution Psycho Posted December 15, 2013 Solution Share Posted December 15, 2013 Hmm, my reading of that code is that it would create a folder for every instance in the result set which does not exist. The only thing that is out of place is the elseif() which does absolutely nothing. But, it wouldn't make the code exit the process early. The code shoudl look like this //Query DB $sql = "SELECT DISTINCT tn from tic"; $result = mysql_query($sql) or die(mysql_error()); $loc = "../tickets/"; while ($row = mysql_fetch_assoc($result)) { $dir = $loc . $row['tn']; if(!is_dir($dir)) { mkdir($dir); } } exit(); Have you verified the results of your query to see that it contains what you think it should? Quote Link to comment Share on other sites More sharing options...
renfley Posted December 15, 2013 Author Share Posted December 15, 2013 I did i made sure that the actual folder was missing 3 dirs but when i run the script it only creates the first missing one leaving the other not to be created. i am still stuck on this ill try again this morning but any input is awesome Thanks Quote Link to comment Share on other sites More sharing options...
ignace Posted December 15, 2013 Share Posted December 15, 2013 That's because you need to specify the third parameter. mkdir mkdir($dir, 0777, true); Quote Link to comment Share on other sites More sharing options...
renfley Posted December 15, 2013 Author Share Posted December 15, 2013 Thanks Psycho... is was my error, your snippet does loop perfectly i wasn't closing the while correctly Thanks again. Quote Link to comment Share on other sites More sharing options...
renfley Posted December 15, 2013 Author Share Posted December 15, 2013 Hey Ignace insnt the default of mkdir() already set to 0777? Quote Link to comment Share on other sites More sharing options...
ignace Posted December 15, 2013 Share Posted December 15, 2013 Yes, but if you want to specify the third parameter, you'll also have to specify the second one. Quote Link to comment 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.