简体   繁体   English

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

[英]Perform an action after Uploading files with 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. 我想在完成2之前完成2(等待用户点击SUBMIT)。我还希望将活动保持在同一页面上...现在,如果用户点击提交,他/她被重定向到upload.php脚本。

What's the simplest way to accomplish this, given that I'm new to PHP? 考虑到我是PHP新手,最简单的方法是什么? Thank you for your time. 感谢您的时间。

Code below: 代码如下:

HTML (I'd like to keep everything on this page): HTML(我想在此页面上保留所有内容):

<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. 仅使用PHP无法实现此目的。 If you want the process to be on the same page (without refresh) you need to use AJAX, which is javascript. 如果您希望进程位于同一页面上(无需刷新),则需要使用AJAX,即javascript。 Try reading jQuery ajax section. 尝试阅读jQuery ajax部分。

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. 当您上传文件时,您无法使用纯ajax进行文件上传(xmlhttprequest对象不支持文件上传),因此您可能希望查找插件来执行此操作,或者您可以使用隐藏的IFRAME表单将使用目标。

1) in the action of the form give name of same file... 1)在表单的动作中给出相同文件的名称...

2) Give name to submit button lits say name="submit" 2)给出名称提交按钮lits say name =“submit”

3) On the same page: 3)在同一页面上:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM