PHP - fopen():无法在上传的文件上打开流
PHP - fopen(): Failed to Open Stream on uploaded file
我正在尝试上传一个 csv 文件,然后将其内容插入到我的数据库中。我可以成功上传文件,但我的插入查询出错。这是代码的一部分:
//Upload the file
$file_path = "uploads/";
$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
$files = pathinfo($file_path,PATHINFO_EXTENSION);
$uploadstatus = 1;
$found = 0;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// check if the file being submitted is a csv file
$file_type=$_FILES['uploaded_file']['type'];
if ($files=="CSV") {
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
$uploadstatus = 1;
echo "<script type='text/javascript'>alert('File upload successful.')</script>";
} else{
echo "<script type='text/javascript'>alert('File upload failed. Please check then re-upload file.')</script>";
}
}
$filename = $_FILES['uploaded_file']['name'];
if (file_exists($file_path.$filename) && $uploadstatus) {
if (!$found) {
include('conn.php');
//get the file
$handle = fopen($filename,"r");
do {
if (isset($data[0])) {
$data0 = mysql_real_escape_string($data[0]); //rcode
$data1 = mysql_real_escape_string($data[1]); //pcode
$data2 = mysql_real_escape_string($data[2]); //mcode
$data3 = mysql_real_escape_string($data[3]); //bcode
$data4 = mysql_real_escape_string($data[4]); //ecode
$data5 = mysql_real_escape_string($data[5]); //filetype
$data6 = mysql_real_escape_string($data[6]); //rec_count
$data7 = mysql_real_escape_string($data[7]); //gs_count
$data8 = mysql_real_escape_string($data[8]); //be_count
$data9 = mysql_real_escape_string($data[9]); //qc_count
$data10 = mysql_real_escape_string($data[10]); //tt_count
$data11 = mysql_real_escape_string($data[11]); //rm_count
$data12 = mysql_real_escape_string($data[12]); //sc_count
$data13 = mysql_real_escape_string($data[13]); //in_count
$data14 = mysql_real_escape_string($data[14]); //en_count
$data15 = mysql_real_escape_string($data[15]); //co_count
$data16 = mysql_real_escape_string($data[16]); //ve_count
$data17 = mysql_real_escape_string($data[17]); //editing
$query = "INSERT INTO tbltest (rcode,pcode,mcode,bcode,ecode,filetype,rec_count,gs_count,be_count,qc_count,tt_count,rm_count,sc_count,in_count,en_count,co_count,ve_count,ed_count)
VALUES ('$data0','$data1','$data2','$data3','$data4','$data5','$data6','$data7','$data8','$data9','$data10','$data11','$data12','$data13','$data14','$data15','$data16','$data17') ON DUPLICATE KEY UPDATE rec_count=values(rec_count),gs_count=values(gs_count),be_count=values(be_count),qc_count=values(qc_count),tt_count=values(tt_count),rm_count=values(rm_count),sc_count=values(sc_count),in_count=values(in_count),en_count=values(en_count),co_count=values(co_count),ve_count=values(ve_count),ed_count=values(ed_count)";
mysql_query ($query,$connect);
}
} while ($data = fgetcsv($handle,1000,"|"));
}
}
}
回显的结果是正确的文件,但我收到一条错误消息,显示 fopen(): failed to open stream: No such file or directory
和 fgetcsv() expects parameter 1 to be resource, boolean given
。
文件未以其原始名称存储。
这是实际文件:
$_FILES['userfile']['tmp_name']
阅读精品手册:
http://be2.php.net/manual/en/features.file-upload.post-method.php
您正试图打开一个不存在的文件。
函数 move_uploaded_file
完全如其所说 - 它 移动 已上传的文件。调用此函数后,该文件将不再存在于临时上传区。
更改此位:
$filename = $_FILES['uploaded_file']['name'];
if (file_exists($file_path.$filename) && $uploadstatus) {
if (!$found) {
include('conn.php');
//get the file
$handle = fopen($filename,"r");
至:
$filename = $file_path;
if (file_exists($filename) && $uploadstatus) {
if (!$found) {
include('conn.php');
//get the file
$handle = fopen($filename,"r");
我正在尝试上传一个 csv 文件,然后将其内容插入到我的数据库中。我可以成功上传文件,但我的插入查询出错。这是代码的一部分:
//Upload the file
$file_path = "uploads/";
$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
$files = pathinfo($file_path,PATHINFO_EXTENSION);
$uploadstatus = 1;
$found = 0;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// check if the file being submitted is a csv file
$file_type=$_FILES['uploaded_file']['type'];
if ($files=="CSV") {
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
$uploadstatus = 1;
echo "<script type='text/javascript'>alert('File upload successful.')</script>";
} else{
echo "<script type='text/javascript'>alert('File upload failed. Please check then re-upload file.')</script>";
}
}
$filename = $_FILES['uploaded_file']['name'];
if (file_exists($file_path.$filename) && $uploadstatus) {
if (!$found) {
include('conn.php');
//get the file
$handle = fopen($filename,"r");
do {
if (isset($data[0])) {
$data0 = mysql_real_escape_string($data[0]); //rcode
$data1 = mysql_real_escape_string($data[1]); //pcode
$data2 = mysql_real_escape_string($data[2]); //mcode
$data3 = mysql_real_escape_string($data[3]); //bcode
$data4 = mysql_real_escape_string($data[4]); //ecode
$data5 = mysql_real_escape_string($data[5]); //filetype
$data6 = mysql_real_escape_string($data[6]); //rec_count
$data7 = mysql_real_escape_string($data[7]); //gs_count
$data8 = mysql_real_escape_string($data[8]); //be_count
$data9 = mysql_real_escape_string($data[9]); //qc_count
$data10 = mysql_real_escape_string($data[10]); //tt_count
$data11 = mysql_real_escape_string($data[11]); //rm_count
$data12 = mysql_real_escape_string($data[12]); //sc_count
$data13 = mysql_real_escape_string($data[13]); //in_count
$data14 = mysql_real_escape_string($data[14]); //en_count
$data15 = mysql_real_escape_string($data[15]); //co_count
$data16 = mysql_real_escape_string($data[16]); //ve_count
$data17 = mysql_real_escape_string($data[17]); //editing
$query = "INSERT INTO tbltest (rcode,pcode,mcode,bcode,ecode,filetype,rec_count,gs_count,be_count,qc_count,tt_count,rm_count,sc_count,in_count,en_count,co_count,ve_count,ed_count)
VALUES ('$data0','$data1','$data2','$data3','$data4','$data5','$data6','$data7','$data8','$data9','$data10','$data11','$data12','$data13','$data14','$data15','$data16','$data17') ON DUPLICATE KEY UPDATE rec_count=values(rec_count),gs_count=values(gs_count),be_count=values(be_count),qc_count=values(qc_count),tt_count=values(tt_count),rm_count=values(rm_count),sc_count=values(sc_count),in_count=values(in_count),en_count=values(en_count),co_count=values(co_count),ve_count=values(ve_count),ed_count=values(ed_count)";
mysql_query ($query,$connect);
}
} while ($data = fgetcsv($handle,1000,"|"));
}
}
}
回显的结果是正确的文件,但我收到一条错误消息,显示 fopen(): failed to open stream: No such file or directory
和 fgetcsv() expects parameter 1 to be resource, boolean given
。
文件未以其原始名称存储。
这是实际文件:
$_FILES['userfile']['tmp_name']
阅读精品手册:
http://be2.php.net/manual/en/features.file-upload.post-method.php
您正试图打开一个不存在的文件。
函数 move_uploaded_file
完全如其所说 - 它 移动 已上传的文件。调用此函数后,该文件将不再存在于临时上传区。
更改此位:
$filename = $_FILES['uploaded_file']['name'];
if (file_exists($file_path.$filename) && $uploadstatus) {
if (!$found) {
include('conn.php');
//get the file
$handle = fopen($filename,"r");
至:
$filename = $file_path;
if (file_exists($filename) && $uploadstatus) {
if (!$found) {
include('conn.php');
//get the file
$handle = fopen($filename,"r");