【问题标题】:How to stay on .html after pressing upload button which opens php script?按下打开php脚本的上传按钮后如何保持.html?
【发布时间】:2016-07-12 01:23:00
【问题描述】:

我的.html 文件中有这段代码:

<form action="upload.php" method="post" enctype="multipart/form-data">Select file to upload:        
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload file" name="submit">
</form>

我应该修改代码的哪些部分?

【问题讨论】:

  • 对于这样简单的任务,您可以将 .html 和 .php 合并到一个 PHP 文件中。
  • 你的标题说你需要保持相同的 HTML,但你的问题内容说你想返回到相同的 HTML。是哪一个?

标签: php html file upload


【解决方案1】:

upload.php 的附加代码之后添加以下内容:

header('Location: /path/to/original.html');

它不会停留在原始 .html 文件本身上,但它确实会返回到表单,以便您可以进行更多上传。

【讨论】:

  • 我添加了这段代码,它保留在原始 .html 文件中 :) 这正是我想要的!谢谢!
  • 那么您可以接受它作为正确答案(我的答案左侧的向上/向下投票箭头旁边的灰色复选标记),谢谢 ;)
【解决方案2】:

您可以使用 arizona31 已经提到的 ajax。

其他你也可以让upload.php成为一个类:

<?php
    class upload {
        function __construct($file){
            $this->file = $file;
        }

        function upload_file(){
            // Create the upload code using the $this->file variable.
        }
    }
?>

并使用一些 HTML/PHP 来创建此类的对象:

<form method="post" enctype="multipart/form-data">
    Select file to upload:        
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload file" name="submit">
</form>

<?php
    if(!empty($_POST)){
        $uplaod = new upload($_POST['fileToUpload']);
        $upload->upload_file();
    }
?>

有几种方法可以做到这一点。

【讨论】:

    【解决方案3】:

    你需要使用ajax上传文件到服务器:

    HTML 代码:

    <form action="#" method="post" enctype="multipart/form-data">Select file to upload:        
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload file" name="submitForm" name="submitForm">
    </form>
    

    jQuery 代码可能是这样的:

    $('#submitForm').on('click', function() {
       var file_data = $('#fileToUpload').prop('files')[0];   
       var form_data = new FormData();                  
       form_data.append('file', file_data);                           
       $.ajax({
                url: 'upload.php', // point to server-side PHP script 
                dataType: 'text',  // what to expect back from the PHP script, if anything
                cache: false,
                contentType: false,
                processData: false,
                data: form_data,                         
                type: 'post',
                success: function(php_script_response){
                    alert(php_script_response); // display response from the PHP script, if any
                }
         });
    });
    

    我很想知道这段代码是否适合你

    【讨论】:

    • 我不同意,你不需要需要为此使用 ajax...你可以使用 ajax,但它不是必须才能使其正常工作,请查看我的答案...
    • Webno ...仔细阅读问题...“如何留在.html上”Ajax上传允许您留在页面上...您的解决方案虽然是正确的...不保留如果我错了,请纠正我
    • 我并不是说它不正确,我只是说没有必要达到不必回去的目标......我同意你的看法,如果他们有效 出于某种原因想留在同一页面上,ajax 确实是一个很好的解决方案
    猜你喜欢
    • 1970-01-01
    • 2013-08-06
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多