doujian3132 2015-12-02 14:33
浏览 94

当文件名有空格时,php move_uploaded_file无效

i am uploading a file to the server. everything works fine unless the uploaded file has a space in it.

I tried to use: str_replace(" ", "_", $_FILES['image']['name']);

My code is

 $image_name= str_replace(" ", "_", $_FILES['image']['name']);
               $image_tmp_name = $_FILES['image']['tmp_name'];
               $image=$_FILES['image'];

              $url = "http://jkshahclasses.com/push_images/$image_name";
                if(move_uploaded_file($image_tmp_name,"../../push_images/$image_name"))
                {
                    echo "file uploaded";
                }
                else
                {
                    echo "error: file not uploaded";
                    }

Thanks in advance

  • 写回答

1条回答 默认 最新

  • douhuan2101 2015-12-02 15:08
    关注

    You can use this function to slugify your file name before uploded :

      public function slugify($text)
      {
        // replace all non letters or digits by _
        $text = preg_replace('/\W+/', '_', $text);
    
        // trim and lowercase
        $text = strtolower(trim($text, '_'));
    
        return $text;
      }
    

    But first you have to get only file name you can do it with this function :

    $file_name = pathinfo($path, PATHINFO_FILENAME);
    

    If you want to get file extension you have to use :

    $file_ext = pathinfo($path, PATHINFO_EXTENSION);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000