Jump to content

loop through files in directory


JonnyEnglish
Go to solution Solved by requinix,

Recommended Posts

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);

}
?>
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

  • Solution

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.