如何使用cakephp更改上传img url?

如何使用cakephp更改上传img url?,php,cakephp,cakephp-1.3,cakephp-1.2,Php,Cakephp,Cakephp 1.3,Cakephp 1.2,我正在使用此代码上载图像..此代码保存将url上载到数据库表中,以便再次使用url查看上载的图像..问题是更改保存到数据库中的url,而不是更改上载文件夹(缩短ulr以保存),如下所示 从 到 我需要的这一更改使我可以轻松地使用image()helper <?php /** * App Controller * * file: /app/app_controller.php */ class AppController extends Controller { /**

我正在使用此代码上载图像..此代码保存将url上载到数据库表中,以便再次使用url查看上载的图像..问题是更改保存到数据库中的url,而不是更改上载文件夹(缩短ulr以保存),如下所示

我需要的这一更改使我可以轻松地使用image()helper

<?php
/**
 * App Controller
 *
 * file: /app/app_controller.php
 */
class AppController extends Controller {

    /**
     * slug()
     * creates a slug from a string
     */
    function slug($str) {
        // replace spaces with underscore, all to lowercase
        $str = strtolower(str_replace(' ', '_', $str));

        // create regex pattern
        $pattern = "/[^a-zA-Z0-9_]/";

        // replace non alphanumeric characters
        $str = preg_replace($pattern, '', $str);

    return $str;

}


    /**
     * uploads files to the server
     * @params:
     *      $folder     = the folder to upload the files e.g. 'img/files'
     *      $formdata   = the array containing the form files
     *      $itemId     = id of the item (optional) will create a new sub folder
     * @return:
     *      will return an array with the success of each file upload
     */
    function upload_files($folder, $formdata, $item_id = null) {
        // setup dir names absolute and relative
        $folder_url = WWW_ROOT.$folder;
        $rel_url = $folder;

        // 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($item_id) {
            // set new absolute folder
            $folder_url = WWW_ROOT.$folder.'/'.$item_id;

            // set new relative folder
            $rel_url = $folder.'/'.$item_id;
            // 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');

        // loop through and deal with the files
        foreach($formdata as $file) {
            // replace spaces with underscores
            $filename = str_replace(' ', '_', $file['name']);
            // assume filetype is false
            $typeOK = false;
            // check filetype is ok
            foreach($permitted as $type) {
                if($type == $file['type']) {
                    $typeOK = true;
                    break;
                }
            }

            // if file type ok upload the file
            if($typeOK) {
                // switch based on error code
                switch($file['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
                            $success = move_uploaded_file($file['tmp_name'], $url);
                        } else {
                            // create unique filename and upload file
                            ini_set('date.timezone', 'Europe/London');
                            $now = date('d-m-Y-His');
                            $full_url = $folder_url.'/'.$now.$filename;
                            $url = $rel_url.'/'.$now.' - '.$filename;
                            $success = move_uploaded_file($file['tmp_name'], $url);
                        }
                        // if upload was successful
                        if($success) {
                            // save the url of the file(i want to change this code)
                            $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($file['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;
    }

}
?>

你能不能不简单地改变一下

 $result['urls'][] = $url;


upload_files
的描述中说“$folder=上传文件的文件夹,例如‘img/files’”。是否需要其他内容?$result['url'][]=$url;这段代码将文件的url保存到数据库中..我不需要更改上载文件夹..我只需要像这样保存shourt url上载..dvds/sunset.jpg而不是img/dvds/sunset.jpgi如果变量文件夹让您有机会按照您的要求执行操作,则您无法理解您试图执行的操作。不要在可变文件夹中传递img/DVD,只需传递DVD。。。。或者,如果您想手动执行,您可以尝试
$tmp=explode(“/”,$folder);未结算($tmp[0])$文件夹=内爆(“/”,$tmp)<?php
/**
 * App Controller
 *
 * file: /app/app_controller.php
 */
class AppController extends Controller {

    /**
     * slug()
     * creates a slug from a string
     */
    function slug($str) {
        // replace spaces with underscore, all to lowercase
        $str = strtolower(str_replace(' ', '_', $str));

        // create regex pattern
        $pattern = "/[^a-zA-Z0-9_]/";

        // replace non alphanumeric characters
        $str = preg_replace($pattern, '', $str);

    return $str;

}


    /**
     * uploads files to the server
     * @params:
     *      $folder     = the folder to upload the files e.g. 'img/files'
     *      $formdata   = the array containing the form files
     *      $itemId     = id of the item (optional) will create a new sub folder
     * @return:
     *      will return an array with the success of each file upload
     */
    function upload_files($folder, $formdata, $item_id = null) {
        // setup dir names absolute and relative
        $folder_url = WWW_ROOT.$folder;
        $rel_url = $folder;

        // 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($item_id) {
            // set new absolute folder
            $folder_url = WWW_ROOT.$folder.'/'.$item_id;

            // set new relative folder
            $rel_url = $folder.'/'.$item_id;
            // 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');

        // loop through and deal with the files
        foreach($formdata as $file) {
            // replace spaces with underscores
            $filename = str_replace(' ', '_', $file['name']);
            // assume filetype is false
            $typeOK = false;
            // check filetype is ok
            foreach($permitted as $type) {
                if($type == $file['type']) {
                    $typeOK = true;
                    break;
                }
            }

            // if file type ok upload the file
            if($typeOK) {
                // switch based on error code
                switch($file['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
                            $success = move_uploaded_file($file['tmp_name'], $url);
                        } else {
                            // create unique filename and upload file
                            ini_set('date.timezone', 'Europe/London');
                            $now = date('d-m-Y-His');
                            $full_url = $folder_url.'/'.$now.$filename;
                            $url = $rel_url.'/'.$now.' - '.$filename;
                            $success = move_uploaded_file($file['tmp_name'], $url);
                        }
                        // if upload was successful
                        if($success) {
                            // save the url of the file(i want to change this code)
                            $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($file['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;
    }

}
?>
 $result['urls'][] = $url;
 $result['urls'][] = substr($url, 4);