1

I have a html form to take file input and on upload , upload.php is called and the file is uploaded to the server. I want to stay on the same page and display the upload status on a label in the form. I am using a session variable that gets updated in upload.php , i am echoing $_SESSION['status'] into the label.

<form action="upload.php" method="post" enctype="multipart/form-data">
               <div id="fileDiv">
                   <?php $_SESSION['status']=" ";?>
                <h3>Select file to upload:</h3>
                 <div class="form-group"><input type="file" name="fileToUpload" id="fileToUpload" required="true" ></div>
                 <div class="form-group"><input type="submit" value="Upload" name="submit" id="fileUpload"></div>
                 <div class="form-group"><label><?php echo $_SESSION['status']?></label></div>
               </div>
             </form>  

upload.php

     <?php
session_start();
include ("./PHPExcel/IOFactory.php");
include_once('./PHPExcel/PHPExcel.php');

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Allow only Excel doc
if($FileType != "xlsx" && $FileType != "xls" ) {
    $_SESSION['status']= "Only excel(.xls or .xlsx) files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) 
    $_SESSION['status']="file was not uploaded.";

 else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) 
        $_SESSION['status']="File uploaded";
    else 
      $_SESSION['status']="error uploading your file.";
} ?>

Is there an AJAX solution which is alternative to enctype="multipart/form-data" ? . The file uploads successfully, just that I want to stay on the same page.

sai
  • 135
  • 1
  • 8
  • Possible duplicate of [jQuery Ajax File Upload](https://stackoverflow.com/questions/2320069/jquery-ajax-file-upload) – Script47 Jun 07 '18 at 04:02
  • you can use ajax with processData: false, contentType: false, options – Devsi Odedra Jun 07 '18 at 04:03
  • @Script47 , I don't know how to relate it, please help with a specific solution. – sai Jun 07 '18 at 04:07
  • I have seen many librarys that make this automated for you. Search for an Uploader or Upload helper. For example https://blueimp.github.io/jQuery-File-Upload/ would something like that work for you? –  Jun 07 '18 at 04:15
  • @Dknacht , there is no problem in uploading, I would like to stay with the current method. – sai Jun 07 '18 at 04:23

0 Answers0