我想从index.php中的表单中获取文件,然后我将该文件发送到第二页中的另一个表单(upload1.php) 并再次发送到第三页(以第二种形式提供额外数据)。
请查看此示例以查看不足,我的意思更好。
的index.php
<html>
<body>
<form action="upload1.php">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
现在我将文件发送到此表单:
和第二个文件:(upload1.php)
这是一个问题:如何在没有双重上传的情况下将文件发送到此表单? 我知道这种形式是错的...但我不知道如何解决它。
<form action="upload2.php" method="post"
enctype="multipart/form-data">
<div style="display:none">
<input type="file" name="file" id="file" value="<?php echo $_FILES["file"]?>">
</div>
<input type="text" name="name" value="name of file">
<input type="submit" name="submit" value="Submit">
</form>
和第三个文件打印并存储结果:(upload2.php)
<?php
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
} else {
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
?>
=============================================== =====================
编辑:
我试过通过会话但是文件无法存储在上传文件夹中! 指数:PHP
<html>
<body>
<form action="upload1.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
upload1.php
<?php session_start();
echo "<br>".$_SESSION['tmp_name']=$_FILES["file"]["tmp_name"];
echo "<br>".$_SESSION['error']=$_FILES["file"]["error"];
echo "<br>".$_SESSION['name']=$_FILES["file"]["name"];
echo "<br>".$_SESSION['type']=$_FILES["file"]["type"];
echo "<br>".$_SESSION['size']=$_FILES["file"]["size"];
?>
<form action="upload2.php" method="post"
enctype="multipart/form-data">
<input type="text" name="name" value="name of file">
<input type="submit" name="submit" value="Submit">
</form>
和upload2.php
<?php
session_start();
if ($_SESSION['error'] > 0) {
echo "Return Code: " .$_SESSION['error'] . "<br>";
} else {
echo "Upload: " . $_SESSION['name'] . "<br>";
echo "Type: " . $_SESSION['type'] . "<br>";
echo "Size: " . ($_SESSION['size'] / 1024) . " kB<br>";
echo "Temp file: " . $_SESSION['tmp_name'] . "<br>";
if (file_exists("upload/" . $_SESSION['name'])) {
echo $_SESSION['name'] . " already exists. ";
} else {
move_uploaded_file($_SESSION['tmp_name'],
"upload/" . $_SESSION['name']);
echo "Stored in: " . "upload/" . $_SESSION['name'];
}
}
?>
答案 0 :(得分:0)
您可以将上传文件的文件位置或文件内容本身存储在会话变量中。您需要在需要访问它的每个连续PHP脚本中初始化会话。但是,查看您的脚本我不知道为什么需要将其分布在多个页面中。您可以将前两个脚本合并为一个表单,然后将最后一个脚本用作表单处理程序。这将减轻通过会话维持状态的任何需要。