php警报消息上传成功

时间:2016-09-23 07:28:14

标签: 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;
}
?>

此代码有效,但我需要在完成后插入成功的消息上传文件。 谢谢!

2 个答案:

答案 0 :(得分: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条件,它会对你有所帮助。并从索引文件

获取成功消息
if ($_GET['success'] == true) {
    echo $_GET['message'];
 } 

或者你可以使用SESSION

答案 1 :(得分:1)

您可以在重定向时添加参数,如:

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

在索引中检查是否获得参数并显示消息(如果是这种情况)以及是否显示消息的位置。如果上传工作并且将var更改为已发送

,您也可以使用if else语句进行检查