ster
ster

Reputation: 199

php alert message upload successful

I have this code:

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

This code works, but I need insert a message of successful after that is done Upload file. Thank you!

Upvotes: 1

Views: 380

Answers (2)

KrishCdbry
KrishCdbry

Reputation: 1059

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

use the if condition for the move_uploaded_file function it will help you. And get the success, message from index file

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

Or you can use the SESSION

Upvotes: 1

jean-max
jean-max

Reputation: 1654

You can add a parameter when you redirect like :

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

And in your index check if you get the parameter and display a message if it's the case and if it's where you want to display the message. You can check also with if else statement if the upload work and change the var to sent

Upvotes: 1

Related Questions