Php OpenCart将表单添加到管理页面
opencart 1.5.6 尝试在管理员端添加一个页面,将图像上传到新文件夹(用于站点的其他部分) 我有一个问题,在图像是从表单上传页面重定向到管理员主页。我试图通过在表单响应中重新呈现当前页面来修复此问题,但现在我只加载了一个没有更新的页面版本,并且函数似乎没有运行: 模板文件:(hello.tpl)Php OpenCart将表单添加到管理页面,php,opencart,Php,Opencart,opencart 1.5.6 尝试在管理员端添加一个页面,将图像上传到新文件夹(用于站点的其他部分) 我有一个问题,在图像是从表单上传页面重定向到管理员主页。我试图通过在表单响应中重新呈现当前页面来修复此问题,但现在我只加载了一个没有更新的页面版本,并且函数似乎没有运行: 模板文件:(hello.tpl) 在控制器中,您应该构建OpenCart用于表单发布的所有标准方法,例如: 公共函数插入() 公共函数更新() 公共函数getForm() 等等 看看admin/controller/cata
在控制器中,您应该构建OpenCart用于表单发布的所有标准方法,例如:
公共函数插入()
公共函数更新()
公共函数getForm()
等等
看看admin/controller/catalog/attribute.php
您将看到一个名为insert()
的公共方法,它通过$request
对象捕获您的post数据,即:if($this->request->server['request\u method']=='post')&&$this->validateForm(){
只有在满足条件的情况下,才会执行其中的所有代码,然后该方法将您转发到$this->getForm()
,您的页面就是在这里呈现的
我不确定你从哪里得到了你开始使用的示例代码,但它不正确,或者至少不符合正常的OpenCart标准
使用不同文件中的现有代码来创建新模块。更新:好。因此,经过一些小改动后,我似乎已接近解决方案……我相信问题出现在提交表单时,我不完全确定另一端的函数是否收到表单的任何信息(学习)写一个OpenCart模块/扩展,请先学习PHP。现在是2013年,你的代码看起来像是1995年的(+5年,因为你使用了来自现有控制器的代码)…这并不是要冒犯你,而是要提醒你-OpenCart是按它的方式编写的-但是代码非常可读、干净和漂亮-你制作的代码让我想知道今天的人们从哪里学习PHP(可能的答案:从1995年之前编写的源代码/文章/书籍)。
<?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; ?>
<?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
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; ?>