php需要在成功完成任务后将用户定向到另一个页面

时间:2016-03-30 15:45:30

标签: php forms

我有一个令牌页面,我需要用户上传图片,当完成后无法返回,但是当我完成指示页后,我不会上传或完成任务,但如果我删除页面完成正常任何来自这里应用的逻辑的帮助是代码问题是代码中的粗线是重定向`

<?PHP
session_start();
if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {
    header ("Location: index.php");
}
?>
<?php
$target_dir = "uploads/";
$jobnumber="try_";
$random_digit=rand(0000,9999).$jobnumber;

//combine random digit to you file name to create new file name
//use dot (.) to combile these two variables

$new_file_name=$random_digit. basename($_FILES["fileToUpload"]["name"]);
$target_file = $target_dir .$new_file_name;
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    require_once("mysql_connect.php"); 

    $sql = "INSERT INTO imagepath (jobnumber,imagepath)
VALUES ('$_POST[Name]','$target_file')";

 $result = mysqli_query($connection, $sql);
        **header("location:logout.php");**
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 2000000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";

    } else {
        echo "Sorry, there was an error uploading your file.";
    }

}

?>`

1 个答案:

答案 0 :(得分:1)

如果您向浏览器输出任何内容,则无法执行header。所以这里:

if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {
    header ("Location: index.php");
}
?>
<?php
$target_dir = "uploads/";

您不需要?> <?php(它只是向浏览器发送换行符,破坏了您的header选项),因此请删除这些行。然后,粗略的解决方案是,在echo结果的任何地方,将其放在$_SESSION中,例如:

$_SESSION['upload_error'] = 'File is not an image.';

然后,您需要修改逻辑,以便在$uploadOk == 0将其重定向回上传页面时,以及那个页面,如果$_SESSION['upload_error']不为空,显示它。否则,将它们重定向到别处。如果您需要有关该特定逻辑的帮助,请告诉我。

随着时间的推移,最简单的改变是粗略而快速的解决方案。肯定会有一些优化,但这应该可以满足您的需求。

我们假设图片处理文件为upload.php,您的表单会显示在其他内容上,例如index.php

<?php

    session_start();

    if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {
        header ("Location: index.php");
    }

    $_SESSION['upload_error'] = null;
    $_SESSION['upload_success'] = null;

    $target_dir = "uploads/";
    $jobnumber="try_";
    $random_digit=rand(0000,9999).$jobnumber;

    //combine random digit to you file name to create new file name
    //use dot (.) to combile these two variables

    $new_file_name=$random_digit . basename($_FILES["fileToUpload"]["name"]);
    $target_file = $target_dir . $new_file_name;
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);

    // Check if image file is a actual image or fake image
    if (isset($_POST["submit"])) {
        $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
        if ($check === false) {
            $_SESSION['upload_error'] =  "File is not an image.";
            $uploadOk = 0;
        }
    }

    // Check if file already exists
    if (file_exists($target_file)) {
        $_SESSION['upload_error'] = "File already exists.";
        $uploadOk = 0;
    }

    // Check file size
    if ($_FILES["fileToUpload"]["size"] > 2000000) {
        $_SESSION['upload_error'] = "Your file is too large.";
        $uploadOk = 0;
    }

    // Allow certain file formats
    if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
        $_SESSION['upload_error'] = "Only JPG, JPEG, PNG & GIF files are allowed.";
        $uploadOk = 0;
    }

    if ($uploadOk == 1) {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
            $_SESSION['upload_success'] = "The file " . basename( $_FILES["fileToUpload"]["name"]) . " has been uploaded.";
        } else {
            $uploadOk = 0;  
        }
    }

    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 1) {
        # This is where I make an obligatory comment about how you should not use user-provided data ($_POST['Name']) directly in a query, 
        # which is 100% true, but is a topic for another question
        require_once("mysql_connect.php"); 
        $sql = "INSERT INTO imagepath (jobnumber, imagepath) VALUES ('" . $_POST['Name'] . "', '$target_file')";
        $result = mysqli_query($connection, $sql);

        header("Location: success.php");
    } else {
        header("Location: index.php"); // send them back to the form, where you will display your error message
    }

?>

这将是index.php

的光秃秃的骨架
<?php

    session_start();

    # This will vary a lot depending on if you're in an MVC setup and/or using a templating engine, etc, but basically:

    // display HTML things like a header, etc

    if (!empty($_SESSION['upload_error'])) {
        echo "Sorry, your file was not uploaded: " . $_SESSION['upload_error'];
    }

    // display the rest of your HTML

?>

success.php会很相似,但显然你会显示成功的消息;您可以在index.php上执行此操作,然后检入该文件!empty($_SESSION['upload_success'])并显示该文件(如果不是。