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

发布于 2024-12-11 18:20:46 字数 4865 浏览 0 评论 0原文

我正在使用此代码上传图像..此代码将上传网址保存到数据库表中以再次使用该网址来查看已上传的图像..问题是更改保存到数据库中的网址而不是更改上传文件夹(使要保存的短 ulr)就像这样

img/dvds/sunset.jpg

dvds/sunset.jpg

我需要的这个更改使我可以轻松使用 image() 助手..

<?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;
    }

}
?>

i am use this code to upload image ..this code save uploads url into database table to reuase the url again to view image was uploaded..the problem is to change url that saved into database not to change upload folder (make short ulr to save) like this

from

img/dvds/sunset.jpg

to

dvds/sunset.jpg

this change that i need make me use image() helper easy..

<?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;
    }

}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

靖瑶 2024-12-18 18:20:46

你能不能简单地

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

改为

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

Could you not simply change

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

to

 $result['urls'][] = substr($url, 4);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文