douheng8629 2019-06-07 09:07 采纳率: 100%
浏览 62

如何通过单击调用php文件来处理上传的文件

I'm a javascript and php beginner and I've been trying to get a solution for 2 days now and it might be easy but I couldn't find any solutions. So, i have my index.php with a file input and a button and I want that when the user clicks on the button it executes a js function that will call my excelparse.php file to process the uploaded file but I dont know how i can call the excelparse.php file on the function. How do I call the excelparse.php file on my on.click function?

    //index.php
    <form>
<div class="form-group">
    <label for="ficheiro">Select a file</label>
    <input type="file" class="form-control-file" id="ficheiro">
</div>
<button type="button" id="parse" class="btn btn-light">Parse</button>
</form>

    //js.js
    parse.on("click", function() {

    });

    //excelparse.php
    $path_parts = pathinfo($_FILES["file"]["name"]);
    $extension = $path_parts['extension'];
    if ($extension == 'xls') {
        $extension = 'Xls';
    }
    elseif ($extension == 'xlsx') {
        $extension = 'Xlsx';
    }

    move_uploaded_file($_FILES['file']['tmp_name'],('files/tmp'));

    $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($extension);
    $spreadsheet = $reader->load("files/tmp");
    $sheet = $spreadsheet->getActiveSheet();        
    foreach ($sheet ->getRowIterator() as $row) {
        $cellIterator = $row ->getCellIterator();
        $cellIterator->setIterateOnlyExistingCells(false);
        $array = array();
        foreach ($cellIterator as $cell) {
            if (!is_null($cell)) {
                $value = $cell->getFormattedValue();
                $array[] = $value;                                        
            }                
        }
        print_r($array);
    }
  • 写回答

1条回答 默认 最新

  • doujiang7258 2019-06-07 09:17
    关注

    You can not execute PHP commands in HTML file like Javascript.

    PHP is a server side language, It will execute in server and push response to client.

    Here you need to use AJAX to call a URL (your php file) in javascript (jQuery is easy).

    jQuery Ajax Request

    A simple jQuery Ajax request be like,

        function callPhp() {
    
          $.ajax({
            url: "http://server.com/yourPhpFileUrl.php"
          }).done(function() {
             alert(0);
          });
    
       }
    

    In your code

    You can submit a file using form submit method,

    <form action='http://server.com/yourPhpFileUrl.php' method='post' enctype='multipart/form-data>
    

    Otherwise

    You can also send file using AJAX request

    File upload using Ajax

    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等