我有csv,有很多专栏,
+---------+------------------+
| NAME | ADDRESS |
+---------+------------------+
| JOHN | ADDRESS 1 |
| MARY | ADDRESS 2 |
+---------+------------------+
这一个将上传到网络应用程序。我的问题是,我想将文件名存储在附加列中(每行的最后一列。)
+---------+------------------+-------------+
| NAME | ADDRESS | NAME FILE |
+---------+------------------+-------------|
| JOHN | ADDRESS 1 | MAIN.CSV |
| MARY | ADDRESS 2 | MAIN.CSV |
+---------+------------------+-------------+
这是我的代码:
$csv = $this->input->post('path'); //PATH CSV
$nama_file = $this->input->post('nama'); // NAME OF FILE
$tryOne = array();
if (file_exists($csv)) {
$file = fopen($csv, 'r'); // r flag is for readonly mode
fgetcsv($file, 1000, ",");
while (( $line = fgetcsv($file) ) !== false) { // if line exists
$tryOne[] = $line; // add to array
}
fclose($file);
}
这样的结果
Array
(
[0] => Array
(
[0] => JOHN
[1] => ADDRESS 1
)
[1] => Array
(
[0] => MARY
[1] => ADDRESS 2
)
)
任何帮助它如此欣赏。
答案 0 :(得分:0)
尝试以下代码...我正在考虑你在$ nama_file变量中获取文件名。
$csv = $this->input->post('path'); //PATH CSV
$nama_file = $this->input->post('nama'); // NAME OF FILE
$tryOne = array();
if (file_exists($csv)) {
$file = fopen($csv, 'r'); // r flag is for readonly mode
fgetcsv($file, 1000, ",");
while (( $line = fgetcsv($file) ) !== false) { // if line exists
array_push($line, $nama_file);
$tryOne[] = $line; // add to array
}
fclose($file);
}
答案 1 :(得分:0)
while (( $line = fgetcsv($file) ) !== false) {
$line[] = $nama_file;
$tryOne[] = $line;
}
是和数组,因此您可以将文件名附加到它。
IFoo