将 "manual file upload" 替换为从 "defined directory" 获取文件
replacing "manual file upload" with fetching files from "defined directory"
我遵循的代码最适合使用文件上传器上传文件。
if (isset($_FILES['file'])) {
require_once "simplexlsx.class.php";
$xlsx = new SimpleXLSX( $_FILES['file']['tmp_name'] );
...
list($cols, $rows) = $xlsx->dimension();
...
}
但是,现在我想进一步自动化它。我希望自动从定义的路径获取文件,而不是手动上传它们。到目前为止,我已经想出了以下代码,但它不起作用,
$dir = '../data/';
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
IF(strlen($file)>2) {
echo $file."<br/>";
$dirfile= "{$dir}{$file}";
echo $dirfile;
$filedata = file_get_contents($dirfile);
require_once "simplexlsx.class.php";
//$xlsx = new SimpleXLSX( $_FILES['file']['tmp_name'] );
$xlsx = new SimpleXLSX('{$filedata}');
...
list($cols, $rows) = $xlsx->dimension();
...
我不确定要将什么传递给 SimpleXLSX 函数而不是 $_FILES['file']['tmp_name']。以下根本没有帮助。
之前:$xlsx = new SimpleXLSX( $_FILES['file']['tmp_name']);
之后:$xlsx = new SimpleXLSX('{$filedata}');
我刚刚修复了它,它需要文件路径作为参数。以下是正确的行,最初我正在尝试这个,但有额外的单引号。
$xlsx = new SimpleXLSX({$dirfile});
//where $dirfile = '../data/test.xlsx'
我遵循的代码最适合使用文件上传器上传文件。
if (isset($_FILES['file'])) {
require_once "simplexlsx.class.php";
$xlsx = new SimpleXLSX( $_FILES['file']['tmp_name'] );
...
list($cols, $rows) = $xlsx->dimension();
...
}
但是,现在我想进一步自动化它。我希望自动从定义的路径获取文件,而不是手动上传它们。到目前为止,我已经想出了以下代码,但它不起作用,
$dir = '../data/';
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
IF(strlen($file)>2) {
echo $file."<br/>";
$dirfile= "{$dir}{$file}";
echo $dirfile;
$filedata = file_get_contents($dirfile);
require_once "simplexlsx.class.php";
//$xlsx = new SimpleXLSX( $_FILES['file']['tmp_name'] );
$xlsx = new SimpleXLSX('{$filedata}');
...
list($cols, $rows) = $xlsx->dimension();
...
我不确定要将什么传递给 SimpleXLSX 函数而不是 $_FILES['file']['tmp_name']。以下根本没有帮助。
之前:$xlsx = new SimpleXLSX( $_FILES['file']['tmp_name']);
之后:$xlsx = new SimpleXLSX('{$filedata}');
我刚刚修复了它,它需要文件路径作为参数。以下是正确的行,最初我正在尝试这个,但有额外的单引号。
$xlsx = new SimpleXLSX({$dirfile});
//where $dirfile = '../data/test.xlsx'