PHP 在使用 DropzoneJS 上传文件后重定向用户


PHP redirect user once file is uploaded with DropzoneJS

问题:

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() 以将用户重定向到资源管理器.php。是的,文件已成功上传,没有任何问题。但用户继续保持在同一页面上(上传.php)。

试试这个:

$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');   
 }