WordPress cron do not upload images

I let the task CRON product imports. The data are retrieved from the database in a loop. Each item has its own image, and in the database is only information which the link is located.
I have this code. When you release the script manually add products properly. While connecting to the cron script is added to the product description and the script stops running. Folder uploads in WP, there is nothing thrown (in the case of manual firing script to a folder are uploaded image from the link).

My code to upload and load pictures. (When I delete this part of the code cron works and products are imported correctly)

Read More
$thumb_url = $foto1;
$tmp = download_url( $thumb_url );

preg_match('/[^?]+.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $thumb_url, $matches);
$file_array['name'] = basename($matches[0]);
$file_array['tmp_name'] = $tmp;

$thumbid = media_handle_sideload( $file_array, $new_post_id, 'gallery desc' );

set_post_thumbnail($new_post_id, $thumbid);

$thumb_url2 = $foto2;

$tmp2 = download_url( $thumb_url2 );

preg_match('/[^?]+.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $thumb_url2, $matches);

$file_array2['name'] = basename($matches[0]);
$file_array2['tmp_name'] = $tmp2;
$imgID = media_handle_sideload( $file_array2, $new_post_id, 'desc' );

update_post_meta( $new_post_id, '_product_image_gallery', $imgID);

I spent two days. The problem with access rights to the record? Wrong code? What could be causing this?

Related posts