获取php中fopen()函数的完整驱动器路径

时间:2015-04-29 04:14:10

标签: php

我有一个CSV文件(sample.csv),放在我的flashdisk(驱动器L :)中。我读了那些文件来更新我的数据库。我尝试在localhost上运行它,一切正常。但如果我上传到互联网服务器的脚本,脚本总是错误的。服务器无法识别我放置文件的磁盘驱动器(磁盘驱动器L :)。这是我之前的第一个剧本:

if (isset($_POST['upload1'])) {
$allowed_ext = array('csv');
$file_name  = $_FILES['file']['name'];
$file_ext   = strtolower(end(explode('.', $file_name)));
$file_path  = realpath($_FILES['file']['name']);
if(in_array($file_ext, $allowed_ext) === true){
$handle = fopen($file_path.'/'.$_FILES['file']['name'], "r" );  
while (! feof($handle)) {
$import=fgets($handle);
}
fclose($handle);
}
}

因为那些脚本不顺利,所以我尝试添加几行来确定故障的位置,这是完整的脚本:

if (isset($_POST['upload1'])) {
$allowed_ext = array('csv');
$file_name  = $_FILES['file']['name'];
$file_ext   = strtolower(end(explode('.', $file_name)));
$file_path  = realpath($_FILES['file']['name']);
if(in_array($file_ext, $allowed_ext) === true){
if (!@fopen($file_path.'/'.$_FILES['file']['name'], 'r')) {
echo $file_path.'/'.$_FILES['file']['name'];
var_dump($php_errormsg);
}else{
$handle = fopen($file_path.'/'.$_FILES['file']['name'], "r" );  
while (! feof($handle)) {
$import=fgets($handle);
}
fclose($handle);
}
}
}

从第二个脚本,我知道磁盘驱动器未知,因为它提供以下消息: /SAMPLE.CSV NULL

/SAMPLE.CSV 是echo $ file_path的输出。' /'。$ _ FILES [' file'] [' name& #39];

NULL 是var_dump($ php_errormsg)的输出;

我的问题是程序的脚本如何从驱动器L读取csv文件:( L:/SAMPLE.CSV ) 感谢您的任何建议。

1 个答案:

答案 0 :(得分:0)

将CSV保存到服务器

if (in_array($file_ext, $allowed_ext) === true)
{
    move_uploaded_file($_FILES['file']['tmp_name'], '/your/directory/yourfile.csv');
}

然后按要求循环显示

if (($handle = fopen("/your/directory/yourfile.csv", "r")) !== FALSE)
{
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
    {
        $fieldOne = trim($data[0]);
        $fieldTwo = trim($data[1]);
        $fieldThree = trim($data[3]);

        // do stuff
    }
    fclose($handle);
} 

完成所需的CSV行后,然后删除文件

unlink('/your/directory/yourfile.csv');