0

hi guys i'm facing problem with file upload and download in kohana

my controller is like this:


class Controller_Test extends Controller
{
     public function action_display()
     {
        $type = $_FILES['file']['type'];

          switch ($type)
          {
          case 'image/gif':
             $otype='.gif';   break;
         case 'image/jpeg':
         case 'image/pjpeg':
            $otype= '.jpg';   break;
         case 'image/png':
            $otype= '.png';   break;
          case 'application/octet-stream':
              $otype='.doc';   break;
          case 'txt': $otype='.txt'; break;
          case 'application/pdf': $otype='.pdf'; break;
           }
     //rename the file
     $name = time() . '_' . mt_rand(1000,9999).$otype;
     $directory = $_SERVER['DOCUMENT_ROOT'].URL::base().'media';

      //uploading a file 
     $filename = Upload::save($_FILES['file'],  $name, $directory);

           $this->auto_render = false;
           $this->response->send_file($filename);

    }//action
}//controller

when i call this function file uploaded fine

but downloading file as a corrupted file

help me how to solve this..

thanks in advance.

Ratna
  • 51
  • 7

2 Answers2

0

You shouldn't add URL::base() inside the path name as that could add something like "http://..." inside the file path. Try removing URL::base() and try again.

laurent
  • 88,262
  • 77
  • 290
  • 428
0

To start, there's some simple debug checks you can do here.

I'm going to assume $directory is invalid.

You want to use the absolute path constants to build directory paths. Instead of using $_SERVER['DOCUMENT_ROOT'].URL::base() (which is wrong in any case)

Rather use APPPATH or DOCROOT, eg $directory = APPPATH.'media'; see https://github.com/kohana/kohana/blob/3.2/master/index.php#L57-74

badsyntax
  • 9,394
  • 3
  • 49
  • 67
  • @Badsystax `$directory` is valid and file uploaded successfully problem is when i'm downloading same file it will download corrupted file – Ratna Jun 08 '12 at 11:37
  • @Ratna how can `$directory` be valid when you are using `URL::base()`? – badsyntax Jun 08 '12 at 11:42
  • @Ratna are you absolutely certain there are no errors before sending the file in the response? How about trying to uncomment that line to see if any errors are generated.. – badsyntax Jun 08 '12 at 11:45
  • @Ratna and are you certain there are no errors before sending the file to the browser? Is there any whitespace at the top or bottom of the controller file? – badsyntax Jun 08 '12 at 12:29
  • @Badsystax : actually i created directory(folder) in my project(ratnaKohana) with name `'media'` here ` $_SERVER['DOCUMENT_ROOT'] ` gives `E:/xampp/htdocs' and `'URL::base()' gives /ratnaKohana/ '` `$directory gives "E:/xampp/htdocs/ratnaKohana/media" ` – Ratna Jun 08 '12 at 12:29
  • there are no errors before sending the file and there is no white spaces in the controller – Ratna Jun 08 '12 at 12:37
  • **could you please suggest one good book for kohana ?? or send any link** – Ratna Jun 08 '12 at 12:42
  • when i trying to download file in kohana using $this->response->send_file($path); method i'm getting this Error `Resource interpreted as Document but transferred with MIME type image/jpeg:` – Ratna Jun 09 '12 at 06:12