我正在构建一个基于codeigniter的应用程序。在这里,我只需要下载扩展名为.zip的文件并在我的本地驱动器中上传。但为了做到这一点,我得到了一个名为get_zip的函数,内容如下:
<?php
function get_file($file, $localpath, $newfilename)
{
$err_msg = '';
$out = fopen($localpath.$newfilename,"wb");
if ($out == FALSE){
print "File not opened<br>";
exit;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $file);
curl_exec($ch);
if( curl_error($ch) )
{
echo "<br>Error is : ".curl_error ( $ch);
}
curl_close($ch);
//fclose($ch);
return $localpath.$newfilename;
}//end function
function directory_map_echo($source_dir, $directory_depth = 0, $hidden = FALSE)
{
if ($fp = @opendir($source_dir))
{
$filedata = '';
$new_depth = $directory_depth - 1;
$source_dir = rtrim($source_dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
while (FALSE !== ($file = readdir($fp)))
{
// Remove '.', '..', and hidden files [optional]
if ( ! trim($file, '.') OR ($hidden == FALSE && $file[0] == '.'))
{
continue;
}
if (($directory_depth < 1 OR $new_depth > 0) && @is_dir($source_dir.$file))
{
$filedata .= 'directory:'.$file.directory_map($source_dir.$file.DIRECTORY_SEPARATOR, $new_depth, $hidden);
}
else
{
$filedata .= $file;
}
}
closedir($fp);
return $filedata;
}
return FALSE;
}
但问题是如何限制只会下载.zip文件并上传到我的本地驱动器。
答案 0 :(得分:1)
由于文件名只是一个字符串,您可以使用/修改此SO question的答案:
$rex = "/^.*\.(zip)$/i";
preg_match($rex, $file)
编辑:
对于错误代码,请尝试:
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($httpCode == 404){ //do some error handling }