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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看