Cakephp创建文件夹上传图片

时间:2016-05-05 03:22:46

标签: php image upload cakephp-2.0 directory

我想基于我的idEvenememt自动创建一个文件夹,并在此文件夹中上传许多图像 我的ImageevenementsController

    public function add($id) {

    if ($this->request->is('post')) {
        $this->Imageevenement->create();
      $this->request->data['Imageevenement']['idEvenement'] = $id;


      $data=$this->request->data;
       $dir = IMAGES.$id;
      if(!file_exists($dir))
        mkdir($dir,0777);

       $f = explode('.',$data['Imageevenement']['file']['name']);
    $ext = '.'.end($f);
    $filename = Inflector::slug(implode('.',array_slice($f,0,-1)),'-');
    //sauvgarder eb bdd
    $success = $this->Imageevenement->save(array(
    'name' => $data['Imageevenement']['name'],
    'url' => DS.$id.'/'.$filename.$ext
    ));
   if($success){

  move_uploaded_file($data['Imageevenement']['file']  ['tmp_name'],$dir.DS.$filename.$ext);

       }else{ 
   $this ->Session->setFlash("L'image n'est pas en bonne format");}     
    }
  if ($this->Imageevenement->save($this->request->data)) {
            $this->Flash->success(__('The image has been saved.'));
            return $this->redirect(array('action' => 'index' ,   'controller'=>'evenements'));
        } 
       }

在我的视图中

     <?php if (!empty($evenement['Imageevenement'])): ?>
              <table class="table table-bordered" >
                <tr><th> Nom</th>
                <th> Image</th>

                <th> Actions</th>
            </tr>
            <?php foreach ($evenement['Imageevenement']as $imageevenement): ?>
                <tr>

                    <td><?php echo $imageevenement['name']; ?></td>
                    <td><?php echo $imageevenement['url']; ?></td>

                    <td class="actions">
        <?php echo $this->Html->link(__('View'), array('action' => 'view', 'controller'=>'imageevenements', $imageevenement['idImageEvenement'])); ?>
        <?php echo $this->Html->link(__('Edit'), array('action' => 'edit', 'controller'=>'imageevenements',$imageevenement['idImageEvenement'])); ?>
        <?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete','controller'=>'imageevenements', $imageevenement['idImageEvenement']), array('confirm' => __('Are you sure you want to delete # %s?', $imageevenement['idImageEvenement']))); ?>
    </td>
                </tr>

创建文件夹的路径以\开头,如数据库中的\ 1 / paris.png,文件夹中创建的图像,但我无法显示它们。所以我怎么能删除这个\?为什么它在视图中没有显示任何东西?我该怎么做才能创建文件夹并在其中上传imanges并在视图中显示它们?

1 个答案:

答案 0 :(得分:0)

据我所知,请试试这个。我创建了单独的函数来创建文件夹,更改文件名,移动上传文件等。请检查是否有任何问题然后请告诉我。

public function _create_folder($path) {
    $folderPath = $path ;
    if (!file_exists($folderPath.DS)) {
        App::uses('Folder', 'Utility');
        $dir = new Folder($folderPath , true, 0777);
    }
    return $folderPath. DS;
}

public function moveUploadedFile($filename , $destination) {
    if (move_uploaded_file($filename, $destination)) {
        return true;
    }
    return false;
}

public function change_file_name($fileName= null) {

    $exFileName     = strtolower(substr($fileName,strrpos($fileName,".") + 1));
    $sampleFileName = str_replace('.'.$exFileName,'', $fileName);
    $name           = Sanitize::paranoid($sampleFileName,array('_'));
    $fileRename     = $name.'.'.$exFileName;
    return $fileRename;
}


function add(){

    $file                           =     $data['Imageevenement']['file'];
    $file_name                      =     $file['name'];
    $tmp_name                       =     $file['tmp_name'];
    $return_file_name               =     $this->change_file_name($file_name);
    $path                           =     $this->_create_folder(IMAGES.DS.$id.DS);
    $return_file_name               =     $id.'/'.$return_file_name;
    if($this->moveUploadedFile($tmp_name, IMAGES.DS.$id.DS.$return_file_name)){
        $saveData['resume']         =   $return_file_name;
    }

    $success = $this->Imageevenement->save(array(
        'name' => $return_file_name,
        'url' => '/'.$id.'/'.$return_file_name
    ));

}

用于查看view.ctp中的图像文件

$imageName = $imageevenement['url'];
echo $this->Html->image(IMAGES.$imageName);