2016-01-20 176 views
0

所以我有一個問題,我的foreach循環只運行一次。foreach循環運行一次

我有$ file_url變量的以下數據。

$file_url = "http://www.somedomain.com/12355/1.jpg,http://www.somedomain.com/12355/2.jpg,http://www.somedomain.com/12355/3.jpg,http://www.somedomain.com/12355/4.jpg"; 

現在我的代碼如下所示:

function fetch_media($file_url,$vin,$cacheid) { 
    require_once(ABSPATH . 'wp-load.php'); 
    require_once(ABSPATH . 'wp-admin/includes/image.php'); 
    global $wpdb; 

    if(!$vin) { 
     $vin = $cacheid; 
    } 

    $vin = $vin . '/'; 

    //directory to import to  
    $artDir = "wp-content/uploads/vehiclephotos/$vin"; 

    //if the directory doesn't exist, create it 
    if(!file_exists(ABSPATH.$artDir)) { 
     mkdir(ABSPATH.$artDir); 
    } 

    $file_url = explode(",", $file_url); 
    $gallery_images = array(); 

    foreach ($file_url as $url) { 

    //rename the file... alternatively, you could explode on "/" and keep the original file name 
    $filename = array_pop(explode("/", $url)); 

     if (@fclose(@fopen($url, "r"))) { //make sure the file actually exists 
      copy($url, ABSPATH.$artDir.$filename); 

      $siteurl = get_option('siteurl'); 
      $file_info = getimagesize(ABSPATH.$artDir.$filename); 

      //create an array of attachment data to insert into wp_posts table 
      $artdata = array(); 
      $artdata = array(
       'post_author' => 1, 
       'post_date' => current_time('mysql'), 
       'post_date_gmt' => current_time('mysql'), 
       'post_title' => $filename, 
       'post_status' => 'inherit', 
       'comment_status' => 'closed', 
       'ping_status' => 'closed', 
       'post_name' => sanitize_title_with_dashes(str_replace("_", "-", $filename)), 
       'post_modified' => current_time('mysql'), 
       'post_modified_gmt' => current_time('mysql'), 
       'post_type' => 'attachment', 
       'guid' => $siteurl.'/'.$artDir.$filename, 
       'post_mime_type' => $file_info['mime'], 
       'post_excerpt' => '', 
       'post_content' => '' 
      ); 

      $uploads = wp_upload_dir(); 
      $save_path = $uploads['basedir'].'/vehiclephotos/'.$vin.$filename; 

      //insert the database record 
      $attach_id = wp_insert_attachment($artdata, $save_path); 

      //generate metadata and thumbnails 
      if ($attach_data = wp_generate_attachment_metadata($attach_id, $save_path)) { 
       wp_update_attachment_metadata($attach_id, $attach_data); 
      } 

      array_push($gallery_images,$attach_id); 
      } 

    } 

    return serialize($gallery_images); 
} 

所以我得到的輸出是序列化數組,它是:

a:1:{i:0;i:103525;} 

這很好,但因爲有4個數組項,它應該循環4次,並給我4個attachment_id的序列化數據。其中的所有其他代碼運行良好一次,它從URL下載圖像,重命名它並創建照片的所有縮略圖大小。

任何想法我做錯了什麼?

+0

'$ fire_url!= $ file_url' – wogsland

+0

哈哈,這是寫錯了,這應該是讀取file_url。 – capsloc

+0

在foreach之前使用'print_r($ file_url)'並檢查Array是否被正確提取 –

回答

0

在進行故障排除時,請刪除@-operator以查看所得到的錯誤。所以:

if (@fclose(@fopen($url, "r"))) { //make sure the file actually exists 

變爲:

if (fclose(fopen($url, "r"))) { //make sure the file actually exists 

你也許可以簡化這一行:

if (file_exists($url)) { 
+0

這種幫助移動。看起來它在這部分卡住了。我把它拿出來是因爲我意識到我的URL是實時URL,所以在我甚至到達這個部分之前,這些文件都在那裏是100%確定的。調試仍在繼續,因爲它仍然滯留在第一張照片的導入中。 – capsloc

0

該代碼是好的。因此很可能你的輸入必須是錯誤的。與其他人一樣,您發佈的代碼並不建議您記錄count()。我猜想$file_url數組有四項的陳述實際上是不真實的。

0
foreach ($file_url as $url) { 
try { 

    //rename the file... alternatively, you could explode on "/" and keep the original file name 
    $filename = array_pop(explode("/", $url)); 

     if (@fclose(@fopen($url, "r"))) { //make sure the file actually exists 
      copy($url, ABSPATH.$artDir.$filename); 

      $siteurl = get_option('siteurl'); 
      $file_info = getimagesize(ABSPATH.$artDir.$filename); 

      //create an array of attachment data to insert into wp_posts table 
      $artdata = array(); 
      $artdata = array(
       'post_author' => 1, 
       'post_date' => current_time('mysql'), 
       'post_date_gmt' => current_time('mysql'), 
       'post_title' => $filename, 
       'post_status' => 'inherit', 
       'comment_status' => 'closed', 
       'ping_status' => 'closed', 
       'post_name' => sanitize_title_with_dashes(str_replace("_", "-", $filename)), 
       'post_modified' => current_time('mysql'), 
       'post_modified_gmt' => current_time('mysql'), 
       'post_type' => 'attachment', 
       'guid' => $siteurl.'/'.$artDir.$filename, 
       'post_mime_type' => $file_info['mime'], 
       'post_excerpt' => '', 
       'post_content' => '' 
      ); 

      $uploads = wp_upload_dir(); 
      $save_path = $uploads['basedir'].'/vehiclephotos/'.$vin.$filename; 

      //insert the database record 
      $attach_id = wp_insert_attachment($artdata, $save_path); 

      //generate metadata and thumbnails 
      if ($attach_data = wp_generate_attachment_metadata($attach_id, $save_path)) { 
       wp_update_attachment_metadata($attach_id, $attach_data); 
      } 

      array_push($gallery_images,$attach_id); 
      } 

    } 

    } catch (Exception $e) { 
    echo 'Caught exception: ', $e->getMessage(), "\n"; 
} 

使用try和catch在foreach循環將繼續運行,以及你可以看到錯誤什麼錯在foreach循環

+0

嗯試過,但在保存代碼後出現錯誤500。我添加了一個缺少的},但仍然沒有解決它。 – capsloc

+0

我能夠讓你的代碼運行,但它仍然沒有輸出任何異常。 – capsloc

0

貌似問題是不與循環畢竟像有人提及。在第一張圖像後,我收到錯誤的文件複製到我的服務器......這完全是一個完全不同的問題。

謝謝你的指導,導致我找到了錯誤。