FAQ
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+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

Search Discussions

  • Harsha M V at Jun 6, 2012 at 2:15 pm
    Why dont you use an Existing plugin instead of writing all this ?

    --
    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+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
  • Steve Found at Jun 6, 2012 at 3:08 pm

    On 06/06/12 13:19, Ditshwanelo wrote:
    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();
    }
    }



    }
    Try:
    https://github.com/webtechnick/CakePHP-FileUpload-Plugin/tree/cakephp2.0

    No idea how good this is, but he has branches for 1.x and 2.0 there

    --
    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+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcake-php @
categoriesphp
postedJun 6, '12 at 12:36p
activeJun 6, '12 at 3:08p
posts3
users3
websitecakephp.org
irc#cakephp

People

Translate

site design / logo © 2022 Grokbase