簡體   English   中英

試圖讓PHP將字符串發布到文件中

[英]trying to get PHP to post string to a file

我正在嘗試使用PHP以列表項的格式發送表單的值,該列表項包含圖像和要添加到文本文件的標題,然后由另一頁讀取該文本文件。 圖片上傳部分工作正常,但php的輸出看起來像
<li><img alt= " " src=" " > </li>

什么時候應該更像<li><img src="img/comics/sa005.png">Comic 5 - </li>

    <?php
//get item details
$title = $_POST['title'];
$alt = $_POST['alt'];
$caption = $_POST['caption'];
$location = $_POST['location'];

//file location
$file = 'listitems.txt';
// The list item to add to the file


$string = ("\n <li>" . $title . "<img alt= \" " . $alt . " \" src=\" " . $location . " \" > " . $caption . "</li>");

// Write the contents to the file, 
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $string, FILE_APPEND | LOCK_EX);

echo "string = ";
echo $string;
?>

<form action="appendlist.php" method="post">
  <input name="title" type="textbox" placeholder="Title" class="form-control" ><br><br>
  <input name="alt" type="textbox" placeholder="Alt text" class="form-control" ><br><br>
  <input name="caption" type="textbox" placeholder="Image caption" class="form-control" ><br><br>
  <input name="location" type="textbox" placeholder="File location" class="form-control" >
    <br>
<input type="submit" value="Add Image" name="submit">
</form>

您的代碼也是正確的。 但您應該刪除多余的空格。 如果那不起作用,那么另一個問題。

與其嘗試轉義雙引號,不如僅在需要封裝字符串的地方使用單引號

$string = ("\n <li>" . $title . "<img alt='" . $alt . "' src='" . $location . "' > " . $caption . "</li>");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM