dongxie5698
2014-07-10 09:33
浏览 41
已采纳

cakePHP:从tmp_name中提取没有文件路径的文件名

In my cakePHP Model i'm trying to use a custom validator to move file to new location an check its success. the custom method is:

    //custom method to check image
public function processCoverUpload($check = array()) {
    if (!is_uploaded_file($check['issue_cover']['tmp_name'])) {
        return FALSE;
    }
    if (!move_uploaded_file($check['issue_cover']['tmp_name'], WWW_ROOT . 'img' . DS . 'uploads' . DS . $check['issue_cover']['tmp_name'])) {
        return FALSE;
    }
    $this->data[$this->alias]['issue_cover'] = 'uploads' . DS . $check['issue_cove']['tmp_name'];
    return TRUE;
}

however $check['issue_cove']['tmp_name'] returns file name including its path. This will lead to wrong path string in move_uploaded_file. So how can i have only file name out of 'tmp_name'?

sample

$check['issue_cove']['tmp_name'] is F:\xampp\tmp\phpF765.tmp but i want phpF765.tmp only.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dtzh131555 2014-07-10 09:44
    已采纳

    Here is one way - probably nicer ways though :-)

    $fname = end(explode('\\',$check['issue_cover']['tmp_name']));
    

    $fname should now have the string you want.

    ps assumed it should be issue_cover and not issue_cove so change if my assumption was incorrect!

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题