copy() 只工作一次,然后错误无法打开流:没有这样的文件或目录


copy() only works once then error failed to open stream: No such file or directory

>我在foreach中循环访问了以下 URL 数组。

这些是我的网址:

$file_url = "http://photos2.automanager.com/017529/3cea2be0e7839b41b5bd24b9f7b56d85/71cc0100fc_640.jpg,http://photos2.automanager.com/017529/3cea2be0e7839b41b5bd24b9f7b56d85/7de4801490_640.jpg,http://photos2.automanager.com/017529/3cea2be0e7839b41b5bd24b9f7b56d85/e4a943787c_640.jpg,http://photos2.automanager.com/017529/3cea2be0e7839b41b5bd24b9f7b56d85/d7cf552f10_640.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
        $filename = array_pop(explode("/", $url));
        echo "Next filename: $filename 'n";
        if (!copy($url, ABSPATH.$artDir.$filename)) {
            $errors= error_get_last();
            echo "COPY ERROR: ".$errors['type'];
            echo "<br />'n".$errors['message']."'n";
        } 
        else {
            echo "File copied from remote! 'n";
        }
        $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);
}

我之前遇到了一些问题,认为我的foreach循环不起作用,直到我将其缩小到第一个文件后卡住的copy()

我把文件放在那里以查看输出,这些文件确实存在,但由于某种原因,它为我提供了第一张照片之后每张照片的[function.copy]: failed to open stream: No such file or directory

知道这里发生了什么吗? 顺便说一下,这些是您可以检查以验证文件是否存在的实时 URL。

我弄清楚发生了什么,以及为什么它在第一次运行循环后不起作用。

出于某种原因,此代码在 URL 之前添加一个空格并将其呈现为无效的 URL,这就是我收到无法打开流错误的原因。

我暂时做了一个str_replace来删除URL开头的任何空格,当然,这解决了问题,循环现在没有障碍。 现在找出为什么添加该空间。