使用CakePHP上载文件(PDF和图像)

使用CakePHP上载文件(PDF和图像),php,cakephp,Php,Cakephp,我想做的事我以前从未做过。我正在使用CakePHP构建一个表单,我正在使用表单助手来完成这项工作。这一切都适用于输入和文本区域,但我现在尝试将两个输入更改为文件上载字段 当我调试$This->data['Event']['img'](表单是callevent)时,我已经完成了这项工作,并且似乎完成了这项工作。我得到以下输出: Array ( [name] => logo.gif [type] => image/gif [tmp_name] => /tmp/p

我想做的事我以前从未做过。我正在使用CakePHP构建一个表单,我正在使用表单助手来完成这项工作。这一切都适用于输入和文本区域,但我现在尝试将两个输入更改为文件上载字段

当我调试$This->data['Event']['img'](表单是callevent)时,我已经完成了这项工作,并且似乎完成了这项工作。我得到以下输出:

Array
 (
   [name] => logo.gif
   [type] => image/gif
   [tmp_name] => /tmp/phpaU3aBa
   [error] => 0
   [size] => 7318
 )
现在我在这里留言,但我必须将文件保存到我的“webroot”文件夹中。我已经创建了路径/webroot/eventfiles/img&webroot/eventfiles/pdf-我现在只开始做图像,所以我可以让它工作,但我将上传的另一种文件类型是pdf

现在我看了CakePHP网站上的内容,但我只是说看看PHP网站,似乎有很多方法可以做到这一点,但我不知道如何保存它。以便我以后可以访问它,例如,它进入webroot文件夹,以便Cake可以看到它

另外,还需要将path+文件名保存到数据库中。除非我错了,否则这应该很简单,只要使用$This->data['Event']['img']['name']就可以将其保存到我的数据库中

请帮忙?我已经为此工作了好几天了。我似乎无法理解文件上传。有没有一个简单的方法可以做到这一点?例如,如何将蛋糕作为HTML和表单助手,一种保存上传文件的简单方法

谢谢你能给我的帮助


格伦。

不过我会发布我的答案,这完全有效。我从两个文件输入文件中获取pdf和jpeg图像,然后重命名为用户在输入文本字段中提供的给定标题。对于$newevent中的两个变量,这些变量与其他输入一起保存到数据库、路径和重命名的图像中

            $pathimg = "/eventfiles/img/";
            $pathpdf = "/eventfiles/pdf/";
            $dir_pdf = getcwd().$pathpdf;
            $dir_img = getcwd().$pathimg;
            $PDF_Path = $dir_pdf .  $this->data['Event']['pdf']["name"];
            $IMG_Path = $dir_img .  $this->data['Event']['img']["name"];
            $IMG_WithOutSpace = preg_replace('/\s+/', '_', $newevent['title']);
            $PDF_WithOutSpace = preg_replace('/\s+/', '_', $newevent['title']);

            $newevent['img'] = "/vh/eventfiles/img/" . $IMG_WithOutSpace . ".jpg";
            $newevent['pdf'] = "/vh/eventfiles/pdf/" . $PDF_WithOutSpace . ".pdf";

        if ((( $this->data['Event']['img']["type"] == "application/pdf") || ( $this->data['Event']['img']["type"] == "image/jpeg"))) {
          if (($this->data['Event']['img']["error"] > 0) || ($this->data['Event']['pdf']["error"] > 0)) {
            $this->Session->setFlash(__('An error has occured during updating. Please check all the fields.', true), 'default', array('class' => 'error-message'));
            $this->redirect($this->referer());
            exit();
          } else {

              move_uploaded_file( $this->data['Event']['img']["tmp_name"], $IMG_Path);
              rename($IMG_Path, $dir_img . $IMG_WithOutSpace. ".jpg");

              move_uploaded_file( $this->data['Event']['pdf']["tmp_name"], $PDF_Path);
              rename($PDF_Path, $dir_pdf . $PDF_WithOutSpace . ".pdf");

              $filename= $this->data['Event']['pdf']["name"];
              $filename= $this->data['Event']['img']["name"];
          } 
这段代码非常简单。这段代码的主体部分来自这个网站上的一篇文章,我刚刚将它改为供我使用

如果您还试图添加/上载不同的文件类型,例如doc或png,则只需知道要上载的文档的标题类型,并将其添加到If语句中即可

希望这能帮助别人

格伦

            $pathimg = "/eventfiles/img/";
            $pathpdf = "/eventfiles/pdf/";
            $dir_pdf = getcwd().$pathpdf;
            $dir_img = getcwd().$pathimg;
            $PDF_Path = $dir_pdf .  $this->data['Event']['pdf']["name"];
            $IMG_Path = $dir_img .  $this->data['Event']['img']["name"];
            $IMG_WithOutSpace = preg_replace('/\s+/', '_', $newevent['title']);
            $PDF_WithOutSpace = preg_replace('/\s+/', '_', $newevent['title']);

            $newevent['img'] = "/vh/eventfiles/img/" . $IMG_WithOutSpace . ".jpg";
            $newevent['pdf'] = "/vh/eventfiles/pdf/" . $PDF_WithOutSpace . ".pdf";

        if ((( $this->data['Event']['img']["type"] == "application/pdf") || ( $this->data['Event']['img']["type"] == "image/jpeg"))) {
          if (($this->data['Event']['img']["error"] > 0) || ($this->data['Event']['pdf']["error"] > 0)) {
            $this->Session->setFlash(__('An error has occured during updating. Please check all the fields.', true), 'default', array('class' => 'error-message'));
            $this->redirect($this->referer());
            exit();
          } else {

              move_uploaded_file( $this->data['Event']['img']["tmp_name"], $IMG_Path);
              rename($IMG_Path, $dir_img . $IMG_WithOutSpace. ".jpg");

              move_uploaded_file( $this->data['Event']['pdf']["tmp_name"], $PDF_Path);
              rename($PDF_Path, $dir_pdf . $PDF_WithOutSpace . ".pdf");

              $filename= $this->data['Event']['pdf']["name"];
              $filename= $this->data['Event']['img']["name"];
          } 

我知道很多人说你应该在发帖前搜索这个网站,我为自己辩护说,关于上传文件的帖子太多了,我试过的那些都没用!但这一次,只需稍加编辑!请注意,这是为了在CakePHP内部工作而构建的,因此如果没有Cake框架,可能会有一些函数在PHP内部无法工作。