【问题标题】:file upload cakephp文件上传 cakephp
【发布时间】:2011-01-14 20:43:15
【问题描述】:

当使用 cakephp 上传图片到服务器时

$this->Model->Behaviors->attach('ImageUpload', Configure::read('photo.files'));

照片上传成功,数据库字段也上传

但显示以下错误而不是返回索引页面。


注意(8):未定义索引:类 [CORE\cake\libs\model\behaviors\upload.php,第 104 行]

注意(8):未定义索引:类 [CORE\cake\libs\model\behaviors\upload.php,第 107 行]

警告(2):无法修改标头信息 - 标头已由(输出开始于 E:\umoorthy_105act10\projects\dev1base\core\cake\basics.php:111)[CORE\cake\libs\controller\controller .php,第 614 行]


要做什么?

【问题讨论】:

  • 您实际使用的是什么行为?如果是 MeioUpload,此链接可能会有所帮助:mail-archive.com/cake-php@googlegroups.com/msg76460.html
  • 我正在使用图片上传行为..
  • 任何指向该特定行为的链接?粗略的 Google 搜索没有找到任何有用的信息。

标签: file cakephp upload


【解决方案1】:

Cake 已经写到哪里找问题了

Configure::read('photo.files')

执行以下操作以检查是否一切正常

pr(Configure::read('photo.files'))

【讨论】:

    【解决方案2】:
    public  function uploadFilesIphone($folder, $formdata, $replace , $itemId = null) {
                    // setup dir names absolute and relative    echo "<pre>";       print_r($formdata);     exit;
    
                $folder_url = WWW_ROOT.$folder;
                $rel_url = $folder; //echo
    
                    // create the folder if it does not exist
                    if(!is_dir($folder_url)) {
                        mkdir($folder_url);
                    }
    
                    // if itemId is set create an item folder
                    if($itemId) {
                        // set new absolute folder
                        $folder_url = WWW_ROOT.$folder.'/'.$itemId; 
                        // set new relative folder
                        $rel_url = $folder.'/'.$itemId;
                        // create directory
                        if(!is_dir($folder_url)) {
                            mkdir($folder_url);
                        }
                    }
    
                    // list of permitted file types, this is only images but documents can be added
                    $permitted = array('image/gif','image/jpeg','image/pjpeg','image/png','application/octet-stream');
    
                    // loop through and deal with the files;
    
                    $key = array();
                    $value = array();
                    foreach($formdata as  $key => $value) 
                    {   
                        if($key == is_array($value))
                        {
                            $filename = str_replace(".", $replace , $value['name']);
                        }   
    
                        // replace spaces with underscores
    
                        // assume filetype is false
                        $typeOK = false;
                        // check filetype is ok
    
                        foreach($permitted as $type) 
                        {   
                            if($key == is_array($value))
                            {
                                if($type == $value['type']) 
                                {
                                    $typeOK = true;
                                    break;
                                }
                            }   
                        }
                        // if file type ok upload the file
    
                        if($typeOK) {
                            // switch based on error code
                            if($key == is_array($value))
                            {
                                switch($value['error']) 
                                {
                                case 0:
                                    // check filename already exists
                                    if(!file_exists($folder_url.'/'.$filename)) 
                                    {
                                        // create full filename
                                        $full_url = $folder_url.'/'.$filename;
                                        $url = $rel_url.'/'.$filename;
    
                                        // upload the file
                                        if($key == is_array($value))
                                        {
                                            $success = move_uploaded_file($value['tmp_name'], $url);
                                        }
                                    } 
                                    else 
                                    {
                                        // create unique filename and upload file
                                        //  ini_set('date.timezone', 'Europe/London');
                                        $now = date('Y-m-d-His');
                                        $full_url = $folder_url.'/'.$now.$filename;
                                        $url = $rel_url.'/'.$now.$filename;
                                        if($key == is_array($value))
                                        {   
                                            $success = move_uploaded_file($value['tmp_name'], $url);
                                        }
                                    }
                                    // if upload was successful
                                    if($success) 
                                    {
                                        // save the url of the file
                                        $result['urls'][] = $url;
                                    } 
                                    else 
                                    {
                                        $result['errors'][] = "Error uploaded $filename. Please try again.";
                                    }
                                    break;
                                case 3:
                                    // an error occured
                                    $result['errors'][] = "Error uploading $filename. Please try again.";
                                    break;
                                default:
                                    // an error occured
                                    $result['errors'][] = "System error uploading $filename. Contact webmaster.";
                                    break;
                            }
                        } 
                        elseif($value['error'] == 4) 
                        {
                            // no file was selected for upload
                            $result['nofiles'][] = "No file Selected";
                        } 
                        else 
                        {
                            // unacceptable file type
                            $result['errors'][] = "$filename cannot be uploaded. Acceptable file types: gif, jpg, png.";
                        }
                      }
                    }
                return $result;
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-15
      • 2011-04-24
      • 2014-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-08
      相关资源
      最近更新 更多