繁体   English   中英

foreach循环运行一次

[英]foreach Loop Running Once

所以我的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个attach_id的序列化数据。 那里的所有其他代码都可以正常运行一次,它可以从URL下载图像,将其重命名并创建照片的所有缩略图尺寸。

知道我在做什么错吗?

在进行故障排除时,请删除@-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)) {

该代码很好。 因此,您的输入很有可能一定是错误的。 正如其他人所知,您发布的代码并不表示您正在记录count() 我猜想$file_url数组有四个项目的声明实际上是不正确的。

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 and catch in foreach循环,它将继续运行,并且您可以看到错误,在foreach循环中出了什么问题

看起来问题并非与循环有关,就像有人提到的那样。 在第一个映像之后,将文件复制到服务器上时出现错误……这是一个完全不同的问题。

感谢您的指导,使我找到了问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM