【发布时间】:2010-10-11 10:55:09
【问题描述】:
我正在尝试上传包含多条记录的文本文件,但我不断收到 syntax() 错误。 如果文件中只有一条记录,则实现的逻辑可以正常工作。 问题可能是我在 foreach 循环中回显了多条记录吗?
说明:
用户浏览文件的Ext JS界面。一旦文件被选中,它就会被上传到服务器并在 PHP 中解析。解析后的进程称为processFile 由js文件调用。
我注意到了什么
- 如果文件中只有一条记录,则实现的逻辑可以正常工作。
- json格式正确。
- 如果文件包含 3 条记录,则仍会读取第四条 BLANK 记录。
这里是 PHP 代码:
<?php
$action = $_REQUEST['action'];
if ($action == 'uploadFile') {
$fileName = $_FILES['file']['tmp_name'];
$fileContent = file_get_contents($fileName);
$fileInfo = parseFile($fileContent); //custom formatting
echo wrap_answer(array(
'originalFile' => $fileContent,
'fileInfo' => $fileInfo,
'success' => true
));
}
if ($action == 'processFile') {
$fileContent = $_REQUEST['file'];
$fileInfo = parseFile($fileContent);
foreach($fileInfo['lines'] as $line) {
$input = array(
'id' => $line['id'],
'name' => $line['name']);
//custom function to insert records from file into clients table
//function returns the inserted records ID
$insert_id = handler_Insert('clients',$input);
$success = ($insert_id == null || $insert_id == 0)?false:true;
echo json_encode(array(
'success' => $success)
);
//NOTE:Processing records 1 by 1 and echoing the result
// Could this be the error? Any suggestions to better
// handle this process?
}
}
非常感谢任何帮助、建议等! 谢谢
【问题讨论】:
标签: php javascript ajax extjs weblogic