crazy sarah
crazy sarah

Reputation: 631

noobie - putting the contents of a php file into a controller file

I have a form which has action="process.php". Inside that php file there is a function followed by some code which calls the function, like this:

function uploadImage($fileName, $maxSize, $maxW, $fullPath) {
    // .... does stuff
}

$filename = strip_tags($_REQUEST['filename']);
$maxSize = strip_tags($_REQUEST['maxSize']);
$maxW = strip_tags($_REQUEST['maxW']);
$fullPath = strip_tags($_REQUEST['fullPath']);

if($filesize_image > 0){
    $upload_image = uploadImage($filename, $maxSize, $maxW, $fullPath);
}

I want to use this code in a cakephp app which requires me to cakeify things by turning it into controller actions.

I thought I'd be able to sperate the function into a seperate action and create another function with the other code that calls the first function in it, ie:

function uploadImage($fileName, $maxSize, $maxW, $fullPath) {
    //.... does stuff
}

function calluploadImage() {
    $filename = strip_tags($_REQUEST['filename']);
    $maxSize = strip_tags($_REQUEST['maxSize']);
    $maxW = strip_tags($_REQUEST['maxW']);
    $fullPath = strip_tags($_REQUEST['fullPath']);
    if($filesize_image > 0){
    $upload_image = uploadImage($filename, $maxSize, $maxW, $fullPath);
    }
}

and then make the form's action just action="calluploadImage" but that returns the error:

Fatal error: Call to undefined function uploadimage() in C:\xampp\htdocs\cakephp\app\controllers\campaigns_controller.php on line 102

Can someone help me out? :)

Upvotes: 1

Views: 117

Answers (3)

Moz Morris
Moz Morris

Reputation: 6761

I would suggest keeping your request & business logic separate.

How about having an upload_image action in your Images controller and then moving your uploadImage function to your Image model? Something like this:

/**
 * Images Controller
 *
 */
class ImagesController extends AppController {

  public function upload_image() {

    /**
     * Handle request
     */
    $filename = strip_tags($_REQUEST['filename']);
    $maxSize = strip_tags($_REQUEST['maxSize']);
    $maxW = strip_tags($_REQUEST['maxW']);
    $fullPath = strip_tags($_REQUEST['fullPath']);

    /**
     * Only upload if image exists?
     */
    if ($filesize_image > 0) {
      // call uploadImage in Image model
      $upload_image = $this->Image->uploadImage($filename, $maxSize, $maxW, $fullPath);
    }

  }
}

/**
 * Image Model
 *
 */
class Image extends AppModel {

  function uploadImage($fileName, $maxSize, $maxW, $fullPath) {
      // .... does stuff
      return $result;
  }
}

You would then access your upload form at /images/image_upoad

Upvotes: 0

gustavotkg
gustavotkg

Reputation: 4399

You are calling uploadImage withour $this->

replace

$upload_image = uploadImage($filename, $maxSize, $maxW, $fullPath);

to:

$upload_image = $this->uploadImage($filename, $maxSize, $maxW, $fullPath);

Upvotes: 1

Shakti Singh
Shakti Singh

Reputation: 86386

You should call this function using $this

like this

$this->uploadImage($filename, $maxSize, $maxW, $fullPath);

Note that the function is defined in the class and you are calling it in the another method of the class. So you have to use $this in this case.

Upvotes: 0

Related Questions