簡體   English   中英

通過php一次性將圖像從URl列表復制到我的服務器

[英]Copying Images from URl list to my server all at once by php

我在html文件中有大量的Urls列表,用於這樣的圖像:

<a href="http://example.com/image1.jpg">image1</a>
<a href="http://example.com/image2.jpg">image2</a>
<a href="http://example.com/image3.jpg">image3</a>
<a href="http://example.com/image4.jpg">image4</a>
<a href="http://example.com/image5.jpg">image5</a>
<a href="http://example.com/image6.jpg">image6</a>
<a href="http://example.com/image7.jpg">image7</a>

大約50,000張圖片

我想制作一個小腳本,可以將所有圖像復制到我的服務器,所以我可以將它們放入:

http://Mywebsite.com/images/image1.jpg
http://Mywebsite.com/images/image1.jpg
http://Mywebsite.com/images/image1.jpg
...

我想制作循環,並且必須在成功復制圖像后刪除列表中的每個Url,因為有時如果頁面壓縮加載或者我可以繼續循環而不會覆蓋或再次讀取,如果有更好的解決方案不覆蓋和再次閱讀網址請告訴我。

我會創建一個腳本,每行讀取你的html文件行。
你可以使用fopenfgets來做到這一點。

fopen("path/to/some/file", "r");
while ( ( $line = fgets( $handle ) ) !== false ) 
{
    // do somehting with $line
}

這樣,文件不會簡單地解析到內存中,因此您不必擔心大小

然后解析每一行后,我會寫下一個包含當前行號/索引的鎖文件。 因此,如果您的腳本崩潰並重新啟動它,迭代只會跳過每一行,直到它的當前索引高於鎖定文件的索引。

劇本

它可能會工作,但最終不應該簡單地復制粘貼一切。 但我希望它可以幫助您找到解決方案。

#!/usr/bin/env php
<?php
// I DID NOT TEST THIS! 
// but it should work.

$handle = fopen("path/to/the/html/file/containing/the/urls.html", "r");
$storage = "path/where/you/want/your/images/";
$lockFile = __DIR__.'/index.lock';
$index = 0;

// get the lock index
if ( !file_exists( $lockFile ) )
{
    file_put_contents( $lockFile, 0 );
}

// load the current index
$start = file_get_contents( $lockFile );

if ( $handle ) 
{
    // line by line step by step
    while ( ( $line = fgets( $handle ) ) !== false ) 
    {
        // update the 
        $index++;

        if ( $start > $index )
        {
            continue;
        }

        // match the url from the element
        preg_match( '/<a href="(.+)">/', $line, $url ); $url = $url[1];

        $file = basename( $url );

        // check if the file already exists 

        if ( !file_exists( $storage.$file )) //edited 
        {
            file_put_contents( $storage.$file, file_get_contents( $url ) );
        }

        // update the lock file
        file_put_contents( $lockFile, $index );
    }

    fclose($handle);
} 
else 
{
    throw new Exception( 'Could not open file.' );
} 

你可以做這樣的事情,當然你也應該在這里添加一些錯誤檢查:)

define("SITE_DIR", '/home/www/temp');

$file = file('in.txt');

foreach ($file AS $row){
    preg_match('/(?<=\")(.*?)(?=\")/', $row, $url);

    $path = parse_url($url[0], PHP_URL_PATH);
    $dirname = pathinfo($path, PATHINFO_DIRNAME);

    if (!is_dir(SITE_DIR . $dirname)){
        mkdir(SITE_DIR . $dirname, 0777, true);
    }

    file_put_contents(SITE_DIR. $path, file_get_contents($url[0]));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM