gpt4 book ai didi

php - 如何仅存储从其他网站下载的 zip 文件

转载 作者:行者123 更新时间:2023-11-28 02:04:02 25 4
gpt4 key购买 nike

我正在构建基于 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 文件并将其上传到我的本地驱动器。

最佳答案

由于文件名只是一个字符串,您可以使用/修改此 SO question 的答案:

$rex = "/^.*\.(zip)$/i";
preg_match($rex, $file)

编辑:

对于错误代码尝试:

$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($httpCode == 404){ //do some error handling }

关于php - 如何仅存储从其他网站下载的 zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12537433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com