You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Drupal Webform数据无法写入指定目录CSV文件技术求助

Webform提交数据写入CSV失败问题排查与解决

需求与现状

  • 需求:加载Webform表单详情并向指定用户发送邮件
  • 当前问题:能正常加载数据、下载CSV文件,但无法把Webform提交的数据写入到sites/default/files/csv/csvfile.csv这个指定路径的文件里

现有代码

function webform_email_menu() { 
  $items = array(); 
  $items['webform_email'] = array( 
    'title' => 'Webform Email', //page title 
    'description' => 'Webform Information', //description show when mouse hover on link 
    'page callback' => 'webform_list', //callback function which is invoked when menu item is called. 
    'access callback' => true, //any user can access this page 
  ); 
  return $items; 
} 

function webform_list() { 
  module_load_include('inc', 'webform', 'includes/webform.export'); 
  module_load_include('inc', 'webform', 'includes/webform.components'); 
  module_load_include('inc', 'webform', 'includes/webform.submissions'); 

  $submissions = webform_get_submissions(125); 
  echo "installation Type,Date of Purchase,Date of Install ,Outdoor Unit Model,Outdoor Unit Serial,Indoor Unit 1 Model,Indoor Unit 1 Serial,Indoor Unit 2 Model,Indoor Unit 2 Serial,Indoor Unit 3 Model,Indoor Unit 3 Serial,Indoor Unit 4 Model,Indoor Unit 4 Serial,Indoor Unit 5 Model,Indoor Unit 5 Serial,Indoor Unit 6 Model,Indoor Unit 6 Serial,Indoor Unit 7 Model,Indoor Unit 7 Serial,Indoor Unit 8 Model,Indoor Unit 8 Serial,Indoor Unit 9 Model,Indoor Unit 9 Serial,Owner First Name,Owner Last Name,Installation Address 1,Installation Address 2,City,State/Province,Zip/Postal Code,Country ,Owner Phone Number,Owner Email,Contractor Information,Contractor Name,Contractor Address 1 ,Contractor Address 2,City,State/Province,Zip/Postal Code,Country,Contractor Phone Number,Contractor Email,Distributor Name,Distributor Address 1,Distributor Address 2,City,State/Province,Zip/Postal Code,Country,TAC,Privacy Statement\n"; 

  $surveycomponents = db_select('webform_component') 
    ->fields('webform_component') 
    ->condition('nid', 125) 
    ->orderBy('weight') 
    ->orderBy('name') 
    ->execute() 
    ->fetchAllAssoc('cid', PDO::FETCH_ASSOC); 

  $count =0; 
  $i=2; 
  foreach ($submissions as &$submission) { 
    $submission_value=$submission->data; 
    $i=0; 
    foreach($submission_value as $submissionvalue){ 
      $numItems = count($submission_value); 
      if(++$i === $numItems) { 
        echo $csv_export="\n"; 
      }else{ 
        echo $csv_export=$submissionvalue[0].','; 
      } 
    } 
  } 

  $file_open = fopen('sites/default/files/csv/csvfile.csv', "a"); 
  file_put_contents("sites/default/files/csv/csvfile.csv", $csv_export); 
  $csv_handler = fopen ('sites/default/files/csv/csvfile.csv','w'); 
  fwrite ($csv_handler,$csv_export); 
  fclose ($csv_handler); 
}

输入的多维数组

Array ( 
  [0] => Array ( 
    [2] => Array ( [0] => XXXXX ) 
    [3] => Array ( [0] => 2017-06-02 ) 
    [4] => Array ( [0] => 2017-06-16 ) 
    [5] => Array ( [0] => Tested Value 1 ) 
    [6] => Array ( [0] => Tested Value 1 ) 
    [7] => Array ( [0] => Tested Value 1 ) 
    [8] => Array ( [0] => Tested Value 1 ) 
    [9] => Array ( [0] => ) 
    [10] => Array ( [0] => ) 
    [11] => Array ( [0] => ) 
    [12] => Array ( [0] => ) 
    [13] => Array ( [0] => ) 
    [14] => Array ( [0] => ) 
    [16] => Array ( [0] => Tested Value 1 ) 
    [17] => Array ( [0] => Tested Value 1 ) 
    [18] => Array ( [0] => Tested Value 1 ) 
    [19] => Array ( [0] => ) 
    [20] => Array ( [0] => Tested Value 1 ) 
    [21] => Array ( [0] => Tested Value 1 ) 
    [22] => Array ( [0] => Tested Value 1 ) 
    [23] => Array ( [0] => Tested Value 1 ) 
    [24] => Array ( [0] => test@gmail.com ) 
    [26] => Array ( [0] => Tested Value 1 ) 
    [27] => Array ( [0] => Tested Value 1 ) 
    [28] => Array ( [0] => ) 
    [29] => Array ( [0] => Tested Value 1 ) 
    [30] => Array ( [0] => ST ) 
    [31] => Array ( [0] => 47834 ) 
    [32] => Array ( [0] => SH ) 
    [33] => Array ( [0] => test@gmail.com ) 
    [35] => Array ( [0] => Tested Value 1 ) 
    [36] => Array ( [0] => Tested Value 1 ) 
    [37] => Array ( [0] => ) 
    [38] => Array ( [0] => Tested Value 1 ) 
    [39] => Array ( [0] => DJ ) 
    [40] => Array ( [0] => 0225 ) 
    [41] => Array ( [0] => IN ) 
    [42] => Array ( [0] => yes ) 
    [43] => Array ( [0] => ) 
    [44] => Array ( [0] => ) 
    [45] => Array ( [0] => ) 
    [46] => Array ( [0] => ) 
    [47] => Array ( [0] => ) 
    [48] => Array ( [0] => ) 
    [49] => Array ( [0] => ) 
    [50] => Array ( [0] => ) 
    [51] => Array ( [0] => ) 
    [52] => Array ( [0] => ) 
    [53] => Array ( [0] => ) 
    [54] => Array ( [0] => (000) 123-4567 ) 
    [55] => Array ( [0] => (000) 123-4567 ) 
  ) 
  [1] => Array ( 
    [2] => Array ( [0] => XXXXX ) 
    [3] => Array ( [0] => 2017-06-02 ) 
    [4] => Array ( [0] => 2017-06-16 ) 
    [5] => Array ( [0] => Tested Value 2 ) 
    [6] => Array ( [0] => Tested Value 2 ) 
    [7] => Array ( [0] => Tested Value 2 ) 
    [8] => Array ( [0] => Tested Value 2 ) 
    [9] => Array ( [0] => ) 
    [10] => Array ( [0] => ) 
    [11] => Array ( [0] => ) 
    [12] => Array ( [0] => ) 
    [13] => Array ( [0] => ) 
    [14] => Array ( [0] => ) 
    [16] => Array ( [0] => Tested Value 2 ) 
    [17] => Array ( [0] => Tested Value 2 ) 
    [18] => Array ( [0] => Tested Value 2 ) 
    [19] => Array ( [0] => ) 
    [20] => Array ( [0] => Tested Value 2 ) 
    [21] => Array ( [0] => Tested Value 2 ) 
    [22] => Array ( [0] => Tested Value 2 ) 
    [23] => Array ( [0] => Tested Value 2 ) 
    [24] => Array ( [0] => test@gmail.com ) 
    [26] => Array ( [0] => Tested Value 2 ) 
    [27] => Array ( [0] => Tested Value 2 ) 
    [28] => Array ( [0] => ) 
    [29] => Array ( [0] => Tested Value 2 ) 
    [30] => Array ( [0] => ST ) 
    [31] => Array ( [0] => 47834 ) 
    [32] => Array ( [0] => SH ) 
    [33] => Array ( [0] => test@gmail.com ) 
    [35] => Array ( [0] => Tested Value 2 ) 
    [36] => Array ( [0] => Tested Value 2 ) 
    [37] => Array ( [0] => ) 
    [38] => Array ( [0] => Tested Value 2 ) 
    [39] => Array ( [0] => DJ ) 
    [40] => Array ( [0] => 0225 ) 
    [41] => Array ( [0] => IN ) 
    [42] => Array ( [0] => yes ) 
    [43] => Array ( [0] => ) 
    [44] => Array ( [0] => ) 
    [45] => Array ( [0] => ) 
    [46] => Array ( [0] => ) 
    [47] => Array ( [0] => ) 
    [48] => Array ( [0] => ) 
    [49] => Array ( [0] => ) 
    [50] => Array ( [0] => ) 
    [51] => Array ( [0] => ) 
    [52] => Array ( [0] => ) 
    [53] => Array ( [0] => ) 
    [54] => Array ( [0] => (000) 123-4567 ) 
    [55] => Array ( [0] => (000) 123-4567 ) 
  ) 
  [2] => Array ( 
    [2] => Array ( [0] => XXXXX ) 
    [3] => Array ( [0] => 2017-06-02 ) 
    [4] => Array ( [0] => 2017-06-16 ) 
    [5] => Array ( [0] => Tested Value 3 ) 
    [6] => Array ( [0] => Tested Value 3 ) 
    [7] => Array ( [0] => Tested Value 3 ) 
    [8] => Array ( [0] => Tested Value 3 ) 
    [9] => Array ( [0] => ) 
    [10] => Array ( [0] => ) 
    [11] => Array ( [0] => ) 
    [12] => Array ( [0] => ) 
    [13] => Array ( [0] => ) 
    [14] => Array ( [0] => ) 
    [16] => Array ( [0] => Tested Value 3 ) 
    [17] => Array ( [0] => Tested Value 3 ) 
    [18] => Array ( [0] => Tested Value 3 ) 
    [19] => Array ( [0] => ) 
    [20] => Array ( [0] => Tested Value 3 ) 
    [21] => Array ( [0] => Tested Value 3 ) 
    [22] => Array ( [0] => Tested Value 3 ) 
    [23] => Array ( [0] => Tested Value 3 ) 
    [24] => Array ( [0] => test@gmail.com ) 
    [26] => Array ( [0] => Tested Value 3 ) 
    [27] => Array ( [0] => Tested Value 3 ) 
    [28] => Array ( [0] => ) 
    [29] => Array ( [0] => Tested Value 3 ) 
    [30] => Array ( [0] => ST ) 
    [31] => Array ( [0] => 47834 ) 
    [32] => Array ( [0] => SH ) 
    [33] => Array ( [0] => test@gmail.com ) 
    [35] => Array ( [0] => Tested Value 3 ) 
    [36] => Array ( [0] => Tested Value 3 ) 
    [37] => Array ( [0] => ) 
    [38] => Array ( [0] => Tested Value 3 ) 
    [39] => Array ( [0] => DJ ) 
    [40] => Array ( [0] => 0225 ) 
    [41] => Array ( [0] => IN ) 
    [42] => Array ( [0] => yes ) 
    [43] => Array ( [0] => ) 
    [44] => Array ( [0] => ) 
    [45] => Array ( [0] => ) 
    [46] => Array ( [0] => ) 
    [47] => Array ( [0] => ) 
    [48] => Array ( [0] => ) 
    [49] => Array ( [0] => ) 
    [50] => Array ( [0] => ) 
    [51] => Array ( [0] => ) 
    [52] => Array ( [0] => ) 
    [53] => Array ( [0] => ) 
    [54] => Array ( [0] => (000) 123-4567 ) 
    [55] => Array ( [0] => (000) 123-4567 ) 
  ) 
)

问题根源分析

我看了你的代码,发现几个关键问题:

  1. 变量覆盖问题:$csv_export在循环里每次都会被重新赋值,最后只保留了最后一条提交数据的内容,前面的所有数据都丢失了。
  2. 文件操作逻辑混乱:你先是用fopen以追加模式打开文件,接着用file_put_contents直接覆盖写入,然后又用w模式打开文件再次覆盖,这就导致之前的内容完全被清空,最后只写入了最后一条数据的换行符。
  3. CSV格式处理不严谨:手动拼接逗号和换行,遇到内容里有逗号、引号的情况会直接破坏CSV格式,后续读取会出问题。

修正后的代码

我调整了代码逻辑,用更可靠的方式处理CSV生成和文件写入:

function webform_email_menu() { 
  $items = array(); 
  $items['webform_email'] = array( 
    'title' => 'Webform Email', 
    'description' => 'Webform Information', 
    'page callback' => 'webform_list', 
    'access callback' => true, 
  ); 
  return $items; 
} 

function webform_list() { 
  module_load_include('inc', 'webform', 'includes/webform.export'); 
  module_load_include('inc', 'webform', 'includes/webform.components'); 
  module_load_include('inc', 'webform', 'includes/webform.submissions'); 

  $submissions = webform_get_submissions(125); 
  // 定义CSV表头
  $header = array(
    'installation Type',
    'Date of Purchase',
    'Date of Install',
    'Outdoor Unit Model',
    'Outdoor Unit Serial',
    'Indoor Unit 1 Model',
    'Indoor Unit 1 Serial',
    'Indoor Unit 2 Model',
    'Indoor Unit 2 Serial',
    'Indoor Unit 3 Model',
    'Indoor Unit 3 Serial',
    'Indoor Unit 4 Model',
    'Indoor Unit 4 Serial',
    'Indoor Unit 5 Model',
    'Indoor Unit 5 Serial',
    'Indoor Unit 6 Model',
    'Indoor Unit 6 Serial',
    'Indoor Unit 7 Model',
    'Indoor Unit 7 Serial',
    'Indoor Unit 8 Model',
    'Indoor Unit 8 Serial',
    'Indoor Unit 9 Model',
    'Indoor Unit 9 Serial',
    'Owner First Name',
    'Owner Last Name',
    'Installation Address 1',
    'Installation Address 2',
    'City',
    '
相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:38:57