【问题标题】:Php write line/strine once to codephp写行/字符串一次编码
【发布时间】:2017-06-03 23:35:20
【问题描述】:

我正在使用 php 进行搜索功能,我也需要将</pages> 添加到我正在编写的文件的末尾。 </pages> 只能写一次,最后。

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]
["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image

// Check if file already exists
if (file_exists($target_file)) {
echo "ERR: 418 (File all ready exists)\n";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "ERR: 418 (File too large)\n";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "txt") {
echo "ERR: 418 (Supported file types - .txt)\n";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],                 
$target_file)) {
    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). "     
has been uploaded.";
} else {
    echo "Sorry, there was an error uploading your file.";
}
}

$contentsbro = file_get_contents("links.xml");
$myfile = fopen("links.xml", "w") or die("Unable to open file!");
$txt = "<link><title>". basename( $_FILES["fileToUpload"]["name"]). "
</title>
<url>https://fidead.000webhostapp.com/uploads/". basename( 
$_FILES["fileToUpload"]["name"]). "</url>
</link>";
$txto = $contentsbro . $txt;
fwrite($myfile, $txto);
fclose($myfile);

?>

我需要知道如何在最后写一次字符串,否则会报错。

【问题讨论】:

    标签: php html xml text


    【解决方案1】:

    大概你有这样的东西(有更多条目):

    <?xml version="1.0" encoding="UTF-8" ?>
    <pages>
        <url>https://fidead.000webhostapp.com/uploads/filename.jpg</url>
    </pages>
    

    您可能应该使用 XML 解析器来正确执行此操作:

    # Loads the content of the file
    $XML    =   simplexml_load_file('links.xml');
    # Adds to the URL array
    $XML->addChild('url','https://fidead.000webhostapp.com/uploads/newfile.jpg');
    # Saves the file back
    file_put_contents('links.xml',$XML->asXml());
    

    给你:

    <?xml version="1.0" encoding="UTF-8"?>
    <pages>
        <url>https://fidead.000webhostapp.com/uploads/filename.jpg</url>
    <url>https://fidead.000webhostapp.com/uploads/newfile.jpg</url></pages>
    

    编辑:

    代替:

    $contentsbro = file_get_contents("links.xml");
    $myfile = fopen("links.xml", "w") or die("Unable to open file!");
    etc....
    

    你应该试试:

    # Loads the content of the file
    $XML    =   simplexml_load_file('links.xml');
    # Make a new link child
    $link   =   $XML->addChild('link');
    # Create a new child under the link for title
    $link->addChild('title',basename($_FILES["fileToUpload"]["name"]));
    # Create a new child under the link for url
    $link->addChild('url','https://fidead.000webhostapp.com/uploads/'.basename( $_FILES["fileToUpload"]["name"]));
    # Saves the file back
    file_put_contents('links.xml',$XML->asXml());
    

    【讨论】:

    • 我对php知之甚少,能否给我看一下完成的代码
    • 网址上方还有个标题
    • 所以看起来像这样&lt;pages&gt;&lt;title&gt;name&lt;/title&gt; &lt;url&gt;https://fidead.000webhostapp.com/uploads/filename.jpg&lt;/url&gt; &lt;/pages&gt;
    • addChild() 忽略其他所有内容,只是将子元素添加到 &lt;url&gt; 数组中。您只需放弃 f*() 函数,转而使用 xml 类/方法 addChild()file_put_contents() 将文件保存回来。
    • 我还是卡住了。我不知道如何以及在哪里放置代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 2013-04-01
    • 2019-11-17
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多