【问题标题】:How to upload excel file to php server from <input type="file">如何从 <input type="file"> 上传 excel 文件到 php 服务器
【发布时间】:2016-11-29 14:45:31
【问题描述】:

我想使用 php 上传 excel 文件,并想在该 excel 文件中执行一些文件操作。我无法上传文件,如果有人有什么想法请帮忙,在服务器端如何获取文件上传的路径?

$target_dir = '上传/';不适合我。我的文件在 D 中:请帮忙。

PHP 代码:

 <?php    
if(isset($_POST['SubmitButton'])){ //check if form was submitted

$target_dir = 'uploads/';
$target_file = $target_dir . basename($_FILES["filepath"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

require_once dirname(__FILE__) . '/Includes/Classes/PHPExcel/IOFactory.php';

$inputFileType = PHPExcel_IOFactory::identify($target_file);

$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($target_file);

$i=2;
$val=array();
$count=0;
for($i=2;$i<34;$i++)
{
$val[$count++]=$objPHPExcel->getActiveSheet()->getCell('C'.$i)->getValue();
}
//echo'<pre>';print_r($val);
}    
?>

HTML 代码:

<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file"  name="filepath" id="filepath"/></td><td><input type="submit" name="SubmitButton"/>
</body>

【问题讨论】:

    标签: php excel phpexcel


    【解决方案1】:

    首先需要在读取行之前上传文件:

    $target_dir = 'uploads/';
    $target_file = $target_dir . basename($_FILES["filepath"]["name"]);
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    
    move_uploaded_file($_FILES["filepath"]["tmp_name"], $target_file);
    
    // rest of your code...
    

    现在您应该可以继续处理该文件了。 在此之前,该文件从未在您的上传文件夹中。

    【讨论】:

    • 我收到此错误:警告:move_uploaded_file(uploads/test1.xlsx): failed to open stream:
    • move_uploaded_file 应该接收 2 个参数,再看我的示例。你错过了源的 tmp_name
    • 在这个 ($_FILES["filepath"]["tmp_name"], $target_file); ["filepath"] 是什么?
    • “文件路径”来自您的示例。它是输入文件字段的名称
    【解决方案2】:
    if(isset($_POST['SubmitButton'])){
      try {       //attached file formate
        $statement = $db->prepare("SHOW TABLE STATUS LIKE 'table_name'");
        $statement->execute();
        $result = $statement->fetchAll();
        foreach($result as $row)
            $new_id = $row[10]; //10 fixed
    
        $up_filename=$_FILES["filepath"]["name"];
        $file_basename = substr($up_filename, 0, strripos($up_filename, '.')); // strip extention
        $file_ext = substr($up_filename, strripos($up_filename, '.')); // strip name
        $f2 = $new_id . $file_ext;
    
        move_uploaded_file($_FILES["filepath"]["tmp_name"],"uploads/" . $f2);
    
       // Client's info Insert MySQl 
        $statement = $db->prepare("INSERT INTO table_name (files) VALUES (?)");
        $statement->execute(array($f2));
    
        $success_message = "Excel file upload successfully!";
    }
    catch(Exception $e) {
            $error_message = $e->getMessage();
    }
    }
    

    表格代码:

     <form action="" method="post" enctype="multipart/form-data"> <input
     type="file"  name="filepath" id="filepath"/></td><td><input
     type="submit" name="SubmitButton"/>
     </form>
    

    【讨论】:

      【解决方案3】:

      你没有写代码上传文件。

       move_uploaded_file()
      

      【讨论】:

      • 你能详细说明一下吗?
      • 你说你的文件没有上传。目前你的文件在临时目录中。你必须使用 move_uploaded_file 将文件移动到目标目录。这里是它的完整参考php.net/manual/en/function.move-uploaded-file.php
      【解决方案4】:

      您必须使用move_uploaded_file() 函数将文件从临时上传目录移动到您想要的目录。

      您需要为 move_uploaded_file() 函数添加 2 个参数 - 您刚刚上传的临时文件(例如 $_FILES["file"]["tmp_name"]),然后是您想要将其移动到的目录......所以它会结束看起来像这样:move_uploaded_file($_FILES["file"]["tmp_name"], $path_of_new_file)

      【讨论】:

      • 我现在正在做,但是我得到了这个错误:警告:move_uploaded_file(uploads/test1.xlsx): failed to open stream, test.xlsx is in D:/ system will come to知道它必须特别从 D: 中获取它,如果有人在 E: 中获取它,那么?
      猜你喜欢
      • 2016-05-25
      • 1970-01-01
      • 2016-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-21
      • 2018-07-16
      • 1970-01-01
      相关资源
      最近更新 更多