2012-10-26 82 views
1

我正在測試php,因爲我是這個問題的新生。我把我的php代碼放在一個免費的服務器上,他們讓我做我自己的index.php,管理一些php變量(比如register_globals,magic_quotes_gpc等,我把它們作爲默認值),但顯然我可以處理一個以上的文件PHP代碼,例如:在PHP中管理多個文件

<?php 

//--------Updating Data------------- 
$cdc = intval($_POST['cantidadDeCapitulos']); 
$toWrite = array('ctot' => $cdc); 
for($i=1;$i<$cdc+1;$i += 1){ 
    $toWrite["cap".$i] = $_POST['numdeCap'.$i]; 
}//--------------------------------- 

$datos = file_get_contents("myfile.json."); 

$toWrite = json_encode($toWrite); 

//Open a file in write mode 

$fp = fopen("myfile2.json", "w"); 

if(fwrite($fp, "$toWrite")) { 
    echo "&verify=success&"; 
} else { 
    echo "&verify=fail&"; 
} 
fclose($fp); 
?> 

如果我註釋掉線$datos = file_get_contents("myfile.json.");這是正常的!東西是寫在myfile2.json但如果取消註釋,數據不會被更新。這兩個文件都具有權限666,並且它們位於相同的目錄中,即/ root。

+0

嘗試777 666 – RobinJ

+0

@RobinJ 666代替仍將允許寫作。 – SomeKittens

+0

@SomeKittens我知道,但我想你可能需要執行權限。 – RobinJ

回答

1
$datos = file_get_contents("myfile.json."); 

似乎發生了錯字。從你的文件中取出最後一個點。我的意思是,行更改爲:

$datos = file_get_contents("myfile.json"); 
0

試試這個:

<?php 
$file = 'myfile.json'; 
// Open the file to get existing content 

$current = file_get_contents($file); 

// Write the contents back to the file 
file_put_contents($file, $current); 
?>