-1

我想要完成的是下载两个地方之一的文件。如果文件不存在,则需要尝试从其他位置下载。但是,如果第一个文件确实存在,则需要停止并且不要尝试下载第二个文件,否则第二个文件仍将作为空白文件下载,覆盖第一个文件。我将如何使用诸如 else 值之类的东西来做到这一点?

<?php

$source_file = 'http://somesitehere.tld/' . $_GET["u"] . '.png'; 
$dest_file = '/enter/directory/here/' . $_GET["u"] . '.png';

copy($source_file, $dest_file);

?>
4

1 回答 1

0

您是否尝试过使用类似的东西,根据您的需要使用file_existsfile_get_contentsfopen函数。

如果您使用的是内部文件,请尝试使用file_exists()

if( file_exists( $source_file ) ) { 

copy( $source_file, $dest_file );

} else {

copy( $source_file2, $dest_file2 );

}

如果您使用的是外部文件,请尝试使用file_get_contents()

if( file_get_contents( $source_file ) ) { 

copy( $source_file, $dest_file );

} else {

copy( $source_file2, $dest_file2 );

}

或者,您可以尝试使用fopen()

if( fopen( $source_file ) ) { 

copy( $source_file, $dest_file );

} else {

copy( $source_file2, $dest_file2 );

}
于 2013-04-14T00:34:44.523 回答