php 如何从<input type="file">上传excel文件到php服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38581632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to upload excel file to php server from <input type="file">
提问by anish
I want to upload excel file using to php and wanted to perform some file operation in that excel file. I am not able to upload the file , If any one have some Idea please help , in the server side how to get the path from where the file has been uploaded?
我想使用 php 上传 excel 文件,并想在该 excel 文件中执行一些文件操作。我无法上传文件,如果有人有想法请帮忙,在服务器端如何获取文件上传的路径?
$target_dir = 'uploads/'; is not working for me. and My file is in D: Please help.
$target_dir = '上传/'; 不适合我。我的文件在 D: 请帮忙。
PHP CODE:
代码:
<?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:
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>
采纳答案by Roey Haim
You first need to upload the file before the read line:
您首先需要在读取行之前上传文件:
$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...
now you should be able to continue working on the file. before this, the file never have been in your uploads folder.
现在您应该可以继续处理该文件了。在此之前,该文件从未出现在您的上传文件夹中。
回答by Amranur Rahman
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 code:
表格代码:
<form action="" method="post" enctype="multipart/form-data"> <input
type="file" name="filepath" id="filepath"/></td><td><input
type="submit" name="SubmitButton"/>
</form>
回答by Jeremy Board
You have to move your file from the temporary upload directory to the directory you want it in with the move_uploaded_file()
function.
您必须将您的文件从临时上传目录移动到您希望使用该move_uploaded_file()
功能所在的目录。
There are 2 arguments you need to put for the move_uploaded_file()
function - the temporary file you just uploaded (ex. $_FILES["file"]["tmp_name"]
), and then the directory you want to move it to... So it would end up looking something like this: move_uploaded_file($_FILES["file"]["tmp_name"], $path_of_new_file)
您需要为该move_uploaded_file()
函数设置 2 个参数- 您刚刚上传的临时文件(例如$_FILES["file"]["tmp_name"]
),然后是您想要将其移动到的目录......所以它最终看起来像这样:move_uploaded_file($_FILES["file"]["tmp_name"], $path_of_new_file)
回答by Munish Chechi
U did not write code to upload file.
你没有写代码来上传文件。
move_uploaded_file()