Hello guys im new here =,would you help me with cake php upload.this
is my code and is not uploading...please help me


class NewslettersController extends AppController
{
  var $name='Newsletters';
  var $uses=array("Newsletter");
  var $helpers = array('Form', 'Session', 'Html');

  //allowed upload files
  var $validateFile = array(
                          'size' => 204800,
                          'type' =>
'jpg,jpeg,png,gif','txt','pdf','doc','docx'
                          );
  function index()
  {

  }

  function generateUniqueFilename($fileName, $path='')
  {
    $path = empty($path) ? WWW_ROOT.'/files/' : $path;
    $no = 1;
    $newFileName = $fileName;
        // print_r($path);
        //echo $path;
    while (file_exists("$path/".$newFileName))
        {
      $no++;
      $newFileName = substr_replace($fileName, "_$no.", strrpos
      ($fileName, "."), 1);

    }
    return $newFileName;
  }

//handles file upload
function handleFileUpload($fileData, $fileName)
  {
    $error = false;
    //Get file type
    $typeArr = explode('/', $fileData['type']);
    //If size is provided for validation check with that size. Else
compare the size with INI file
    if (($this->validateFile['size'] && $fileData['size'] > $this-
>validateFile['size']) || $fileData['error'] == UPLOAD_ERR_INI_SIZE)
    {
      $error = 'File is too large to upload';
    }
    elseif ($this->validateFile['type'] && (strpos($this-
>validateFile['type'], strtolower($typeArr[1])) == false))
    {
      //File type is not the one we are going to accept. Error!!
      $error = 'Invalid file type';
    }
    else
    {
      //Data looks OK at this stage. Let's proceed.
      if ($fileData['error'] == UPLOAD_ERR_OK)
      {
        //Oops!! File size is zero. Error!
        if ($fileData['size'] == 0)
        {
          $error = 'Zero size file found.';
        }
        else
        {
          if (is_uploaded_file($fileData['tmp_name']))
          {
                    //Finally we can upload file now. Let's do it and return 
without
errors if success in moving.
            //if(file_exists(WWW_ROOT.'files'.DS.$fileName))
                    if(file_exists(WWW_ROOT.'files'.$fileName))
                        {
                           
$url=file_get_contents(WWW_ROOT.'files'.DS.$filename);
                           echo $url;
               $query = $this->Newsletter->query("INSERT INTO
newsletters (news_name,news_content)
                                  VALUES('{$filename}','{$url}')");

            if (!move_uploaded_file($fileData['tmp_name'], WWW_ROOT.'/
files/'.$fileName))

            {
              $error = true;
            }

                   }
          else
          {
            $error = true;
          }

        }
      }
    }
    return $error;
  }
  }
  function deleteMovedFile($fileName)
  {
    if (!$fileName || !is_file($fileName))
    {
      return true;
    }
    if(unlink($fileName))
    {
      return true;
    }
    return false;
  }

    function add()
    {
           $news_content=$this->data['Newsletter']['news_content'];
          if (empty($this->data))
          {
            $this->Document->create();
          }
          else
          {
            $err = false;
            if (!empty($this->data['Newsletter']['news_content']))
                        {
                          $fileName = $this->generateUniqueFilename($this-
>data['Newsletter']['news_content']);
              //$fileName = $this->generateUniqueFilename($this-
>data['Attachment']['filename']['name']);
              echo 'The filename is:'. $fileName;
              $error = $this->handleFileUpload($this-
>data['Newsletter']['news_content'], $fileName);
              echo 'The error is: '. $error;
           }
                  else
                  {
            print_r($this->data);
          }
            if (!$error)
            {
              $this->data['Newsletter']['news_content'] = $fileName;
              if ($this->Newsletter->save($this->data))
              {
                $this->Session->setFlash(__('The Attachment has been
saved',true));
                                print_r($news_content);
                $this->redirect(array('action'=>'admins'=>'index'));
              }
                          else
                          {
                $err = true;
              }
            }
                        else
                        {
              $this->Newsletter->set($this->data);
            }


            if ($error || $err)
            {
              //Something failed. Remove the image uploaded if any.
              $this->deleteMovedFile(WWW_ROOT.FILE_URL.$fileName);
              $this->set('error', $error);
              $this->set('data', $this->data);
              $this->validateErrors($this->Newsletter);
              $this->render();
            }
          }



}

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to