用PHP上传文件后执行操作?

I have a page with the following structure:

1. Text

2. Form with SUBMIT button that allows users to upload files

3. PHP code to handle the files

I'd like to complete 2 (wait for user to hit SUBMIT) before going to 3. I'd also like to keep the activity on the same page... right now if a user hits SUBMIT he/she is redirected to the upload.php script.

What's the simplest way to accomplish this, given that I'm new to PHP? Thank you for your time.

Code below:

HTML (I'd like to keep everything on this page):

<html>
<body>

<form action="upload_file.php" method="post" enctype="multipart/form-data">
<br />
<label for="file">Upload Files:</label>
<br />
<br />
<input type="file" name="file1" id="file" /> 
<input type="file" name="file2" id="file" /> 
<input type="file" name="file3" id="file" /> 
<br />
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>


 <?php




//$result = $storageClient->putBlob('testcontainer', 'example.txt', 'C:/example.txt');
// I'll be uploading each of the three uploaded files here
// Syntax: putBlob (ContainerName, NameInStorage, FileLocation)

?> 

Script:

<?php
error_reporting(E_ALL);


for ($i=1; $i<4; $i++)
    {
    $file = "file" . $i;
    if (($_FILES[$file]["size"] < 20000))
      {
      if ($_FILES[$file]["error"] > 0)
        {
        echo "Return Code: " . $_FILES[$file]["error"] . "<br />";
        }
      else
        {
        echo "Upload: " . $_FILES[$file]["name"] . "<br />";
        echo "Type: " . $_FILES[$file]["type"] . "<br />";
        echo "Size: " . ($_FILES[$file]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES[$file]["tmp_name"] . "<br />";


        $moved = move_uploaded_file($_FILES[$file]["tmp_name"], "C:/inetpub/wwwroot/PHP_Ramp/upload/" . $_FILES[$file]["name"]);

        if ($moved) {
            echo "Move: Success <br/>";
        }
        else {
            echo "Move Failed <br/>";
        }


          echo "Stored in: " . "C:/inetpub/wwwroot/PHP_Ramp/upload/" . $_FILES[$file]["name"];
          }
        }

    else
      {
      echo "Invalid file";
      }

}
?>

You cannot achieve this using only PHP. If you want the process to be on the same page (without refresh) you need to use AJAX, which is javascript. Try reading jQuery ajax section.

And as you're uploading file, you cannot do a file upload using pure ajax (the xmlhttprequest object does not support file upload) so you might want to look for plugins to do this, or you can use a hidden IFRAME that your form will target.

1) in the action of the form give name of same file...

2) Give name to submit button lits say name="submit"

3) On the same page:

if(isset($_POST['submit']))
{
  write file handliig code here..
  all file processing code you were doing in that redirected php script do here..
}