我正在构建基于codeigniter的应用程序。在这里,我只需要下载具有.zip扩展名并上传到本地驱动器的文件。但要做到这一点,我已给定一个名为get_zip内容功能如下:如何仅存储从其他网站下载的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文件将被下载并上传到我的本地驱动器。
你可以得到的扩展名在文件名末尾,并检查它是否是.zip或不是。你有没有尝试过任何东西? – scrappedcola
其实我没怎么检查 – user1684080
对于这个我建议使用[DirectoryIterator](http://php.net/manual/en/class.directoryiterator.php)或PHP 5.3> = [FilesystemIterator](http ://php.net/manual/en/class.filesystemiterator.php) – dbf