简体   繁体   English

在自定义组件后端上传Joomla 3.0 MVC文件

[英]Joomla 3.0 MVC file upload in custom component backend

SOLUTION: How to Save Uploaded File's Name on Database 解决方案: 如何在数据库上保存上载的文件名

this ended up helping me. 这最终帮助了我。

i am trying to add a file upload to a custom component using XML and database. 我正在尝试使用XML和数据库将文件上载添加到自定义组件。

I know how to get file upload done in a static PHP environment but my knowledge about the PHP MVC structure in joomla makes it so I am unable to add it. 我知道如何在静态PHP环境中完成文件上传,但是我对joomla中的PHP MVC结构的了解使得我无法添加它。

What I have done so far: 到目前为止我做了什么:

• Added the field in the XML file (of the type file) •在XML文件中添加了字段(类型文件)
• Added the form fields in admin view project •在管理视图项目中添加了表单域
• Added an extra field My_project table(same as the image upload column) •添加了一个额外的字段My_project表(与图像上传列相同)

Until this point it works.(fields are shown in admin backend component) 在此之前它可以工作。(字段显示在admin后端组件中)

Now when you save the document with a file uploaded in the admin back end it does not save it to the database. 现在,当您使用在管理后端上载的文件保存文档时,它不会将其保存到数据库中。

if i put media as field type then it works, but when i change it to file it breaks down. 如果我把媒体作为字段类型然后它工作,但当我将其更改为文件时,它会崩溃。

XML file XML文件

<?xml version="1.0" encoding="utf-8"?>
<form>
    <fieldset>

  <field name="project_file" type="file"

            label="Upload file"
            description="Upload file"  
            directory="mysites" /> 


       <field name="main_image" type="media"

            label="COM_MYSITES_FORM_LBL_PROJECT_MAIN_IMAGE"
            description="COM_MYSITES_FORM_DESC_PROJECT_MAIN_IMAGE" 
            filter="raw"
            directory="mysites" /> 

 </fieldset>

PHP file upload script i normaly use PHP文件上传脚本我正常使用

<?php
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);

if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

but what part goes in the model and what part goes in the controller? 但是模型中的哪个部分以及控制器中的哪个部分? and how to call it. 以及如何调用它。

entire view is called in the controller 在控制器中调用整个视图

class MysitesControllerProject extends JControllerForm {

    function __construct() {
        $this->view_list = 'projects';
        $jinput = JFactory::getApplication()->input;
        $files = $jinput->files->get('jform');
        $file = $files['project_file']; 
        $this->upload($file);
        parent::__construct();
    }

    public function upload($files)
    {
        $file_name = $files['name'];
        $src = $files['tmp_name'];
        $size = $files['size'];
        $upload_error = $files['error'];
        $type = $files['type'];
        $dest = "/home/vol3/byethost33.com/b33_13467508/bim-portfolio.cu.cc/htdocs/tmp";

        if (isset( $file_name)) {
            // Move the uploaded file.
            JFile::upload( $src, $filepath );
        }
    }

}

Placing new field in database and XML form is only half of work. 在数据库和XML表单中放置新字段只是工作的一半。 You also have to write file save/upload functionality. 您还必须编写文件保存/上载功能。 There are two places you can do it. 你可以在两个地方做到这一点。 In controller (for example save task procedure) or model (there are 2-3 functions where you can do it). 在控制器(例如保存任务程序)或模型中(有2-3个功能可以执行)。 Look into this file /administrator/components/com_media/controllers/upload.php (upload procedure). 查看此文件/administrator/components/com_media/controllers/upload.php (上传过程)。 I would just extend your save function so before data will be saved into database file will be stored on file system. 我只是扩展你的保存功能,所以在将数据保存到数据库文件之前将存储在文件系统中。 You can find original save function declaration in /libraries/legacy/controller/legacy.php (for Joomla 3.0.1, for other versions it shouldn't be hard to find) 您可以在/libraries/legacy/controller/legacy.php找到原始的保存函数声明(对于Joomla 3.0.1,对于其他版本,它应该不难找到)

Here is sample save function: 这是样本保存功能:

public function save($key = null, $urlVar = null){
    // youre file upload code

    return parent::save($key = null, $urlVar = null)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM