2016-01-20 177 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

貌似问题是不与循环毕竟像有人提及。在第一张图像后,我收到错误的文件复制到我的服务器......这完全是一个完全不同的问题。

谢谢你的指导,导致我找到了错误。