gpt4 book ai didi

php - 文件上传返回上传html页面

转载 作者:搜寻专家 更新时间:2023-10-31 21:08:02 25 4
gpt4 key购买 nike

我希望在文件上传后返回到上一页,并在上传页面上显示“文件上传成功”。

在顶部的 upload.php 中,我放置了

sesssion_start();

在我放置的文件上传脚本的末尾

$_SESSION['upload_success'] = TRUE;
header("Location: stream.php");

现在我知道我需要将一些代码放入 html 文档中,但不确定需要放入什么。下面是我的 html 表单脚本

<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="90000000" />
Select video to upload:
Please choose a file: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />

我知道它会是类似这样的东西,但不确定我会如何或在哪里放置它。

 session_start();
if (isset($_SESSION['upload_success']) && $_SESSION['upload_success']) {
echo "File uploaded successfully";
}

如果有人能指导我将 HTML 代码添加到正确的位置,我将非常感激

在评论之后,我修改了我的 php 代码,使其看起来像这样。

<?php error_reporting(E_ALL); ini_set('display_errors', 1);
sesssion_start();
$target_path = "upload/";
$target = $target_path . basename($_FILES['uploadedfile']['name'] );
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'] , $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name'] ). " has been uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
$_SESSION['upload_success'] = TRUE;
header("Location: stream.php");
exit();

stream.php 里面的语法为:

    <?phpsession_start();
if (isset($_SESSION['upload_success']) && $_SESSION['upload_success']) {
echo "File uploaded successfully";
}
?>

谢谢,

标记

最佳答案

注意:你也不能同时使用 echo 和 header,因为那样会被认为是在 header 之前输出,所以我们只使用 session 数组作为消息和 header 重定向到“upload_form.php”,然后显示各自的之后该页面上的消息。

也可以使用 session_destroy() 来销毁任何之前的 session 。

旁注:使用两个单独的文件。

HTML 表单:称之为“upload_form.php

<?php 
session_start();
session_destroy();
?>

<form action="stream.php" method="post" enctype="multipart/form-data">

<input type="hidden" name="MAX_FILE_SIZE" value="90000000" />
Select video to upload:
Please choose a file: <input name="uploadedfile" type="file" /><br />

<input type="submit" value="Upload File">
</form>

<?php
if(isset($_SESSION['upload_success'])){
echo $_SESSION['upload_success'];
}

else{
echo "Please select a file.";
}

?>

PHP(文件 2):称其为“stream.php

<?php 
session_start();

$target_path = "upload/";
$target = $target_path . basename($_FILES['uploadedfile']['name'] );

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'] , $target))
{
$_SESSION['upload_success'] = "File successfully uploaded.";
header("Location: upload_form.php");
exit;
}

else {
$_SESSION['upload_success'] = "Sorry, there was a problem uploading your file.";

header("Location: upload_form.php");
exit;
}

编辑:

if(move_uploaded_file...

之后修改并添加如下内容
if(isset($_FILES['uploadedfile']) && !empty($_FILES['uploadedfile'])){
$target_path = "upload/";
$target = $target_path . basename($_FILES['uploadedfile']['name']);
}

关于php - 文件上传返回上传html页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28996553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com