【问题标题】:Copy images from url to server, delete all images after将图片从url复制到服务器,之后删除所有图片
【发布时间】:2011-08-25 12:36:31
【问题描述】:

我有一个带有产品图片的会员链接

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

我想将想象复制到我的网站,在一个名为 let say products 的文件夹中。它托管在共享的 sherver 上,而不是我的电脑上。我认为 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); 
?>

【问题讨论】:

  • 您想在下载正确的图像后删除它们吗?如果是这样,请调用 unlink($filename);在每个图像上。将其与下面的“Lawrence Cherone”脚本结合使用。
  • 您使用相同的图像 url,因此每次迭代都会调用相同的图像,如果提要中有很多或条目,那么无论如何都需要一段时间,在 sql 查询上使用更多逻辑将如果您检查是否已添加条目等,请加快速度。

标签: php curl


【解决方案1】:

在循环中使用 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');
?>

【讨论】:

  • 不要将 getimg() 函数放入循环中
  • 致命错误:无法在 /web/sites/129/website.com 中重新声明 getimg()(之前在 /web/sites/129/website.com/cron/cron.php:49 中声明) /cron/cron.php 第 49 行
  • Ty,现在劳伦斯我遇到了和我使用的其他方法一样的问题,页面正在加载并一直加载
  • 你有一个无限循环,用完整的代码编辑你的问题
  • 循环在没有代码的情况下工作。我正在做的是获取我的产品的图像,我想将它们保存在我的服务器上,使用它们,并在下一次 cronjob 更新时删除它们......
猜你喜欢
  • 2019-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-23
  • 2014-07-07
  • 2015-05-11
  • 2013-01-19
  • 2019-06-01
相关资源
最近更新 更多