2012-09-21 69 views
2

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

+0

你可以得到的扩展名在文件名末尾,并检查它是否是.zip或不是。你有没有尝试过任何东西? – scrappedcola

+0

其实我没怎么检查 – user1684080

+0

对于这个我建议使用[DirectoryIterator](http://php.net/manual/en/class.directoryiterator.php)或PHP 5.3> = [FilesystemIterator](http ://php.net/manual/en/class.filesystemiterator.php) – dbf

回答

1

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

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

编辑:

对于错误代码尝试:

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

谢谢,但我怎么能确保它的文件我的意思是,如果该网站的地址是错误的,那么它显示一个错误页面,该文件正在上传例如,返回http://stackoverflow.com/test.zip这些文件不属于它只是上传页面未找到文件 – user1684080

+0

您可以检查curl调用的状态,并查看调用的状态。 http://php.net/manual/en/book.curl.php。在评论中查看评论弗兰克在交流.com 2011年3月11日01:29。 – scrappedcola

相关问题