我正在構建基於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