2016-09-23 58 views
1

我有這樣的代碼:PHP的警告信息上傳成功

<?php 
if (isset ($_FILES['UploadFileField'])){ 
    $UploadName = $_FILES['UploadFileField'] ['name']; 
    $UploadName = mt_rand (100000, 999999).$UploadName; 
    $UploadTmp = $_FILES['UploadFileField'] ['tmp_name']; 
    $UploadType = $_FILES['UploadFileField'] ['type']; 
    $FileSize = $_FILES['UploadFileField'] ['size']; 
    $UploadName = preg_replace("#[^a-z0-9.]#i", "", $UploadName); 
    if(($FileSize > 1250000)){ 
    die ("Error - File to Big"); 
    } 
    if(!$UploadTmp) { 
    die ("No File Selected"); 
    } 
    else { 
    move_uploaded_file($UploadTmp, "Upload/$UploadName"); 
    } 
    header('Location: /index.php'); 
    exit; 
} 
?> 

此代碼的工作,但我需要做到這一點上傳文件後插入成功的消息。 謝謝!

+0

打開[錯誤檢查](http://php.net/manual/fr/function.error-reporting.php#85096) –

+0

在會話中設置消息。 – Dave

回答

1
if (move_uploaded_file($UploadTmp, "Upload/$UploadName")) { 
    $message = "Successfully inserted"; 
    header('Location: /index.php?success=true&message='.$message); 
} 
else { 
    $message = "Something went wrong"; 
    header('Location: /index.php?success=false&message='.$message); 
} 

使用,如果條件爲move_uploaded_file功能它會幫助你。並獲得成功,消息從索引文件

if ($_GET['success'] == true) { 
    echo $_GET['message']; 
} 

或者你可以使用會話

1

您可以添加參數,當你重定向,如:

header('Location: /index.php?upload=true'); 

,並在你的索引檢查,如果你獲取參數並在顯示消息的情況下顯示消息。你也可以檢查if else語句,如果上傳工作和更改var發送