打开购物车 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; ?>