search
HomeBackend DevelopmentPHP Tutorial 文件上传编码有关问题

文件上传编码问题
用Flash+PHP实现文件的批量上传。PHP在保存文件时,遇到编码问题:

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?php header("Content-Type:text/html;charset=utf-8");
    function createDir($path){
        if (!file_exists($path)){
            createDir(dirname($path));
            mkdir($path, 0777);
        }
    }
    $uploaddir='upfile/'.date('Ymd').'/';
    createDir($uploaddir);
    $uploadfile=$uploaddir.iconv('utf-8','gbk',$_POST['fn']);
    if (move_uploaded_file($_FILES['Filedata']['tmp_name'], $uploadfile)) {
        echo 'OK'.$_POST['bn'].'{[(*})]'.$_POST['up'].'{[(*})]'.'$uploadjson'.'{[(*})]'.'$listjson';
    }else{
        echo 'ERROR';
    }
?>

文件名很不规范,有中文、英文、法文、俄文、……不确定类型,这时候有些文件保存下来,要么是乱码,要么被截断(扩展名都丢失了)。iconv('utf-8','gbk',$_POST['fn']),这里的问题?该如何做?

------解决方案--------------------
你当前页面用了utf-8
header("Content-Type:text/html;charset=utf-8");


而这个应不该转成gbk
$uploadfile=$uploaddir.iconv('utf-8','gbk',$_POST['fn']);

去掉或是
$uploadfile=$uploaddir.iconv('gbk','utf-8', $_POST['fn']);
试试
------解决方案--------------------
GBK编码的字符串没有俄文这些符号。也就是说它只适用于中文
建议你用utf-8来保存,文件名乱码的问题就这么撂着吧
------解决方案--------------------
$uploadfile = $uploaddir . base64_encode($_POST['fn']) . '.' . pathinfo($_POST'fn'], PATHINFO_EXTENSION);
------解决方案--------------------
你数据源是GBK要怎子搞呢。
iconv用//IGNORE忽略特殊符号转成UTF8、要么就重命名。
------解决方案--------------------
你自己在你的windows机上能够输入俄文,阿拉伯文命名一个文件吗?
文件名要视乎客户操作系统的编码,一般国内用户用的都是中文版windows,系统默认内部编码属GBK,GBK怎么可能包含俄文之类的语种呢。或者换过来说你让操作系统编码为iso-8859-1的用户下载你的中文名文件,中文即使给你转成gbk还不一样乱码。干脆就随机命名好了,不然有啥好方法,全世界各地不同的操作系统用户系统编码千差万别,除非所有都统一用utf-8做内码,你这边也不用费劲用iconv转了。
你看我现在用ubuntu,系统编码就是utf-8的,那些中文俄文法文你不转GBK对我这种用户就算正常的,你转GBK倒还乱码了。呵呵。
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is the difference between unset() and session_destroy()?What is the difference between unset() and session_destroy()?May 04, 2025 am 12:19 AM

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

What is sticky sessions (session affinity) in the context of load balancing?What is sticky sessions (session affinity) in the context of load balancing?May 04, 2025 am 12:16 AM

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

What are the different session save handlers available in PHP?What are the different session save handlers available in PHP?May 04, 2025 am 12:14 AM

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

What is a session in PHP, and why are they used?What is a session in PHP, and why are they used?May 04, 2025 am 12:12 AM

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

Explain the lifecycle of a PHP session.Explain the lifecycle of a PHP session.May 04, 2025 am 12:04 AM

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

What is the difference between absolute and idle session timeouts?What is the difference between absolute and idle session timeouts?May 03, 2025 am 12:21 AM

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

What steps would you take if sessions aren't working on your server?What steps would you take if sessions aren't working on your server?May 03, 2025 am 12:19 AM

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

What is the significance of the session_start() function?What is the significance of the session_start() function?May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment