如何从<input type =“file”/>上传excel文件到php服务器

时间:2016-07-26 05:26:43

标签: php excel phpexcel

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

$ target_dir ='uploads /';不适合我。我的档案在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 CODE:

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

4 个答案:

答案 0 :(得分: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...

现在您应该可以继续处理该文件了。 在此之前,该文件永远不会出现在您的上传文件夹中。

答案 1 :(得分:0)

U没有写代码来上传文件。

 move_uploaded_file()

答案 2 :(得分:0)

您必须使用namespace blah { std::unordered_map<int, /* funcptr type 0 */> _funcmap_0 { ... } std::unordered_map<int, /* funcptr type 1 */> _funcmap_1 { ... } ... const Bar<Foo0> FSET0 (_funcmap_0); const Bar<Foo1> FSET1 (_funcmap_1); ... } int main() { Quux<blah::FSET0> a; Quux<blah::FSET1> b; ... return 0; } 功能将文件从临时上传目录移动到您想要的目录。

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

答案 3 :(得分:0)

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>