文件上传 return 上传 html 页
file upload return to upload html page
我希望在文件上传后 return 到上一页,并且在上传页面上有 "file uploaded successfully"。
在顶部的 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";
}
?>
谢谢,
马克
您的代码工作正常,但您应该在回显成功消息后删除带有未设置函数的会话['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
}
放置
$_SESSION['upload_success'] = TRUE;
header("Location: stream.php");
最后,我相信,无论文件上传实际发生了什么,都会设置为真,原因是没有检查条件。
除非脚本在失败时有退出命令,否则它最终会到达它说的部分:"Set the upload success as true and then go to stream.php"而不是说,"If the upload is successful, set the upload success as true and then go to 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");
之前回显(或执行任何类型的输出以供用户查看)
页面将在用户阅读输出之前切换。
您的代码当前在您的问题中的编写方式可能会发生以下情况:
- 服务器回显"Sorry, there was a problem uploading your file",用户永远看不到。
- $_SESSION['upload_success']然后设置为TRUE,这显然不符合#1.
- 然后将用户发送到 stream.php,其中显示成功消息
显示。
另一种使用不太有用的场景描述来解决问题的更懒惰的方法是改为这样做(在 upload.php 中):
else
{
die("Sorry, there was a problem uploading your file.");
}
希望对您有所帮助!
注意:你也不能同时使用 echo 和 header,因为那样会被认为是在 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']);
}
要获得快速解决方案,您可以使用 Ravi Kusuma 的 jQuery File Upload Plugin 或 AJAX 解决方案。
不过,对于上面提出的方案,另一种替代方法是以编程方式构建/输出一个 HTML 表单,其中包含一些 javascript,并将其发送到 POST 一条消息 stream.php
:
CAVEAT: I haven't tried this myself, but I can't think why it wouldn't work. Would someone please confirm my sanity? -- Tested it myself: it works.
<?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 库。
我希望在文件上传后 return 到上一页,并且在上传页面上有 "file uploaded successfully"。
在顶部的 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";
}
?>
谢谢,
马克
您的代码工作正常,但您应该在回显成功消息后删除带有未设置函数的会话['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
}
放置
$_SESSION['upload_success'] = TRUE;
header("Location: stream.php");
最后,我相信,无论文件上传实际发生了什么,都会设置为真,原因是没有检查条件。
除非脚本在失败时有退出命令,否则它最终会到达它说的部分:"Set the upload success as true and then go to stream.php"而不是说,"If the upload is successful, set the upload success as true and then go to 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");
页面将在用户阅读输出之前切换。
您的代码当前在您的问题中的编写方式可能会发生以下情况:
- 服务器回显"Sorry, there was a problem uploading your file",用户永远看不到。
- $_SESSION['upload_success']然后设置为TRUE,这显然不符合#1.
- 然后将用户发送到 stream.php,其中显示成功消息 显示。
另一种使用不太有用的场景描述来解决问题的更懒惰的方法是改为这样做(在 upload.php 中):
else
{
die("Sorry, there was a problem uploading your file.");
}
希望对您有所帮助!
注意:你也不能同时使用 echo 和 header,因为那样会被认为是在 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']);
}
要获得快速解决方案,您可以使用 Ravi Kusuma 的 jQuery File Upload Plugin 或 AJAX 解决方案。
不过,对于上面提出的方案,另一种替代方法是以编程方式构建/输出一个 HTML 表单,其中包含一些 javascript,并将其发送到 POST 一条消息 stream.php
:
CAVEAT: I haven't tried this myself, but I can't think why it wouldn't work. Would someone please confirm my sanity? -- Tested it myself: it works.
<?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 库。