icez Posted October 29, 2010 Share Posted October 29, 2010 Hey, I'm currently making a little script which I often get a string like = "images/folder/something/maybeanotherone/file.ext" so I'm interresting to know how could I create every directory in the correct order and the numbers of sub-directory isn't fix, so if anybody can give me an hint, I would appreciate =D Btw, tell me if I'm you don't understand what I mean, English isn't my primary language. Quote Link to comment https://forums.phpfreaks.com/topic/217250-mkdir-and-a-string-d/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 29, 2010 Share Posted October 29, 2010 Use explode to break it into it's parts, in an array. Use a foreach loop to iterate over the parts in the array. Quote Link to comment https://forums.phpfreaks.com/topic/217250-mkdir-and-a-string-d/#findComment-1128196 Share on other sites More sharing options...
icez Posted October 29, 2010 Author Share Posted October 29, 2010 Use explode to break it into it's parts, in an array. Use a foreach loop to iterate over the parts in the array. Thank you, but how should I know which part of this is actually a directory and not the file at the end. count-1 could work in the loop no? Quote Link to comment https://forums.phpfreaks.com/topic/217250-mkdir-and-a-string-d/#findComment-1128198 Share on other sites More sharing options...
PFMaBiSmAd Posted October 29, 2010 Share Posted October 29, 2010 Use count to get the number of elements. You can then get the file name from the last element and remove that element from the array before you iterate over the folder names. Quote Link to comment https://forums.phpfreaks.com/topic/217250-mkdir-and-a-string-d/#findComment-1128200 Share on other sites More sharing options...
PFMaBiSmAd Posted October 29, 2010 Share Posted October 29, 2010 <?php $path = "images/folder/something/maybeanotherone/file.ext"; $parts = explode('/',$path); $last = count($parts) - 1; // the index starts at zero $filename = $parts[$last]; // you can also use end($parts); unset($parts[$last]); // remove last element (filename) foreach($parts as $part){ if(!is_dir($part)){ mkdir($part); echo "Made: $part<br />"; } chdir($part); // make current working directory be the current part of the path } ?> Quote Link to comment https://forums.phpfreaks.com/topic/217250-mkdir-and-a-string-d/#findComment-1128204 Share on other sites More sharing options...
icez Posted October 30, 2010 Author Share Posted October 30, 2010 <?php $path = "images/folder/something/maybeanotherone/file.ext"; $parts = explode('/',$path); $last = count($parts) - 1; // the index starts at zero $filename = $parts[$last]; // you can also use end($parts); unset($parts[$last]); // remove last element (filename) foreach($parts as $part){ if(!is_dir($part)){ mkdir($part); echo "Made: $part<br />"; } chdir($part); // make current working directory be the current part of the path } ?> It's not working, I tried a few things, and still not working, the problem is that the script doesn't create the sub-directory at the right place... Quote Link to comment https://forums.phpfreaks.com/topic/217250-mkdir-and-a-string-d/#findComment-1128228 Share on other sites More sharing options...
icez Posted October 30, 2010 Author Share Posted October 30, 2010 $parts = explode('/', $element->src); $last = count($parts) - 1; unset($parts[$last]); for($i = 0; $i < $last; $i++) { $download .= $parts[$i].'/'; mkdir($download); echo 'Created:'.$download; } it echo 'Created: (RIGHT PATH GO HERE)' and create every folder perfectly, but when I try to add something to the folder right after this loop, I get ... Warning: file_put_contents(extract/1288399680/images/) [function.file-put-contents]: failed to open stream: No such file or directory in C:\wamp\www\themeforest\index.php on line 26 Quote Link to comment https://forums.phpfreaks.com/topic/217250-mkdir-and-a-string-d/#findComment-1128232 Share on other sites More sharing options...
trq Posted October 30, 2010 Share Posted October 30, 2010 If your using php5 just set the recursive argument to true. See mkdir. Quote Link to comment https://forums.phpfreaks.com/topic/217250-mkdir-and-a-string-d/#findComment-1128234 Share on other sites More sharing options...
icez Posted October 30, 2010 Author Share Posted October 30, 2010 I'm not sure if I understood, but I change mkdir to this : mkdir($download, 0777, true); and still not working Quote Link to comment https://forums.phpfreaks.com/topic/217250-mkdir-and-a-string-d/#findComment-1128370 Share on other sites More sharing options...
trq Posted October 31, 2010 Share Posted October 31, 2010 Post your code and tell us what error you are getting. Quote Link to comment https://forums.phpfreaks.com/topic/217250-mkdir-and-a-string-d/#findComment-1128538 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.