dpwuvfpu52502
2016-05-30 03:46
浏览 53
已采纳

在Code Igniter中上传带有空格的文件名的图像问题

I am developing a website using Angular JS + Codeigniter.

The problem lies with image upload. The file names with space will not be uploaded (eg: "first image.jpg").

If we remove the space then it gets uploaded(eg:"firstimage.jpg"). This is my configuration.

$config = array(
  'allowed_types'     => 'jpg|jpeg|gif|png', 
  'max_size'          => 2048, //2MB max
  'upload_path'       => $this->original_path,
  'remove_spaces'     => true
);

$this->load->library('upload', $config);
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongre6227 2016-05-30 09:48
    已采纳

    Remove nunecessary white space and symbols from your file name by using.

    $file_name = $_FILES["userfiles"]['name'];
    $newfile_name= preg_replace('/[^A-Za-z0-9]/', "", $file_name);
    
    $config = array('allowed_types'     => 'jpg|jpeg|gif|png', 
    'upload_path'       => $this->original_path,
    'file_name'=>$newfile_name);
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dsfsda121545 2016-05-30 03:54

    Firstly I'd ask "What's wrong with stripping spaces in the way that you have?" Do you really want to keep uploaded files with spaces in them?

    The clean way to do this would be to formalise what you have already found and consolidate the logic into a new class or method on some existing (controller?) class:

    • Decide what the full range of acceptable characters is for user-uploaded filenames in your app / site.
    • Create a regular expression that converts various input strings (e.g. filenames with spaces, tab characters and any other unwanted stuff) into a useable filename according to your requirements
    • Call that function / class::function accordingly
    评论
    解决 无用
    打赏 举报
  • douwen5924 2016-05-31 06:06

    try this function if only white space issue in your filename:

    when you get your data into controller in that function you have to use trim before pass to model like this :

    $file_name = trim($_FILES["userfiles"]['name']); so you can easily remove white space from filename.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题