Jump to content

Memory Hungry PHP script ..


dmuldoon

Recommended Posts

I have a PHP script, that should be simple ... it just reads some filenames from a CSV file and copies the associated files to a location,  but due to my lack of skill it is HUNGRY!  This sucker eats like 400M of RAM and then dies.

 

What am I doing wrong?  This seems like a simple thing ..

 

Here's the code:

 

<?php

 

ignore_user_abort(true);

 

/**

* Tell WordPress we are doing the CRON task.

*

* @var bool

*/

if (!defined('ABSPATH')) {

    /** Set up WordPress environment */

    require_once('./wp-load.php');

}

 

if (count($argv) < 2) die();

 

$filename = $argv[1];

 

$filenamearr = explode('.', $filename);

$extensionarr = array('csv', 'CSV');

 

if (in_array($filenamearr[count($filenamearr) - 1], $extensionarr)) {

    $destination_path = ABSPATH . "vielerets/data/";

 

    if (!file_exists($destination_path)) {

        mkdir($destination_path, 0777);

    }

 

    $target_path = $destination_path . $filename;

    $csv_target_path = $target_path;

   

$fd = fopen ($target_path, "rt");

 

if (!$fd) die();

if (!feof ($fd)) $buffer = fgetcsv($fd, 4096);

$rowcount = 0;

$last_postid = 0;

$pre = -1;

while (!feof ($fd)) {

            try{

$buffer = fgetcsv($fd, 4096);

if (count($buffer) < 4) continue;

$rowcount++;

 

if ($pre != $buffer[0]) {

$pre = $buffer[0];

//The Query

query_posts('meta_key=sysid&meta_value='.$buffer[0]);

 

//The Loop

if ( have_posts() ) {

the_post();

$last_postid = get_the_ID();

echo "update property images: $last_postid\n";

wp_reset_query();

}

else {

wp_reset_query();

continue;

}

}

 

$menu_order = 0;

$image_folder_name = 'property/';

 

if($buffer[3])

{

$image_name = $buffer[3];

$menu_order = $buffer[1];

$image_name_arr = explode('/',$image_name);

$img_name = $image_name_arr[count($image_name_arr)-1];

$img_name_arr = explode('.',$img_name);

$post_img = array();

$post_img['post_title'] = $img_name_arr[0];

$post_img['post_status'] = 'attachment';

$post_img['post_parent'] = $last_postid;

$post_img['post_type'] = 'attachment';

$post_img['post_mime_type'] = 'image/jpeg';

$post_img['menu_order'] = $menu_order;

$last_postimage_id = wp_insert_post( $post_img );

update_post_meta($last_postimage_id, '_wp_attached_file', $image_folder_name.$image_name);

$post_attach_arr = array(

"width" => 580,

"height" => 480,

"hwstring_small"=> "height='150' width='150'",

"file" => $image_folder_name.$image_name,

);

wp_update_attachment_metadata( $last_postimage_id, $post_attach_arr );

                        unset($post_img);

                        unset($post_attach_arr);

echo " add images: $img_name\n";

}

                unset($buffer);

            } catch (Exception $ex) {

                echo "$rowcount\n";

            }

}

echo "image csv uploaded successfully\n";

echo "Total of $rowcount records inserted\n";

}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/219137-memory-hungry-php-script/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.