OpenCart将表单添加到管理页面

时间:2013-09-19 14:17:08

标签: php opencart

opencart 1.5.6

尝试向管理员添加页面以将图像上传到新文件夹(用于网站的其他部分)

我遇到的问题是,从图像上传图像后,页面会重定向到管理主页。我试图通过在表单响应中重新呈现当前页面来解决这个问题,但现在我只是加载了一个没有更新的页面版本而且该函数似乎没有运行:

模板文件:(hello.tpl)

<?php echo $header; ?>
<div id="content">
  <div class="breadcrumb">
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
    <?php echo $breadcrumb['separator']; ?><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a>
    <?php } ?>
  </div>

<form action="index.php?route=common/helloworld&token=<?php echo $token; ?>"     method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

<?php
if ($error > 0)
{
echo $error;
}
elseif ($error1 > 0)
{
echo $error;
}
elseif ($error2 > 0)
{
echo $error;
}
elseif ($up > 0)
{
echo $up;
echo $type;
echo $size;
echo $store;
echo $store2;
}
else
{
echo "Upload File";
}

?>
</div> 
<?php echo $footer; ?>

控制器文件(hellowworld.php - 我知道:P我说我正在试验)

<?php
class ControllerCommonHelloworld extends Controller { 
    public function index(){
                // VARS

        $this->language->load('common/hello');

                $template="common/hello.tpl"; // .tpl location and file
        $this->load->model('common/hello');
        $this->template = ''.$template.'';
        $this->children = array(
            'common/header',
            'common/footer'
        );      

        $this->data['token'] = $this->session->data['token'];

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('text_home'),
            'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('heading_title'),
            'href'      => $this->url->link('common/helloworld', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => ' :: '
        );  

        $this->data['error'] = 0;
        $this->data['error1'] = 0;
        $this->data['error2'] = 0;
        $this->data['up'] = 0;

        $this->response->setOutput($this->render());
    }

    public function image ($file) {
    $allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
    if ($_FILES["file"]["error"] > 0)
  {
  $this->data['error'] = "Error: " . $_FILES["file"]["error"] . "<br>";
      }
else
  {
  $this->data['up'] = "Upload: " . $_FILES["file"]["name"] . "<br>";
  $this->data['type'] = "Type: " . $_FILES["file"]["type"] . "<br>";
  $this->data['size'] = "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
  $this->data['store'] = "Stored in: " . $_FILES["file"]["tmp_name"];

  if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      $this->data['error1'] = $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "../upload/" . $_FILES["file"]["name"]);
      $this->data['store2'] = "Moved To: " . "../upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  $this->data['error2'] = "Invalid file";
  }


    $template="common/hello.tpl"; // .tpl location and file
        $this->load->model('common/hello');
        $this->template = ''.$template.'';
        $this->children = array(
            'common/header',
            'common/footer'
        );      

        $this->data['token'] = $this->session->data['token'];

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('text_home'),
            'href'      => $this->url->link('common/home', 'token=' . $this- >session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('heading_title'),
            'href'      => $this->url->link('common/helloworld', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => ' :: '
        );
    $this->response->setOutput($this->render());
}
}
?>

正如我所说,我是新手并尝试使用此软件,之前从未使用过PHP MVC。 任何指针都会受到欢迎!

解决:

控制器:

<?php
class ControllerCommonHelloworld extends Controller { 
    public function index(){
                     .................

    public function image() {

        $this->language->load('common/hello');
        $template="common/hello.tpl"; // .tpl location and file
        $this->load->model('common/hello');
        $this->template = ''.$template.'';
        $this->children = array(
            'common/header',
            'common/footer'
        );      

        $this->data['token'] = $this->session->data['token'];

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('text_home'),
            'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('heading_title'),
            'href'      => $this->url->link('common/helloworld', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => ' :: '
        );

$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 200000)
&& in_array($extension, $allowedExts))
  {
    if ($_FILES["file"]["error"] > 0)
  {
  $this->data['error'] = "Error: " . $_FILES["file"]["error"] . "<br>";
  }
else
  {
  $this->data['up'] = "Upload: " . $_FILES["file"]["name"] . "<br>";
  $this->data['type'] = "Type: " . $_FILES["file"]["type"] . "<br>";
  $this->data['size'] = "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
  $this->data['store'] = "Stored in: " . $_FILES["file"]["tmp_name"];

  if (file_exists("/image/TEST/" . $_FILES["file"]["name"]))
      {
      $this->data['error1'] = $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "/image/TEST/" . $_FILES["file"]["name"]);
      $this->data['store2'] = "Moved To: " . "image/TEST/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  $this->data['error2'] = "Invalid file";
  }


    $this->response->setOutput($this->render());
 }
}
?>

TEMPLATE:

<?php echo $header; ?>
<div id="content">
  <div class="breadcrumb">
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
    <?php echo $breadcrumb['separator']; ?><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a>
    <?php } ?>
  </div>

<form action="index.php?route=common/helloworld/image&token=<?php echo $token; ?>" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

<?php
if (isset($error))
{
echo $error;
}

if (isset($error1))
{
echo $error1;
}

if (isset($error2))
{
echo $error2;
}

if (isset($up))
{
echo $up;
echo $type;
echo $size;
echo $store;
echo $store2;
}

if (!isset($error) and !isset($error1) and !isset($error2) and !isset($up))
{
echo "Upload File";
}

?>
</div> 
<?php echo $footer; ?>

1 个答案:

答案 0 :(得分:0)

在您的控制器中,您应该构建OpenCart用于表单帖子的所有标准方法,例如:

public function insert()

public function update()

public function getForm()

查看admin/controller/catalog/attribute.php

你会看到有一个名为insert()的公共方法,它通过$request对象捕获你的帖子数据,即:if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {只有满足条件才能执行里面的所有代码,然后该方法会将您转到$this->getForm(),这是您呈现页面的位置。

我不确定你从哪里开始使用的示例代码,但它不正确,或者至少不符合正常的OpenCart标准。

使用其他文件中的现有代码来启用新模块。