2014-05-17 56 views
-1

我寫一個簡單的文件編輯器PHP file_put_contents人品問題

這裏是代碼:

<?php 
$file_path = "/home/user/file.php"; 
$file = file_get_contents($file_path); 
print' <form method="post"> 
    <textarea name="content" rows="20" cols="150">'; 
    echo $file; 
    print'</textarea> 
    <input type="submit" name="save" value="save !"> 
    </form>'; 
if (isset($_POST['save'])) { 


    $content = $_POST['content']; 
    if (file_exists($file_path)) { 
    echo "$file_path Updated"."<br>"; 
    file_put_contents($file_path,$content); 


    } 
    else { 
    echo "The file $filenames does not exist"; 
    file_put_contents($file_path, ''); //Create empty file 
    } 
} 

在這個文件$file我有一個像

<div class="banner"> 
<a href="http://google.com/" target="_blank" rel="nofollow"> 
<img alt="" src="http://google.com/logo.png" ></a> 
<br> 
</div> 

一些HTML代碼,但是當我保存通過這個腳本,這是

<div class=\"banner\"> 
<a href=\"http://google.com/\" target=\"_blank\" rel=\"nofollow\"> 
<img alt=\"\" src=\"http://google.com/logo.png\" ></a> 
<br> 
</div> 

它在localhost中工作正常,我不知道什麼是問題,

+2

你可能有[魔術引號(http://www.php.net/manual/en/security.magicquotes.php)上。 ..如果你把這個代碼至少有一個寶寶小貓會死亡... –

回答

0

magic_quotes_gpc已啓用。禁用它php.ini還是在全球這樣的事情包括:

if(get_magic_quotes_gpc()) { 
    $_POST = array_map('stripslashes', $_POST); 
    $_GET = array_map('stripslashes', $_GET); 
    $_COOKIE= array_map('stripslashes', $_COOKIE); 
} 
+0

添加到檢查,看看函數'get_magic_quotes_gpc'實際上是否存在,因爲它已被刪除在更高版本的PHP,但理想情況下,你只是禁用它在'php.ini'中 –

+0

也許是PHP 6,但不是任何版本5.在'magic_quotes_gpc'已被棄用的版本中'get_magic_quotes_gpc()'將始終返回'false'。 – AbraCadaver

+0

Aah我的壞,PHP文檔說:「總是返回FALSE,因爲魔術引號功能已從PHP中刪除。」,我想他們完全刪除了魔術引號的功能。 –