我有一个脚本,它接受一个文本文件并将其插入到db。我已经为xls,xlsx,csv做了exacly脚本,并且工作正常,但是使用.txt文件,我在读取文件时出错。这是我的代码
//html form that uploads the file
<html>
<body>
<form action="" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
//this is my php script
if(isset($_POST['submit'])){
move_uploaded_file($_FILES["file"]["tmp_name"],$_FILES["file"]["name"]);
$file = $_FILES["file"]["name"];
$document = fopen($file,'r');
$contents = fread($document, filesize($file));
fclose($document);
$contents = str_replace("\r\n","|",$contents);
$contents = str_replace("\"","",$contents);
$contents = explode("|",$contents);
$contents = array_chunk($contents, 10);
$count = 0;
for($i = 1; $i < sizeof($contents);$i++){
$categoria = $contents[$i][0];
$ragsoc = $contents[$i][1];
$telefono = $contents[$i][2];
$via = $contents[$i][3];
$comune = $contents[$i][4];
$cap = $contents[$i][5];
$pro = $contents[$i][6];
$reg = $contents[$i][7];
$gas = $contents[$i][8];
$scadenza = convert_date($contents[$i][9]);
$query = "insert into tabela set
categoria='".mysqli_real_escape_string($categoria)."',
ragsoc='".mysqli_real_escape_string($ragsoc)."',
telefono='".mysqli_real_escape_string($telefono)."',
via='".mysqli_real_escape_string($via)."',
comune='".mysqli_real_escape_string($comune)."',
cap='".mysqli_real_escape_string($cap)."',
pro='".mysqli_real_escape_string($pro)."',
reg='".mysqli_real_escape_string($reg)."',
gas='".mysqli_real_escape_string($gas)."',
scadenza='".mysqli_real_escape_string($scadenza)."',
data_ins='$data'";
$result = mysqli_query($query);
if(!$result){
echo mysqli_error();
}
$count++;
//echo $query;exit;
}
echo "Total records :".$count;
}
问题是我无法读取文件。如果我把文件来源像这样
$file = "myfile.txt";
脚本将正常工作,但当我指向这样的脚本
$file = $_FILES["file"]["name"];
我得到一个emty数组()