0

I am trying to decode multiple json files to a database I have created a folder which has 2 json files in it but I get the error as

failed to open stream: Permission denied

using following code

$json = file_get_contents('C:\Users\Richard\Desktop\JSON_Files'); 

code implemented as per answers

<?php 


$con=mysqli_connect("localhost","root","","json_map");
$response = array(); 
$res=array(); 
$json='';
foreach (glob("C:\xampp\htdocs\laravel\awsconfig\app\JSON_Files\*.json") as $filename) {
$json.= file_get_contents($filename); 
}

if($json!=null){ 
$decoded=json_decode($json,true); 
//$decode= var_dump($decoded); 
//$ss=$decode["array"]; 
//echo $decoded['number']; 

if(is_array($decoded["configurationItems"])) 
{ 
foreach($decoded["configurationItems"] as $configurationItems) 
//for($i=0;$i>sizeof($decoded["configurationItems"]);$i++) 

 { 


$configurationItemVersion=$configurationItems["configurationItemVersion"]; 
echo "<br />","configuration_Item_Version:",$configurationItemVersion,"<br />"; 

$configurationItemCaptureTime=$configurationItems["configurationItemCaptureTime"]; 
echo "configurationItemCaptureTime:",$configurationItemCaptureTime,"<br />"; 

 $configurationStateId=$configurationItems["configurationStateId"]; 
  echo "configurationStateId:",$configurationStateId,"<br />"; 

 $awsAccountId=$configurationItems["awsAccountId"]; 
 echo "awsAccountId:",$awsAccountId,"<br />"; 

$configurationItemStatus=$configurationItems["configurationItemStatus"]; 
echo "configurationItemStatus:",$configurationItemStatus,"<br />"; 

$resourceId=$configurationItems["resourceId"]; 
echo "resourceId:",$resourceId,"<br />"; 

$ARN=$configurationItems["ARN"]; 
echo "ARN:",$ARN,"<br />"; 


$awsRegion=$configurationItems["awsRegion"]; 
echo "awsRegion:",$awsRegion,"<br />"; 

   $availabilityZone=$configurationItems["availabilityZone"]; 
   echo "availabilityZone:",$availabilityZone,"<br />"; 

$configurationStateMd5Hash=$configurationItems["configurationStateMd5Hash"]; 
echo "configurationStateMd5Hash:",$configurationStateMd5Hash,"<br />"; 

$resourceType=$configurationItems["resourceType"]; 
echo "resourceType:",$resourceType,"<br />"; 


$resourceCreationTime=$configurationItems["resourceCreationTime"]; 
echo "resourceCreationTime:",$resourceCreationTime,"<br />"; 



$result = mysqli_query($con, "INSERT INTO        configuration_item(configuration_item_version,configuration_item_capture_time,configuration_state_id, aws_account_id, configuration_item_status, resource_id, arn, aws_region, availability_zone,configuration_state_md5_hash, resource_type, resource_creation_time)



VALUES('$configurationItemVersion','$configurationItemCaptureTime','$configurationStateId','$awsAccountId','$configurationItemStatus','$resourceId','$ARN','$awsRegion','$availabilityZone','$configurationStateMd5Hash','$resourceType','$resourceCreationTime' )")or die("Insert Failed ".((is_object($con)) ? mysqli_error($con) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));; 

}



// check if row inserted or not 
if ($result) { 
    // successfully inserted into database 
    $response["code"] = 1; 
    $response["message"] = "successfully stored configuration items "; 

    // echoing JSON response 
    echo json_encode($response); 
} else { 
    // failed to insert row 
    $response["code"] = 2; 
    $response["message"] = "Oops! An error occurred."; 

    // echoing JSON response 
    echo json_encode($response); 
} 




} 


} 


?>
Richard Hewitt
  • 345
  • 1
  • 5
  • 22

3 Answers3

2

You can't open file on C:\Users\Richard\Desktop\ Copy it into your app folder: for exemple /mysite/html/jsonfiles/

<?php
$json='';
foreach (glob("/mysite/html/jsonfiles/*.json") as $filename) {
    $json.= file_get_contents($filename); 
}

the $json var contains all your json code... Adpat code...

Imaginaerum
  • 769
  • 8
  • 22
  • I have implemented your code added to question my code as now when I try and decode it just comes up blank any ideas ? – Richard Hewitt Jan 21 '15 at 13:39
  • @ imaginaerum see [ http://stackoverflow.com/questions/28035019/php-how-to-map-json-with-element-name-in-form-somethingsomethingsomething] its quite substantial just take it that b efore I implemented your code and pointed to a single file it worked but now I get a blank screen so its obviously happy with the file location thanks – Richard Hewitt Jan 21 '15 at 14:10
  • foreach (glob("/mysite/html/jsonfiles/*.json") as $filename) { $json = file_get_contents($filename); [YOUR CODE] } – Imaginaerum Jan 21 '15 at 14:13
0

You need to read the files individually, you can use the glob function try the following:

// Specify file pattern 
$json_files = glob("C:\\Users\\Richard\\Desktop\\JSON_Files\\*.json");
$all_files_contents = '';

// Iterate over the found files
foreach($json_files as $json_file) {
   $all_files_contents .= file_get_contents($json_file);
}

At the end in $all_files_contents you will have all files contents.

Plamen Nikolov
  • 2,643
  • 1
  • 13
  • 24
0

First of all, I am guessing that you are trying to open a file existing in the client side (your local computer and not the server).

PHP code is server-side code. This means that you are trying to open a file in the server and not the computer that is the client. You probably can not open the client file server-side, but it also seems you do not have the permissions to open the file even in the client (take a look here).

What you can simply do is put the file into an application folder (server-side) and then try to open it, as Imaginaerum mentioned.

Community
  • 1
  • 1
Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54