copy()警告文件名不能为空

时间:2013-09-30 01:42:55

标签: php

我正在尝试在我的网站上保存外部图像

我有这段代码:

$file = $_GET['url'];
$newfile = $_SERVER['DOCUMENT_ROOT'] . '/images/name.jpg';

if ( copy($file, $newfile) ) {
    echo "Copy saved";
}else{
    echo "copy failed";
}

它运行良好并将图像保存在我的服务器上确定,但是当我尝试在FOR内使用时,我有这个警告:

Warning: copy(): Filename cannot be empty in /home/animesad/public_html/bot/getimg.php on line 18

我的代码是:

<?php
require "../functions.php";
$p = $_GET['p'];

$url = "http://www.mysite.net/en/movie/page/".$p."/";
$source = curl_get_contents("$url");

preg_match_all('#<li title="(.*?)"><a href#',$source,$name); //outputs title post
preg_match_all('#<img src="(.*?)" width="140" height="200"/>#',$source,$img); //outputs img url

for($i=0;$i<10;$i++){
//print_r($name[1][$i]."<hr />");  //that code show the names OK. (Title posts);

$namefile = slugify($name[1][$i]); //slugify is my function that convert titles in slug.

$newfile = $_SERVER['DOCUMENT_ROOT'] .'/img/'.$namefile.'.jpg';

    if ( copy($file, $newfile) ) {
        echo "copy saved <hr />";
    }else{
        echo "copy failed <hr />";
    }

}

?>

欢迎任何想法。

1 个答案:

答案 0 :(得分:1)

你忘了你的$ file

$file = $_GET['url'];

for($i=0;$i<10;$i++){

    $namefile = slugify($name[1][$i]); //slugify is my function that convert titles in slug.

    $newfile = $_SERVER['DOCUMENT_ROOT'] .'/img/'.$namefile.'.jpg';

    if ( copy($file, $newfile) ) {
        echo "copy saved <hr />";
    }else{
        echo "copy failed <hr />";
    }
}