如何在 Codeigniter 中上传的文件名中添加前缀?

发布于 2024-12-27 13:41:53 字数 492 浏览 3 评论 0原文

我的网站上有一个文件上传表单,希望在用户上传照片时在文件名的开头添加前缀。

我已经设置了上传配置(见下文),但不确定如何设置前缀。

我最好不想打扰 ['encrypt_name'],因为这可以节省我的时间和精力来制作唯一的名称,但在加密名称的开头添加前缀。我将把用户 ID 添加到开头。

    $config['upload_path'] = $upload_path;
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';
    $config['encrypt_name'] = true;

    $this->load->library('upload', $config);

总是非常感谢帮助。

I have a file upload form on my website and would like to add a prefix to the beginning of the file name when a user uploads a photo.

I have set my configuration for the upload (see below) but am unsure how to set a prefix.

I preferably do not want to disturb the ['encrypt_name'] as this saves me time and effort making unique names but prefix to the beginning of the encrypted name. I will be adding the users id to the beginning.

    $config['upload_path'] = $upload_path;
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';
    $config['encrypt_name'] = true;

    $this->load->library('upload', $config);

Help always greatly appreciated.

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

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

发布评论

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

评论(1

香橙ぽ 2025-01-03 13:41:53

使用 $this->upload->data() 获取文件名可能是最简单的,然后使用 php rename() 将 id 添加到开头。

像这样的东西:

$upload_data = $this->upload->data();
rename(
    $upload_data['full_path'],
    $upload_data['file_path'].'/'.$user_id.'_'.$upload_data['orig_name']
);

It's probably easiest to get the filename using $this->upload->data(), and then using php rename() to add the id to the beginning.

Something like this:

$upload_data = $this->upload->data();
rename(
    $upload_data['full_path'],
    $upload_data['file_path'].'/'.$user_id.'_'.$upload_data['orig_name']
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文