Cakenoobie -将PHP文件的内容放入控制器文件


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

我有一个有action="process.php"的表单。在php文件中有一个函数,后面跟着一些调用函数的代码,如下所示:

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

我想在cakephp应用程序中使用这段代码,这需要我通过将其转换为控制器动作来cakeify事物。

我想我可以将函数分离成一个单独的动作,并创建另一个函数与其他代码调用其中的第一个函数,即:

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

,然后使表单的动作只是action="calluploadImage",但返回错误:

致命错误:调用未定义的函数uploadimage()C: ' xampp ' ' campaigns_controller.php根' cakephp ' app '控制器第102行

有人能帮我一下吗?:)

您正在调用没有$this->的uploadImage

代替

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

:

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

您应该使用$this

调用此函数这样的

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

注意,函数是在类中定义的,您是在类的另一个方法中调用它的。所以在本例中必须使用$this

我建议保留你的请求&业务逻辑分离。

如何在您的Images控制器中具有upload_image动作,然后将uploadImage函数移动到您的Image模型?像这样:

/**
 * 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;
  }
}

然后您将访问您的上传表单在/images/image_upload