dmuldoon Posted November 18, 2010 Share Posted November 18, 2010 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"; } ?> Quote Link to comment Share on other sites More sharing options...
papaface Posted November 18, 2010 Share Posted November 18, 2010 How big are the files it needs to read? Quote Link to comment Share on other sites More sharing options...
btherl Posted November 19, 2010 Share Posted November 19, 2010 memory_get_usage() might help you narrow down where it's getting used. Quote Link to comment Share on other sites More sharing options...
dmuldoon Posted November 19, 2010 Author Share Posted November 19, 2010 They vary in size .. the smaller ones actually make it all the way through .. but usually it craps out after processing between 5000 and 6000 lines. The larger files are around 270K, and have about 70,000 lines. Quote Link to comment Share on other sites More sharing options...
dmuldoon Posted November 23, 2010 Author Share Posted November 23, 2010 Any clues? Anyone? Quote Link to comment Share on other sites More sharing options...
btherl Posted November 24, 2010 Share Posted November 24, 2010 Try adding calls to memory_get_usage() throughout the script, and calculate the difference in usage before and after anything you think might be using memory. That can help you find where it's being used. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.