PHP - fopen():无法在上传的文件上打开流

时间:2015-10-12 09:01:25

标签: php fopen fgetcsv

我试图上传一个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 directoryfgetcsv() expects parameter 1 to be resource, boolean given

2 个答案:

答案 0 :(得分:1)

您正在尝试打开不存在的文件。

函数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");

答案 1 :(得分:-1)

文件不以其原始名称存储。

这是实际文件:

 $_FILES['userfile']['tmp_name']

在精细手册中阅读更多内容:

http://be2.php.net/manual/en/features.file-upload.post-method.php