鍍金池/ 問(wèn)答/PHP  HTML  Office/ php導(dǎo)入excel 這段代碼怎么修改為指定網(wǎng)站路徑下的excel文件而非上傳

php導(dǎo)入excel 這段代碼怎么修改為指定網(wǎng)站路徑下的excel文件而非上傳

php代碼

if($ta == 'export') {
    $_W['page']['title'] = '批量導(dǎo)入';
    if($_W['ispost']) {
        $file = upload_file($_FILES['file'], 'excel');
        if(is_error($file)) {
            imessage(error(-1, $file['message']), '', 'ajax');
        }
        $data = read_excel($file);
        if(is_error($data)) {
            imessage(error(-1, $data['message']), '', 'ajax');
        }
        unset($data[0]);
        if(empty($data)) {
            imessage(error(-1, '沒(méi)有要導(dǎo)入的數(shù)據(jù)'), '', 'ajax');
        }
        foreach($data as $da) {
            $insert = array(
                'uniacid' => $_W['uniacid'],
                'sid' => $sid,
                'title' => trim($da[0]),
                'displayorder' => intval($da[1]),
                'status' =>  intval($da[2]),
            );
            pdo_insert('tiny_wmall_goods_category', $insert);
        }
        imessage(error(0, '導(dǎo)入商品分類成功'), iurl('store/goods/category/list'), 'ajax');
    }
}

問(wèn)題:

以上代碼為選擇電腦上的excel文件導(dǎo)入;請(qǐng)大神指點(diǎn)下,我想改成導(dǎo)入網(wǎng)站服務(wù)器上指定路徑下excel文件如:/wwwroot/goods_category.xls,請(qǐng)問(wèn)怎么修改。

回答
編輯回答
綰青絲

有兩個(gè)地方要修改.

前端頁(yè)面
把上傳控件修改成文本框. 類似這樣

<input type="file" name="file" />
<!-- 以上修改成以下 -->
<span>XLS 文件名(不帶擴(kuò)展名)</span>
<input type="text" name="xlsfilename" />

這樣你可以在網(wǎng)頁(yè)上輸入待導(dǎo)入的文件名, 而不是上傳本地文件了.

后臺(tái)代碼(你貼出來(lái)的 php 代碼)
把這部分

        $file = upload_file($_FILES['file'], 'excel');
        if(is_error($file)) {
            imessage(error(-1, $file['message']), '', 'ajax');
        }

修改成類似這樣的

        $base_name = $_POST["xlsfilename"];
        // 必須檢查文件名, 防止惡意輸入
        // 合理的文件名只含 a-z, A-Z, 0-9, 及 "-" 和 "_", 最多 30 個(gè)字符.
        if (!preg_match("/^[a-zA-Z0-9_-]{1,30}$/g", $base_name)) {
            die("invalid file name");
            return;
        }
        // 到 "/wwwroot/" 目錄下找指定的 xls 文件
        $file = fopen("/wwwroot/" . base_name . ".xls","r");
2017年1月5日 05:57