2

I am building a application based in codeigniter. Here I need to download only files that have .zip extension and upload in my local drive. But to do it I had been given a function named get_zip contents are as follows:

<?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;
}

But the problem is how I can restrict that only .zip files will be downloaded and uploaded to my local drive.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user1684080
  • 73
  • 1
  • 8
  • You could get the extension off the end of the filename and check to see if it is .zip or not... Have you tried anything? – scrappedcola Sep 21 '12 at 20:04
  • Actually i didn't how to check it – user1684080 Sep 21 '12 at 20:05
  • For this I would recommend using [DirectoryIterator](http://php.net/manual/en/class.directoryiterator.php) or for PHP 5.3 >= [FilesystemIterator](http://php.net/manual/en/class.filesystemiterator.php) – dbf Sep 21 '12 at 20:09

1 Answers1

1

Since the filename is just a string you could use/modify the answer from this SO question:

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

Edit:

For error codes try:

$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 if($httpCode == 404){ //do some error handling }
Community
  • 1
  • 1
scrappedcola
  • 10,423
  • 1
  • 32
  • 43
  • Thanks but how can i make sure its a file i mean if the address of the site is wrong then it displays a error page and that file is being uploaded For example return http://stackoverflow.com/test.zip the files does not belong it just uploading the page not found file – user1684080 Sep 21 '12 at 20:31
  • You can check the status of your curl call and see what the status of the call is. http://php.net/manual/en/book.curl.php. In the comments check out the comment by frank at interactinet dot com 11-Mar-2011 01:29. – scrappedcola Sep 21 '12 at 20:50