将图像从URL复制到服务器,之后删除所有图像


Copy images from url to server, delete all images after

我有一个带有产品图片的附属链接

http://www.myaffiliate.com/products/product.jpg

我想将想象复制到我的网站上,在一个名为"让我们说产品"的文件夹中。它托管在共享服务器上,而不是我的电脑上。 我认为 php copy() 不起作用,试过了,我认为我的主机不支持该方法。 我该怎么做,也许是卷曲? 然后,我需要一个 php 命令来删除产品文件夹中的所有图像 - 我将使用 cronjob 执行此操作。

<?php
$table = 'cron';
$feed = 'myfeed';
$xml = simplexml_load_file($feed);
mysql_query("TRUNCATE TABLE ".$table."", $dbh1);
mysql_query("TRUNCATE TABLE ".$table."", $dbh2);

function getimg($url) {         
        $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';              
        $headers[] = 'Connection: Keep-Alive';         
        $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';         
        $user_agent = 'php';         
        $process = curl_init($url);         
        curl_setopt($process, CURLOPT_HTTPHEADER, $headers);         
        curl_setopt($process, CURLOPT_HEADER, 0);         
        curl_setopt($process, CURLOPT_USERAGENT, $useragent);         
        curl_setopt($process, CURLOPT_TIMEOUT, 30);         
        curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);         
        curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);         
        $return = curl_exec($process);         
        curl_close($process);         
        return $return;     
        } 
foreach( $xml->performerinfo as $performerinfo )
{
    $performerid = $performerinfo->performerid;
    $category = $performerinfo->category;
    $subcategory = $performerinfo->subcategory;
    $bio = $performerinfo->bio;
    $turnons = $performerinfo->turnons;
    $willing = $performerinfo->willingness;
    $willingness = str_replace(',', ', ', $willing); 
    $age = $performerinfo->age;
    $build = $performerinfo->build;
    $height = $performerinfo->height;
    $weight = $performerinfo->weight;
    $breastsize = $performerinfo->breastsize;
    $haircolor = $performerinfo->haircolor;
    $hairlength = $performerinfo->hairlength;
    $eyecolor = $performerinfo->eyecolor;
    $ethnicity = $performerinfo->ethnicity;
    $sexpref = $performerinfo->sexpref;
    $pic0 = $performerinfo->picture[0];
    $pic1 = $performerinfo->picture[1];
    $pic2 = $performerinfo->picture[2];
    $pic3 = $performerinfo->picture[3];
    $pic4 = $performerinfo->picture[4];   
    $test = $performerinfo->picture[0];  
    $imgurl = 'http://www.foodtest.ru/images/big_img/sausage_3.jpg'; 
    $imagename= basename($imgurl);
    if(file_exists('./tmp/'.$imagename)){continue;} 
    $image = getimg($imgurl); 
    file_put_contents('tmp/'.$imagename,$image); 
}  
   //baracuda reloaded
   mysql_query("TRUNCATE TABLE reloaded", $dbh1);
   mysql_query("TRUNCATE TABLE reloaded", $dbh2);
   mysql_query("INSERT INTO reloaded SELECT * FROM ".$table."", $dbh1); 
   mysql_query("INSERT INTO reloaded SELECT * FROM ".$table."", $dbh2); 
?>

循环中使用file_get_contents或cURL

    <?php 
    //loop with a list of images
    loop{
    $imgurl = 'url to image';
    $imagename= basename($imgurl);
    if(file_exists('./images_folder/'.$imagename)){continue;}
    $image = file_get_contents($imgurl);
    file_put_contents('images_folder/'.$imagename,$image);
    }
    ?>
    or cURL 
    <?php 
    //loop this
loop{
    $imgurl = 'url to image';
    $imagename= basename($imgurl);
    if(file_exists('./images_folder/'.$imagename)){continue;}
    $image = getimg($imgurl);
    file_put_contents('images_folder/'.$imagename,$image);
}
    ?>


   <?php
    //cURL function (outside of loop) to get img
        function getimg($url) {
            $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';
            $headers[] = 'Connection: Keep-Alive';
            $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
            $user_agent = 'php';
            $process = curl_init($url);
            curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($process, CURLOPT_HEADER, 0);
            curl_setopt($process, CURLOPT_USERAGENT, $useragent);
            curl_setopt($process, CURLOPT_TIMEOUT, 30);
            curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
            $return = curl_exec($process);
            curl_close($process);
            return $return;
        }
    ?>

在服务器上要删除的文件中,将其放入其中,并在获得所有图像后在循环结束时调用它

<?php 
function rrmdir($dir) {
    if (is_dir($dir)) {
        $objects = scandir($dir);
        foreach ($objects as $object) {
            if ($object != "." && $object != "..") {
                if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
            }
        }
        reset($objects);
        rmdir($dir);
        mkdir($dir);
    }
}
rrmrir('./products');
?>