简体   繁体   中英

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

I am, using a cakephp form helper to upload images to my server. The uploaded file-name gets stored in the DB but i want to store the full file-path on the server because i need this to show in my json View(result). I tried to accomplish this with a hidden input in my form but that didn't work so im stuck. Please any help! This is my form:

<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>

You could use CakePHP's beforeSave() function to append the path to the filename before it is saved to the database. This would allow you to manipulate the form data before it is saved to the database. CakePHP beforeSave()

I would also check out these other resources on different methods of uploading and storing files in Cake:

Keep in mind that there are better ways to dynamically detect an uploaded file's path rather than putting in a static path like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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