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

I have the following array of URLs being looped through in a foreach.

These are my URLs:

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

All separated by commas between photos.

Here is my code:

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

I was having some issues earlier thinking my foreach loop wasn’t working until I narrowed it down to being the copy() that got stuck after the first file.

I put the in there to see the output and those files do exist, but for some reason it’s giving me a [function.copy]: failed to open stream: No such file or directory for every photo after the first one.

Any idea what is going on here? By the way, those are live URLs that you can check to verify that the file exists.

Related posts

1 comment

  1. I figured out what was happening and why it wasn’t working past the first run through the loop.

    For some reason this code adds a space before the URL and rendered it as a non valid URL so that’s why I was getting the failed to open stream error.

    I did a str_replace for the moment to remove any spaces that come at the beginning of the URL and surely, that fixed the issue and the loop run without a hitch now. Now to find out why that space is getting added.

Comments are closed.