为什么相同的PHP脚本可能在一个子域而不是另一个子域中工作? [英] Why identical PHP script might work in one subdomain and not another?

查看:155
本文介绍了为什么相同的PHP脚本可能在一个子域而不是另一个子域中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



所以,在一些子域的脚本是工作,在一些不是。 WordPress安装是相同的。 php脚本是相同的,我下载并从一个域上传到其他域以进行故障排除。它工作在一个而不是另一个。我尝试了相同的file.csv,仍然在一个子域而不是其他子域。



以下错误出现在错误日志


[2013年11月17日11:00:05] PHP致命错误:允许内存大小为134217728字节用尽(试图分配8388608字节)在/ filepath / _adder.php on line 16


但是,file.csv文件在两个安装中是相同的,并且大小相同。但它仍然可以在一个而不是其他。



为什么脚本可以正常工作在子子域而不是其他?



为了完整起见,下面是有问题的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);
}

//文件读取
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 ==($ 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);

//检查重复的
// $ 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>。clearers($ values [$ i] [$ k])。< / em> \r\\\
:'
}

// 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 {
//添加类别
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-fullsrc ='。$ siteurl。'/ images /'.$ im_name。'alt =/> ';
}
}

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

$ 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>地址:$。$ post ['Address']。< / em>
'。$ post ['topimage']。 'Body_text']。'
<! - more - >
'。$ post ['details']。'
'。$ post ['images']。
',
'post_status'=>'publish',
'post_author'=> 1,
'post_category'=> $ cats
);
unset($ cats);

// add post
// echoID:。
$ postid = wp_insert_post($ my_post); // post ID

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

echo $ post ['name']。 ' - 添加。 ';

// 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']),
数组('%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),
数组('%s','%s','%s' %s')
);
} else {
//无法插入数据
}

添加回显地址。< br />

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

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

$ input = explode(\\\
,file_get_contents(file.csv));
foreach($ input as $ line){
//处理所有行。
}

//此函数删除第一个$ CNT元素。
//更多信息:
// http://php.net/manual/en/function.array-slice.php
$ output = array_slice($ input,$ cnt);
file_put_contents(file.csv,implode(\\\
,$ output));

?>
< html>
< body>
< form enctype =multipart / form-datamethod =post>
CSV:< input name =filetype =file/>
< input type =submitvalue =发送文件/>
< / form>
< / body>
< / html>


解决方案

您的脚本可能使用了大量内存。我的客人原因是读取整个文件到数组:

  $ csv_lines = file(file.csv); 

检查每个服务器上的内存限制。

  var_dump(ini_get('memory_limit')); 

在脚本服务器上,此值应该高于服务器上的错误。



您可以尝试在导入文件之上切割此值

  ini_set('memory_limit','256M'); 

或在 .htaccess 您的服务器使用apache)

  php_value memory_limit 256M 

最佳解决方案是重构导入脚本以降低内存消耗。


I have a php script that creates WordPress posts from a csv file ("file.csv") that is on the same subdomain as WordPress. This has been working for months, however, I just uploaded a new "file.csv" file to a couple of subdomains and the script is not working, resulting in a blank screen and does not create posts.

To troubleshoot, I checked other subdomains where I have the php script set up and uploaded the new "file.csv" file. It worked there.

So, on some subdomains the script is working and on some it is not. The WordPress installs are identical. The php script is identical, I downloaded and uploaded it from one domain to the other to troubleshoot. It works on one and not the other. I have tried identical "file.csv", still works on one subdomain and not the other.

The below error comes up in the error logs

[17-Nov-2013 11:00:05] PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 8388608 bytes) in /filepath/_adder.php on line 16

However the "file.csv" file is identical in both installs, and is the same size. But it still works in one and not the other.

Why might the script be working correctly on done subdomain and not the other? Any suggestions, things to try or tips would be very much appreciated.

For sake of completeness, below is the php script in question.

<?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>

解决方案

Your script is probably using a lot of memory. My guest on cause whould be reading whole file to array with:

$csv_lines  = file("file.csv");

Check memory limit on each server.

var_dump(ini_get('memory_limit'));

On server that script works this value should be higher then on servers that you have your error.

You can try to incise this value on top of your import file

ini_set('memory_limit','256M');

Or in your .htaccess file (if your server using apache)

php_value memory_limit 256M

Best solution would be to refactor your import script to lower memory consumption.

这篇关于为什么相同的PHP脚本可能在一个子域而不是另一个子域中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆