【问题标题】:How to pass $_FILE from Page1 to page3 via Page2如何通过第 2 页将 $FILE 从第 1 页传递到第 3 页
【发布时间】:2014-02-14 03:28:49
【问题描述】:

我正在尝试使用 PHPExcel 上传一个 excel 文件。此过程涉及3个文件- Page1.php

<table width="100%">
<form  action ="" method="post" enctype="multipart/form-data">
 <input type="hidden" name="action" value="uploadexcel">
   <tr>
      <td width="30%">Upload Student-Enquiry File Here:</td>
      <td><input type="file" name="studentfile"></td>
      <td><input type="submit" name="uploadexcel" value="Upload"></td>
   </tr>
</form>
</table>

Page2.php

<?php
if (!isset($_SESSION['eschools']['admin_user']) || $_SESSION['eschools']['admin_user']=="" ) {
   header('location: ./?pid=1&unauth=0');
   exit;
}
if ($action =="uploadexcel"){
     if($uploadexcel="Upload"){
        $inputFileName =$_FILES['studentfile']['tmp_name'];
    $filepath=$_FILES['studentfile']['name'];
    $url = "exam_excel/page3.php";
        header("Location: ".$url);
     }
}
?>

Page3.php 这个页面实际上是把 excel 数据上传到 MySQL 表中,我需要在这里处理选择的 excel 文件 -

<?php session_start();
include("../../includes/constants.inc.php"); //Database configuration File

if(isset($_FILES['studentfile']['tmp_name']))
if(isset($inputFile))
{   
    require_once 'Classes/PHPExcel.php';
    require_once 'Classes/PHPExcel/IOFactory.php';
    $inputFileName =$_FILES['studentfile']['tmp_name'];

//Read your Excel workbook
    try {
        $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
        $objReader = PHPExcel_IOFactory::createReader($inputFileType);
        $objPHPExcel = $objReader->load($inputFileName);
    } catch (Exception $e){
        die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME).'": ' . $e->getMessage());
}

//  Get worksheet dimensions
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow();
        $highestColumn = $sheet->getHighestColumn();

//  Loop through each row of the worksheet in turn
        for ($row = 2; $row <= $highestRow; $row++)
        {
            //  Read a row of data into an array
            $temp=array("");
            $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
            foreach($rowData[0] as $k=>$v)
            {
                array_push($temp,$v);
            }
            if(!empty($temp[45]) &!empty($temp[46]) )
            {
                $tccase="Yes";
            }
            else
            {
            $tccase="No";
            }
            $q="insert into es_enquiry(eq_submitedon,eq_application_no,eq_class,class_sift,eq_name,eq_sex,eq_dob,age,month,day,blood_g,scat_id,ehandi,e_ews,e_bpl,e_sgc,eq_mothername,father_name,eq_countryid,eq_emailid,office_address,office_phone,resident_phone,dist_from_kv,eq_permaddress,domicile,  no_of_transfer,parent_cate_id,kv_tc_no,kv_tc_date,last_class_cgpa,documents,mother_occu,father_occu,eq_city,eq_state,eq_zip,per_city,per_state,per_zip,per_country,mid_name,last_name,tc_case,eq_from_aca,eq_to_aca,eq_tempaddress,present_country,cam_image) values('".$temp[5]."','".$temp[3]."','".$temp[4]."','".$temp[2]."','".$temp[6]."','".$temp[9]."','".$temp[10]."','".$temp[11]."','".$temp[12]."','".$temp[13]."','".$temp[14]."','".$temp[17]."','".$temp[40]."','".$temp[41]."','".$temp[42]."','".$temp[43]."','".$temp[20]."','".$temp[18]."','".$temp[15]."','".$temp[16]."','".$temp[22]."','".$temp[23]."','".$temp[29]."','".$temp[36]."','".$temp[30]."','".$temp[39]."','".$temp[38]."','".$temp[37]."','".$temp[45]."','".$temp[46]."','".$temp[44]."','".$temp[47]."','".$temp[21]."','".$temp[19]."','".$temp[25]."','".$temp[26]."','".$temp[28]."','".$temp[31]."','".$temp[32]."','".$temp[35]."','".$temp[33]."','".$temp[7]."','".$temp[8]."','".$tccase."','".$temp[49]."','".$temp[50]."','".$temp[24]."','".$temp[27]."','".$temp[8]."')";
            mysql_query($q)or die(mysql_error());
            unset($temp);
        }
        echo "Your data inserted successfully";
}
else
{
echo "Select Excel FIle";
}
?>

如果直接使用 Page1.php 中的 Page3.php 使用 Action="Page3.php" 可以正常工作,但未经身份验证我不会直接访问。我尝试了各种方法来获取 Page3.php 上的选定文件名,但获取临时名称。我尝试通过像这样的 URL 传递 temp_name $url = "exam_excel/page3.php?inputFile=".$inputFileName;并接收它 $filename=$_GET['inputFile'];然后我尝试在 page3.php 上使用此变量代替 inputFileName 但失败了。

就这个问题给出一些建议/指导

【问题讨论】:

    标签: php


    【解决方案1】:

    当文件上传被传递到 PHP 页面时,您需要立即处理该文件,方法是将其移动到更永久的存储位置或处理其内容。您不能将其传回给用户,因为无法预先填写&lt;input type=file&gt;,并且期望用户再次上传可能很大的文件是不合理的。如果你对上传的文件不做任何事情,PHP 会在请求结束时销毁它,然后它就消失了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-22
      • 2018-08-23
      • 1970-01-01
      • 1970-01-01
      • 2018-05-08
      • 1970-01-01
      • 2020-05-13
      • 2021-03-24
      相关资源
      最近更新 更多