繁体   English   中英

如何使用CakePHP在数据库中存储要上传的文件的文件路径?

[英]How can i store the filepath of the file that i am uploading in my DB using CakePHP?

我正在使用cakephp表单助手将图像上传到我的服务器。 上传的文件名已存储在数据库中,但我想在服务器上存储完整的文件路径,因为我需要在我的json View(result)中显示它。 我试图用表单中的隐藏输入来完成此操作,但是那样行不通。 请任何帮助! 这是我的表格:

<div class="images form">
<?php // echo $this->Form->create('Image');?>
<?php echo $form->create('Image',array('type' => 'file')); ?>
<fieldset>
    <legend><?php __('Add Image'); ?></legend>
<?php
    echo $this->Form->input('gallery_id');
    echo $this->Form->input('name');
    //echo $this->Form->input('img_file');
    $form->input('img_file', array('type' => 'file'));
    echo $form->input('plaatsfoto', array('value'=>'http://localhost/tut_blog/img/uploads/images/','type' => 'hidden'));

?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
</div>
<div class="actions">
<h3><?php __('Actions'); ?></h3>
<ul>

    <li><?php echo $this->Html->link(__('List Images', true), array('action' => 'index'));?      ></li>
    <li><?php echo $this->Html->link(__('List Galleries', true), array('controller' => 'galleries', 'action' => 'index')); ?> </li>
    <li><?php echo $this->Html->link(__('New Gallery', true), array('controller' => 'galleries', 'action' => 'add')); ?> </li>
</ul>
</div>

您可以使用CakePHP的beforeSave()函数将路径附加到文件名中,然后再保存到数据库中。 这将允许您在将表单数据保存到数据库之前对其进行操作。 CakePHP beforeSave()

我还将检查这些其他资源,以了解在Cake中上载和存储文件的不同方法:

请记住,有更好的方法来动态检测上传文件的路径,而不是像这样放置静态路径。

暂无
暂无

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

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