2016-03-30 6 views
0

私はトークンページを持っています。画像をアップロードするユーザーが必要です。終了したら終了できませんでしたが、完了後、 「tはアップロードしたり、タスクを完了するが、私はページを削除した場合、それは完全に罰金ここで適用ロジックから任意のヘルプは、コードの太線は、そのredirection`になるという問題があるコードがあるPHPはタスクを成功裏に完了した後、別のページにユーザーを誘導する必要があります

<?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."; 
    } 

} 

?>` 
+0

解決策の1つは、リダイレクトヘッダーに役立つ「出力バッファリング」です。異なる適切な答えが得られない場合、ここに情報とのリンクがあります。 [出力バッファリング](http://stackoverflow.com/questions/2832010/what-is-output-buffering) – lovermanthing

答えて

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'])をチェックインし、そうでない場合はそれを表示することができます。

+0

ありがとうDavis実際のロジックは、事前に大きな方向感謝になります – Tim

+0

@Tim、詳細については私の編集をチェックしてください。それがあなたのために働く場合、答えを受け入れるようにしてください。何か問題がある場合は教えてください。 – Davis

+0

これは役に立ちました – Tim

関連する問題