无法使用 fopen/fwrite/fclose (php) 在 mysql 中保存 xml 文件内容

Can't save xml file content in mysql with fopen/fwrite/fclose (php)

所以我正在用 fopen/fwrite/fclose 在 php 中写一个 XML 文件,然后试图将该信息保存到 MySQL数据库BLOB字段;但是,我 运行 遇到了一个问题,在将信息保存到 MySQL 之后,所有文件包含 "Resource id #33" 和 none 我试图保存的信息。

$blahXMLFile = fopen($_POST['scenBlahNewName'].".xml","wb");
fwrite($blahXMLFile, "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>");
fwrite($blahXMLFile, "<Blah>");
fwrite($blahXMLFile, "<Name>");
fwrite($blahXMLFile, $_POST['scenBlahNewName']);
fwrite($blahXMLFile, "</Name>");
fwrite($blahXMLFile, "</Blah>");
fclose($blahXMLFile);

$sql = "INSERT INTO blah (xmlFile) 
                      VALUES ('$blahXMLFile');";

if($conn->query($sql) === TRUE) {
    } else { echo "<script>alert('Error: " . $sql . "<br>" . $conn->error . "');</script>";
        echo "Error: " . $sql . "<br>" . $conn->error;
    } //end else

然后我将 $blahXMLFile php 变量保存在 BLOB 字段在我的 MYSQL 数据库中,“Resource id #33” 就是文件中的全部内容。如果我使用 javascript 来提醒 php 变量,情况也是如此。

我错过了什么?

编辑:尝试根据评论在此处正确使用 fread:

$blahXMLResource = fopen($_POST['scenBlahNewName'].".xml","w");
$blahXMLFile = fread($blahXMLResource, filesize($_POST['scenBlahNewName'].".xml")); 

因为那根本就不再保存文件了。

好的,我明白了;在 @chris85 的建议下,我将我的 fopen/fwrite/fclose 替换为 file_put_contents(我使用的是 file_get_contents,但每次都是附加而不是覆盖文件)。

$blahXMLFile = $_POST['scenBlahNewName'].".xml";

$blahXMLFileContent = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<Blah xmlns='uri:/mil/tatrc/physiology/datamodel' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation=''>   
    <Name>".$_POST['scenBlahNewName']."</Name>
</Blah>";

file_put_contents($blahXMLFile, $blahXMLFileContent);

echo "<script>alert('".addslashes($blahXMLFileContent)."');</script>";

$blahXMLFileContent 是我最终放入 MySQL 数据库的内容。