简体   繁体   English

PHP文件上传->替换特殊字符不起作用

[英]PHP file upload -> replace special chars didn't work

I'm now searching for a solution for two hours, but nothing I found online helped... 我现在正在寻找解决方案两个小时,但是我在网上发现的任何内容都无济于事...

I have a webservice with an upload form, which both work fine. 我有一个带有上传表单的网络服务,两者都可以正常工作。 I'm more into JS, my whole service is built in HTML5, CSS3 and JS... but my Upload Form is PHP. 我更喜欢JS,我的整个服务都建立在HTML5,CSS3和JS中...但是我的上传表单是PHP。 I found a script online and it worked, so I was fine. 我在网上找到了一个脚本,它可以工作,所以很好。 Now I realized, that there is a problem with my service, when I want to upload files with chars like ä,ö,ü and ß. 现在我意识到,当我要使用ä,ö,ü和ß等字符上传文件时,服务存在问题。 Unfortunately here in Germany these are common chars in our language, so I have to replace them with other chars. 不幸的是,在德国,这些是我们语言中常见的字符,因此我必须用其他字符替换它们。

I had a look here and there and at the moment my code looks like this: 我到处看看,此刻我的代码如下:

$files = $_FILES['fileselect'];

foreach ($files['error'] as $id => $err) {
    if ($err == UPLOAD_ERR_OK) {

        $fn = $files['name'][$id];

        $search_array  = array ('ä', 'ö', 'ü', 'ß');
        $replace_array = array ('ae', 'oe', 'ue', 'ss');

        $gfn = str_replace($search_array, $replace_array, $fn);

        move_uploaded_file(
            $files['tmp_name'][$id],
            'bildtransfer/' . $gfn
        );

        echo "<p>Die Datei $gfn wurde hochgeladen.</p>";
    }
}

The upload itself still works with files as long as they don't have those special chars. 只要文件没有特殊字符,上传本身仍然可以使用。 My file still has all special chars and it seems like there haven't been any replacement? 我的文件仍然包含所有特殊字符,似乎还没有替换?

Do I need preg_replace instead of str_replace? 我需要preg_replace而不是str_replace吗? I tried it, but it didn't work either... 我尝试过,但是也没有用...

I hope you can help me? 我希望你能帮帮我? Would be very pleased!!! 会很高兴的!!! :) :)

It looks like you're using the $_FILES array incorrectly. 看来您错误地使用了$ _FILES数组。 Any error will be stored in $_FILES['fileselect']['error'] - there's no need for the foreach statement. 任何错误都将存储在$_FILES['fileselect']['error'] -不需要foreach语句。

The original name of the file is stored in $_FILES['fileselect']['name'] , so try using that in the str_replace() call: 文件的原始名称存储在$_FILES['fileselect']['name'] ,因此请尝试在str_replace()调用中使用该$_FILES['fileselect']['name']

$file = $_FILES['fileselect'];
if($file['error'] == UPLOAD_ERR_OK) {
    $fn = $file['name'];

    $search_array  = array ('ä', 'ö', 'ü', 'ß');
    $replace_array = array ('ae', 'oe', 'ue', 'ss');

    $gfn = str_replace($search_array, $replace_array, $fn);

    move_uploaded_file(
        $file['tmp_name'],
        'bildtransfer/' . $gfn
    );

    echo "<p>Die Datei $gfn wurde hochgeladen.</p>";
}

To be honest I'm not sure why it works at the moment! 老实说,我不知道为什么现在能正常工作! Have a read through the PHP documentation on Handling file uploads . 阅读有关处理文件上传的PHP文档。

To fix the problem so you don't have to bother replacing characters, try telling the browser you're using UTF-8 when you send them, as suggested by @aaron: 要解决此问题,以使您不必麻烦替换字符,请按照@aaron的建议,在发送字符时告诉浏览器您正在使用UTF-8:

header('Content-Type: text/html; charset=utf-8');

It could be an encoding issue. 这可能是编码问题。 Try changing the encoding type to UTF-8 in the headers. 尝试将标头中的编码类型更改为UTF-8。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM