简体   繁体   中英

failed to open stream: Permission denied json decode multiple files

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




} 


} 


?>

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...

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM