duanba3707 2015-11-23 20:20
浏览 71
已采纳

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

So I'm writing an XML file in php with fopen/fwrite/fclose and then attempting to save that information into a MySQL database BLOB field; however, I'm running into an issue where after saving the information into MySQL, all the file contains is "Resource id #33" and none of the information that I was trying to save.

$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

And then after that I take the $blahXMLFile php variable and try to save it in the BLOB field in my MYSQL database and "Resource id #33" is all that's in the file. This is also the case if I use javascript to alert the php variable.

What am I missing?

EDIT: Trying to use fread here correctly based off of a comment:

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

Because that isn't saving a file at all anymore.

  • 写回答

1条回答 默认 最新

  • donglu7286 2015-11-23 23:21
    关注

    Alright, so I got it; after advice from @chris85 I replaced my fopen/fwrite/fclose with file_put_contents (I was using file_get_contents, but that was appending rather than overwriting the files each time).

    $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 is what I eventually put into the MySQL database.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器