为什么这个PHP脚本在一个子域中工作,而不是在另一个子域中工作?

为什么这个PHP脚本在一个子域中工作,而不是在另一个子域中工作?,php,wordpress,csv,Php,Wordpress,Csv,我有一个php脚本,可以从与WordPress位于同一子域上的csv文件(“file.csv”)创建WordPress帖子。这已经工作了几个月,但是,我刚刚上传了一个新的“file.csv”文件到几个子域,脚本不工作,导致一个空白屏幕,并且不创建帖子 为了排除故障,我检查了其他设置了php脚本的子域,并上传了新的“file.csv”文件。它在那里奏效了 因此,在某些子域上,脚本正在工作,而在某些子域上则不工作。WordPress的安装是相同的。php脚本是相同的,我从一个域下载并上传到另一个域进

我有一个php脚本,可以从与WordPress位于同一子域上的csv文件(“file.csv”)创建WordPress帖子。这已经工作了几个月,但是,我刚刚上传了一个新的“file.csv”文件到几个子域,脚本不工作,导致一个空白屏幕,并且不创建帖子

为了排除故障,我检查了其他设置了php脚本的子域,并上传了新的“file.csv”文件。它在那里奏效了

因此,在某些子域上,脚本正在工作,而在某些子域上则不工作。WordPress的安装是相同的。php脚本是相同的,我从一个域下载并上传到另一个域进行故障排除。它对其中一个有效,而对另一个无效。我尝试过相同的“file.csv”,但仍然在一个子域上工作,而在另一个子域上不工作

以下错误出现在错误日志中

[17-Nov-2013 11:00:05]PHP致命错误:第16行的/filepath/_adder.PHP中允许的内存大小为134217728字节(尝试分配8388608字节)

但是,“file.csv”文件在两次安装中是相同的,并且大小相同。但它在其中一种情况下仍然有效,而在另一种情况下则无效

为什么脚本可以在done子域而不是其他子域上正常工作?如有任何建议、尝试事项或提示,将不胜感激

为了完整起见,下面是讨论中的php脚本

<?php
require_once('wp-config.php');

$siteurl = get_site_url();


function clearer($str) {
  //$str = iconv("UTF-8", "UTF-8//IGNORE", $str);
  $str = utf8_encode($str);
  $str = str_replace("’", "'", $str);
  $str = str_replace("–", "-", $str);
  return htmlspecialchars($str);
}

//file read
if(file_exists("file.csv")) $csv_lines  = file("file.csv");
if(is_array($csv_lines)) {

  $cnt = 15;
  for($i = 0; $i < $cnt; $i++) {
    $line = $csv_lines[$i];
    $line = trim($line);
    $first_char = true;
    $col_num = 0;
    $length = strlen($line);
    for($b = 0; $b < $length; $b++) {
      if($skip_char != true) {
        $process = true;
        if($first_char == true) {
          if($line[$b] == '"') {
            $terminator = '",';
            $process = false;
          }else
            $terminator = ',';
          $first_char = false;
        }

        if($line[$b] == '"'){
          $next_char = $line[$b + 1];
          if($next_char == '"')
            $skip_char = true;
          elseif($next_char == ',') {
            if($terminator == '",') {
              $first_char = true;
              $process = false;
              $skip_char = true;
            }
          }
        }

        if($process == true){
          if($line[$b] == ',') {
             if($terminator == ',') {
                $first_char = true;
                $process = false;
             }
          }
        }

        if($process == true)
          $column .= $line[$b];

        if($b == ($length - 1)) {
          $first_char = true;
        }

        if($first_char == true) {
          $values[$i][$col_num] = $column;
          $column = '';
          $col_num++;
        }
      }
      else
        $skip_char = false;
    }
  }

  $values = array_values($values);
  //print_r($values);

  /*************************************************/

  if(is_array($values)) {
    //file.csv read
    for($i = 0; $i < count($values); $i++) {
      unset($post);

      //check duplicate
      //$wpdb->show_errors();
      $wpdb->query("SELECT `ID` FROM `" . $wpdb->prefix . "posts`
                            WHERE `post_title` = '".clearer($values[$i][0])."' AND `post_status` = 'publish'");
        //echo $wpdb->num_rows;

      if($values[$i][0] != "Name" && $values[$i][0] != "" && $wpdb->num_rows == 0) {
        $post['name'] = clearer($values[$i][0]);
        $post['Address'] = clearer($values[$i][1]);
        $post['City'] = clearer($values[$i][2]);
        $post['Categories'] = $values[$i][3];
        $post['Tags'] = $values[$i][4];
        $post['Top_image'] = $values[$i][5];
        $post['Body_text'] = clearer($values[$i][6]);

        //details
        for($k = 7; $k <= 56; $k++) {
          $values[$i][$k] != '' ? $post['details'] .= "<em>".clearer($values[$i][$k])."</em>\r\n" : '';
        }

        //cats
        $categoryes = explode(";", $post['Categories']);
        foreach($categoryes AS $category_name) {
          $term = term_exists($category_name, 'category');
          if (is_array($term)) {
            //category exist
            $cats[] = $term['term_id'];
          }else{
            //add category
            wp_insert_term( $category_name, 'category' );
            $term = term_exists($category_name, 'category');
            $cats[] = $term['term_id'];
          }
        }

        //top image
        if($post['Top_image'] != "") {
          $im_name = md5($post['Top_image']).'.jpg';

          $im = @imagecreatefromjpeg($post['Top_image']); 
          if ($im) {
            imagejpeg($im, ABSPATH.'images/'.$im_name);
            $post['topimage'] = '<img class="alignnone size-full" src="'.$siteurl.'/images/'.$im_name.'" alt="" />';
          }
        }

        //bottom images
        for($k = 57; $k <= 76; $k++) {
          if($values[$i][$k] != '') {
            $im_name = md5($values[$i][$k]).'.jpg';

            $im = @imagecreatefromjpeg($values[$i][$k]);
            if ($im) {
              imagejpeg($im, ABSPATH.'images/'.$im_name);
              $post['images'] .= '<a href="'.$siteurl.'/images/'.$im_name.'"><img class="alignnone size-full" 

src="'.$siteurl.'/images/'.$im_name.'" alt="" /></a>';
            }
          }
        }

        $post = array_map( 'stripslashes_deep', $post );

        //print_r($post);

        //post created
        $my_post = array (
           'post_title' => $post['name'],
           'post_content' => '
              <em>Address: '.$post['Address'].'</em>
              '.$post['topimage'].'
              '.$post['Body_text'].'
              <!--more-->
              '.$post['details'].'
              '.$post['images'].'
           ',
           'post_status' => 'publish',
           'post_author' => 1,
           'post_category' => $cats
        );
        unset($cats);

        //add post
        //echo "ID:" .
        $postid = wp_insert_post($my_post); //post ID

        //tags
        wp_set_post_tags( $postid, str_replace(';',',',$post['Tags']), true ); //tags

        echo $post['name']. ' - added. ';

        //google coords
        $address = preg_replace("!\((.*?)\)!si", " ", $post['Address']).', '.$post['City'];
        $json = json_decode(file_get_contents('http://hicon.by/temp/googlegeo.php?address='.urlencode($address)));
        //print_r($json);

        if($json->status == "OK") {
          //нашло адрес
          $google['status'] = $json->status;

          $params = $json->results[0]->address_components;
          if(is_array($params)) {
            foreach($params AS $id => $p) {
              if($p->types[0] == 'locality') $google['locality_name'] = $p->short_name;
              if($p->types[0] == 'administrative_area_level_2') $google['sub_admin_code'] = $p->short_name;
              if($p->types[0] == 'administrative_area_level_1') $google['admin_code'] = $p->short_name;
              if($p->types[0] == 'country') $google['country_code'] = $p->short_name;
              if($p->types[0] == 'postal_code') $google['postal_code'] = $p->short_name;
            }
          }
          $google['address'] = $json->results[0]->formatted_address;
          $google['location']['lat'] = $json->results[0]->geometry->location->lat;
          $google['location']['lng'] = $json->results[0]->geometry->location->lng;

          //print_r($params);

          //print_r($google);

          //insert into DB
          $insert_code = $wpdb->insert( $wpdb->prefix . 'geo_mashup_locations',
                                        array( 'lat' => $google['location']['lat'], 'lng' => 

$google['location']['lng'], 'address' => $google['address'],
                                               'saved_name' => $post['name'], 'postal_code' => $google['postal_code'],
                                               'country_code' => $google['country_code'], 'admin_code' => 

$google['admin_code'],
                                               'sub_admin_code' => $google['sub_admin_code'], 'locality_name' => 

$google['locality_name'] ),
                                        array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )
                                      );
          if($insert_code) {
            $google_code_id = $wpdb->insert_id;
            $geo_date = date( 'Y-m-d H:i:s' );
            $wpdb->insert(
              $wpdb->prefix . 'geo_mashup_location_relationships',
              array( 'object_name' => 'post', 'object_id' => $postid, 'location_id' => $google_code_id, 'geo_date' => 

$geo_date ),
              array( '%s', '%s', '%s', '%s' )
            );
          }else{
            //can't insert data
          }

          echo ' address added.<br />';

        }else{
          //echo $json->status;
        }

      }
    } //$values end (for)
  }
}else{
  //not found file.csv
  echo 'not found file.csv';
}

$input = explode("\n", file_get_contents("file.csv"));
foreach ($input as $line) {
 // process all lines.
}

// This function removes first $CNT elements.
// More info:
// http://php.net/manual/en/function.array-slice.php
$output = array_slice($input, $cnt);
file_put_contents("file.csv", implode("\n", $output));

?>
<html>
<body>
<form enctype="multipart/form-data" method="post">
 CSV: <input name="file" type="file" />
 <input type="submit" value="Send File" />
</form>
</body>
</html>

您导入的文件大于服务器上允许的限制。您正在导入大量数据并将其存储在导致崩溃的内存中。有两种方法可以停止崩溃

1.您可以在编写任何其他东西之前,将代码放在顶部,以便更大程度地访问内存

ini_set('memory_limit','256M');
2.你逐行阅读文件,并据此进行处理

我的建议是走第二条路,有两个理由

  • 如果文件的大小增加到无法保持文件内存的程度,您会怎么做。要么你必须拆分它,要么选择第二个选项,那么为什么不从第二个选项开始,而不是在以后的阶段使用

  • 第二个原因是内存分配和执行速度。如果您采用第二个选项,那么内存占用将更少,并且您的程序执行速度将快于读取内存中的完整文件,然后再对其进行操作

  • 您的“内存不足”错误似乎是相当确凿的。此.csv文件是否比以前的文件大?当脚本运行时,您是否检查了这些子域上的
    内存\u限制
    s?您错误地解释了整个“子域”问题,您开会是为了告诉是否所有这些子域都托管在同一台服务器上,以及如何设置php。但正如@w3d所解释的,这是一个内存限制问题。您需要在所有主机上具有相同的设置,代码才能正常工作。