无法打开流:权限被拒绝 json 解码多个文件


failed to open stream: Permission denied json decode multiple files

我正在尝试将多个 json 文件解码到数据库中,我创建了一个包含 2 个 json 文件的文件夹,但我收到错误

无法打开流:权限被拒绝

使用以下代码

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

根据答案实现的代码

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


} 

} 

?>

您无法在 C:''Users''Richard''Desktop'' 上打开文件将其复制到您的应用程序文件夹中:例如/mysite/html/jsonfiles/

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

$json var 包含您所有的 JSON 代码...广告代码...

您需要单独读取文件,您可以使用 glob 函数尝试以下操作:

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

$all_files_contents结束时,您将拥有所有文件内容。

首先,我猜您正在尝试打开客户端(本地计算机而不是服务器)中存在的文件。

PHP 代码是服务器端代码。这意味着您正在尝试打开服务器中的文件,而不是作为客户端的计算机。您可能无法在服务器端打开客户端文件,但似乎即使在客户端中,您也没有打开文件的权限(请看这里)。

您可以简单地将文件放入应用程序文件夹(服务器端),然后尝试打开它,正如Imaginaerum所提到的。