【问题标题】:PHP redirect user once file is uploaded with DropzoneJS使用 DropzoneJS 上传文件后,PHP 重定向用户
【发布时间】:2013-10-30 02:44:01
【问题描述】:

问题:

PHP 代码不会使用 header() 将用户重定向到目标页面。

代码(上传.php):

<?php
    session_start();

    $folder      = 'upload';

    if (!empty($_FILES))
    {
        // Set temporary name
        $tmp    = $_FILES['file']['tmp_name'];

        // Set target path and file name
        $target = $folder . '/' . $_FILES['file']['name'];

        // Upload file to target folder
        $status = move_uploaded_file($tmp, $target);

        if ($status)
        {
            // Set session with txtfile name
            $_SESSION['txtfile'] = $_FILES['file']['name'];

            // Redirect user
            header('Location: explorer.php');   
        }
    }
?>

所需功能:

获取 header() 以将用户重定向到 explorer.php。是的,文件已成功上传,没有任何问题。但用户继续停留在同一页面(upload.php)。

【问题讨论】:

  • $status 是真的吗?如果是这样,您需要 exit();
  • @SajunaFernando 我在标题后添加了一个 exit() 没有任何成功。
  • 试试这个:try{ $status = move_uploaded_file($tmp, $target); }catch(异常 $oException){ v​​ar_dump($oException);死; }
  • @SajunaFernando 刚刚完成,文件已上传,仅此而已。
  • 检查您的错误日志。是空的还是有什么错误?

标签: php redirect upload dropzone.js


【解决方案1】:

试试这个:

$status = false;
if (is_uploaded_file($tmp) == true) {
   $status = @move_uploaded_file($tmp, $target);
}
else {
    $status = @copy($tmp, $target);
}

if(file_exists($target)){
    $status = true;
}

if ($status)
{
    // Set session with txtfile name
    $_SESSION['txtfile'] = $_FILES['file']['name'];

     // Redirect user
     header('Location: explorer.php');   
 }

【讨论】:

  • 刚刚尝试了这个解决方案,上传后没有任何反应。文件已上传,error_log 中没有错误。
  • 您能否将其添加到脚本的顶部,以确保再试一次:ini_set('display_errors','On'); error_reporting(E_ALL);
  • 添加了两个错误处理程序。上传文件时没有错误。只是考虑转储 DropzoneJS 并使用其他一些 jQuery 上传文件。这样的问题感觉太麻烦了。
  • 我会 var_dump $_FILES 数组以确保您收到文件。然后稍微删除会话存储部分。除此之外,您的代码看起来不错
  • 试过了,没有任何反应,文件确实像往常一样上传。我知道这些信息不多,但我也很困惑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-18
  • 2015-08-28
相关资源
最近更新 更多