[英]OpenCart adding form to admin page
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-我知道:PI DID说我正在实验)
<?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());
}
}
?>
模板:
<?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; ?>
在您的控制器中,您应该构建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标准不符。
使用其他文件中的现有代码作为新模块的基础。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.