【问题标题】:file upload return to upload html page文件上传返回上传html页面
【发布时间】:2015-05-13 19:53:48
【问题描述】:

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

在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 代码添加到正确的位置,我将非常感激

在 cmets 之后,我将我的 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";
    }
    ?>

谢谢,

标记

【问题讨论】:

  • if (isset($_SESSION['upload_success']) &amp;&amp; $_SESSION['upload_success'] == TRUE) 试试看,如果这就是问题所在。
  • @Markjose 我认为你走在正确的轨道上。尝试并报告发生了什么。
  • 但是,您不能同时使用 echo 和 header,因此如果 echo 与要使用的 header 在同一个文件中,则需要删除它。这就是所谓的“在标题之前输出”。
  • @Fred-ii- 我会在 ` ' 下添加它吗? =>在php标签中?
  • 看起来header在upload.php中,上传html表单在stream.php中。

标签: php html session upload


【解决方案1】:

注意:你也不能同时使用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']);
}

【讨论】:

    【解决方案2】:

    您的代码运行良好,但您应该在回显成功消息后使用 unset 函数删除 session['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
    
    }
    

    【讨论】:

    • 好的修改了代码,但这没有影响。 &lt;?phpsession_start(); if (isset($_SESSION['upload_success'])){ echo "File uploaded successfully"unset( $_SESSION['upload_success']); } ?&gt;
    • 上传成功并创建会话然后回显消息时可以看到影响,但之后即使您的代码不起作用,您仍然有成功消息...
    • 我现在很困惑,因为目前没有消息显示
    【解决方案3】:

    对于快速解决方案,您可以使用 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,因此您需要将 jQuery 库包含在您的 upload.php 页面中(如上所示)。

    【讨论】:

      【解决方案4】:

      放置

      $_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"); 之前回显(或执行任何类型的用户查看的输出)

      页面将在用户读取输出之前切换。

      您的代码当前在您的问题中的编写方式可能会发生以下情况:

      1. 服务器回显“抱歉,上传您的文件时出现问题”,用户永远看不到。
      2. $_SESSION['upload_success'] 然后设置为 TRUE,这显然与 #1 不一致。
      3. 然后它将用户发送到 stream.php,其中有一条成功消息 显示。

      另一种更懒惰的方法是使用不太有用的场景描述来解决您的问题,而不是这样做(在 upload.php 中):

      else 
          {
          die("Sorry, there was a problem uploading your file."); 
          }
      

      希望有帮助!

      【讨论】:

      • 好的,尝试了语法错误,但在第 6 行 syntax error, unexpected ')' in /var/www/html/upload.php on line 6, 第 6 行 if(!file_exists('$target_path' . $_FILES['uploadedfile']['name']*)) 的任何地方都看不到错误
      • 我已经清除了一些错误。看到*了吗?那不应该在那里。这是由堆栈的斜体功能添加的,我试图将路径斜体......我也不认为你应该在 $target_path.我也没有 basename() 。在这一点上,我解决了所有这些问题。对此感到抱歉。
      • 试试if(!file_exists($target_path . basename($_FILES['uploadedfile']['name'])))
      • 根据 apache 日志,else 是意料之外的。但这是唯一的问题。
      • 好吧,我发现我做错了什么。将 basename($_FILES['uploadedfile']['name']))) ? 1 : 0; 更改为 `basename($_FILES['uploadedfile']['name'])) ? 1:0);
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-18
      • 2012-06-07
      • 2012-06-11
      • 2012-01-12
      • 2011-07-07
      相关资源
      最近更新 更多