我希望在文件上传后返回上一页并成功上传"文件"在上传页面上。
在顶部的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";
}
?>
谢谢,
马克
答案 0 :(得分:2)
Nota:你也不能同时使用echo和header,因为这会被认为是在header之前输出,所以我们只使用一个会话数组作为消息,并将标题重定向到“upload_form.php”,然后显示相应的之后该页面上的消息。
使用session_destroy()
也可以销毁以前的所有会话。
旁注:使用两个单独的文件。
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']);
}
答案 1 :(得分:0)
您的代码运行正常,但在执行回显成功消息后,应删除具有未设置功能的会话['upload_success']。 试试
unset( $_SESSION['upload_success'])
在之后的stream.php中
echo "File uploaded successfully";
更新: 如果你想在一个页面上完成所有这些工作,你可以像下面这样做:
if(isset($_SESSION['upload_success']) and $_SESSION['upload_session'])
{
//echo success message
//remove session
}
if(isset($_POST['file'])){
//upload process , if it was successfull make seesion true...
}
else {
//show form
}
答案 2 :(得分:0)
配售
$_SESSION['upload_success'] = TRUE;
header("Location: stream.php");
最后,我相信,无论文件上传的实际情况如何,都会设置为true,没有检查条件。
除非脚本在失败时有退出命令,否则它最终会到达以下部分:“将上传成功设置为true,然后转到stream.php”,而不是说“如果上传成功” ,将上传成功设置为true,然后转到stream.php“
我会尝试:
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
session_start();
if($_FILES['uploadedfile']['size'] == 0)//In other words, if no file was selected.
{
$_SESSION['upload_success'] = 4;//File wasn't selected
header("Location: stream.php");
exit();
}
if(!file_exists('upload/' . basename($_FILES['uploadedfile']['name'])))
{
$_SESSION['upload_success'] = (move_uploaded_file($_FILES['uploadedfile']['tmp_name'],'upload/' . basename($_FILES['uploadedfile']['name'])) ? 1 : 2);
}
elseif(file_exists('upload/' . basename($_FILES['uploadedfile']['name'])))
{
$_SESSION['upload_success'] = 3;
}
header("Location: stream.php");
exit();
?>
现在在stream.php中你有if语句显示消息,而不是这样做:
<?php
session_start();
switch (@$_SESSION['upload_success']) {
case 1:
echo "File uploaded successfully";
break;
case 2:
echo "Sorry, there was a problem uploading your file.";
break;
case 3:
echo "A file with that name already exists!";
break;
case 4:
echo "You must select a file to upload!";
break;
}
unset($_SESSION['upload_success']);
?>//So if you reload stream.php yet another time no messages will be displayed again for no reason. ie. none of the cases will match an unset variable.
最后,在header(Location: "somepage.php");
在用户可以读取输出之前,页面将会切换。
您的代码目前在您的问题中编写的方式可能会发生以下情况:
另一种更有用的场景描述方法也可以解决您的问题(在upload.php中):
else
{
die("Sorry, there was a problem uploading your file.");
}
希望有所帮助!
答案 3 :(得分:0)
要获得快速解决方案,您可以使用Ravi Kusuma的jQuery File Upload Plugin或AJAX解决方案来执行此操作。
但是,上面提出的另一个选择是以编程方式构造/输出带有一些javascript的HTML表单,并让它将消息发送到stream.php
:
CAVEAT:我自己没试过,但我想不出为什么不行。有人请确认我的理智吗? - 自己测试:它有效。
<?php
//upload.php
//Do file upload stuff, then:
$out = '
<form id="frmUpOkay" action="stream.php" method="post">
<input name="upMsg" value="Upload Successful" />
</form>
<script type="text/javascript">
$(function(){
$("#frmUpOkay").submit();
});
</script>
';
echo $out;
?>
您还必须将此位添加到stream.php
文件的顶部:
<?php
if ( isset($_POST['upMsg']) && isset($_POST['upMsg']) != '' ){
$upMsg = $_POST['upMsg']; //you should sanitize this input
}else{
$upMsg = '';
}
?>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<div>
Your normal website content is here.<br>
<br>
Upload message: <?php echo $upMsg; ?> <br>
<br>
</div>
</body>
注意:
以上代码使用jQuery,因此您需要在upload.php
页中包含jQuery库(如上所示)。