JonnyEnglish Posted March 12, 2017 Share Posted March 12, 2017 Hi guys, I like to run this script for each of the json files in a folder but I’m struggling to get it working and would appreciate some guidance =) <?php error_reporting(E_ALL|E_STRICT); ini_set('display_errors', true); include_once('db.txt'); ///-----------------first effort--------------------------------------------- /// runs without an error but it doesnt produce anything //$files = glob('C:\php\Inventories\Json.{json}', GLOB_BRACE); //foreach($files as $file) { ///-----------------second effort--------------------------------------------- /// this produces this interesting error: PHP Warning: file_get_contents(C:\php\Inventories\Json): failed to open stream: Permission denied in C:\php\Inventories\index.php on line 17 (thats line 27 in this example) $dir = "C:\php\Inventories\Json"; foreach(glob($dir) as $file) { ///-------------------------------------------------------------- $json = file_get_contents("$file"); $json = json_decode($json, true); ///-------------------------------------------------------------- $name1 = array(); $name1[] = $json['user_data']['name']; ///------------------- DECK1 ------------------------------------ $deck1 = array(); $id = $json['user_decks']['1']['deck_id']; $com = $json['user_decks']['1']['commander_id']; $deck1[] = $commander_id = $d[$com][0]; $dom = $json['user_decks']['1']['dominion_id']; $deck1[] = $dominion_id = $d[$dom][0]; $card = $json['user_decks']['1']['cards']; foreach ($card as $cardid => $cardqty){ $deck1[] = $g = $d[$cardid][0] . "-" . $d[$cardid][1] . "(" . $cardqty . ")"; } ///------------------- INVENTORY -------------------------------- $inventory = array(); foreach ($json['user_cards'] as $row => $v){ if ($v['num_owned'] > 0){ $inventory[] = $d[$row][0] . "-" . $d[$row][1] . "(" . $v['num_owned'] . ")" . "\n"; } } sort ($inventory); $name2 = implode($name1); $deck2 = implode(",",$deck1); $inventory2 = implode($inventory); $file = $name2 . ".txt"; file_put_contents($file, $deck2, FILE_APPEND); file_put_contents($file, $inventory2, FILE_APPEND); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/303429-loop-through-files-in-directory/ Share on other sites More sharing options...
requinix Posted March 12, 2017 Share Posted March 12, 2017 "C:\php\Inventories\Json" How many files or directories do you have matching that pattern? Exactly one. Quote Link to comment https://forums.phpfreaks.com/topic/303429-loop-through-files-in-directory/#findComment-1544118 Share on other sites More sharing options...
NigelRel3 Posted March 12, 2017 Share Posted March 12, 2017 It looks as thought the user that is running the php process (not sure who this is on Windows) doesn't have read access to these files. Are these files under your webserver root directory (where your PHP scripts are)? (You can rung phpinfo() to check this) Are the permissions similar to the .php files your running? Quote Link to comment https://forums.phpfreaks.com/topic/303429-loop-through-files-in-directory/#findComment-1544120 Share on other sites More sharing options...
Solution requinix Posted March 12, 2017 Solution Share Posted March 12, 2017 The issue is that file_get_contents doesn't work on directories - "Permission denied" is Windows' weird way of telling you that. If you want all files in a directory, and including the paths with the names, then use a simple * wildcard with glob(). glob($dir . "\\*")And note the escaped backslash since it's a double-quoted string. (Or use forward slashes.) The fact that "C:\php\Inventories\Json"worked without them is a coincidence: there are no \p \I \J metacharacters. Quote Link to comment https://forums.phpfreaks.com/topic/303429-loop-through-files-in-directory/#findComment-1544128 Share on other sites More sharing options...
JonnyEnglish Posted March 12, 2017 Author Share Posted March 12, 2017 Got it, thank you for your help! Quote Link to comment https://forums.phpfreaks.com/topic/303429-loop-through-files-in-directory/#findComment-1544130 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.