【发布时间】:2014-03-14 17:27:30
【问题描述】:
这是一个相当冗长的问题,因为我完全迷路了!
概念:用户输入他们希望写入的文本文件,提交后将被发送到用户可以创建形状并将其提交到文本文件的页面,然后使用此数据计算形状区域,选择的颜色等...
问题是我如何写入会话中的文本文件?
这是我在主页上的内容:
<?php
// This line starts the session
session_start();
//The below calls the file
$txtFile = $_POST['submittedTxtFile'];
$_SESSION['submittedTxtFile']= $txtFile;
$file = fopen($txtFile, "r") or exit("That file does not exist");
include_once 'classShapeCollection.php';
//Creates the shapecollection
$shapes = new ShapeCollection();
//These lines get the called file, unserialize the $shapes and serialize them again before entering them into the session.
$buffer = fgets($file);
//Checking if there are any contents in the file
if($buffer)
{
$shapes = unserialize($buffer); //unserialize takes Text and turns it into an object
$_SESSION['serial']= serialize($shapes); //Serialize takes the objects and converts them into Text
}
else //if there is nothing in the file, the session serialises the new ShapeCollection
{
$_SESSION['serial']= serialize($shapes);
}
// Closes the called file
fclose($file);
?>
【问题讨论】:
-
你可以在一行中做到这一点
$txtFile = $_POST['submittedTxtFile'] = $_SESSION['submittedTxtFile'];(链接方法)然后只需使用你现在使用的$file = fopen($txtFile, "r")...你就可以摆脱$txtFile = $_POST['submittedTxtFile']; $_SESSION['submittedTxtFile']= $txtFile; -
file_put_contents($txtfile, serialize($whatever))。这不是火箭科学…… -
@Fred-ii- 抱歉,您能否详细说明一下,因为我对此很陌生,所以不确定您希望我更改什么
-
您的
$file = fopen($txtFile, "r")也存在错误,其中r是只读的。您需要使用w或w+,如下所述。另外,有问题的文件;这将取决于您要使用哪种格式。旁注:如果要添加到文件,需要使用a或a+开关。 -
但是我没有在那个页面上写任何东西,只是阅读使用 sn-ps 就像计算当前存在多少个形状......但是当我尝试添加一个新形状时,我得到这个文件不存在