dongmi5177 2016-01-20 05:24
浏览 56

foreach循环运行一次

So I'm having an issue with my foreach loop only running once.

I have the following data for the $file_url variable.

$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";

Now my code looks like this:

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);
}

So the output I'm getting is the serialized array which is:

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

Which is fine, but since there were 4 array items, it should have looped 4 times and given me the serialized data with 4 attachment_id's. All the other code in there runs fine once, it downloads the image from the URL, it renames it and creates all thumbnail sizes of the photo fine.

Any idea what I'm doing wrong?

  • 写回答

4条回答 默认 最新

  • dri98076 2016-01-20 05:56
    关注

    While troubleshooting, remove the @-operator to see what errors you get. So:

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

    becomes:

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

    You could probably simplify this line to:

    if (file_exists($url)) {
    
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致