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

时间:2015-01-21 12:57:13

标签: php json

我正在尝试将多个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); 
} 




} 


} 


?>

3 个答案:

答案 0 :(得分:2)

您无法在C:\ Users \ Richard \ Desktop \上打开文件 将其复制到您的app文件夹中:例如:exemple / mysite / html / jsonfiles /

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

$ json var包含你所有的json代码...... Adpat代码......

答案 1 :(得分:0)

您需要单独阅读文件,您可以使用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的最后,您将拥有所有文件内容。

答案 2 :(得分:0)

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

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

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