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

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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?