【问题标题】:uploading file to server saves file with invalid characters将文件上传到服务器会保存包含无效字符的文件
【发布时间】:2014-10-14 12:44:04
【问题描述】:

我正在从我的 java 应用程序将文件上传到 php 服务器,它上传一切正常,但我注意到如果它有一个字符,例如 ' ,那么在文件中它将是 \' 而不是 '

例如,如果我在 java 中使用以下代码上传文件:

public static void main(String[] args)
{
    FILE tmpFile = new FILE(EncryptionUtil.EncryptAndZip("tes't file.txt").getAbsoluteFile().toString());

    postData
    (
        UPLOAD_URL + "?filename=" + URLEncoder.encode(file.getFileName() + ".zip", "utf-8"),
        tmpFile.getFileName() + ".zip",
        tmpFile.getFileLoader().readAllBytes() // FILE.getFileLoader().readAllBytes() is just a wrapper and reads all bytes from a file to byte[]
    );
}

private static final String CrLf = "\r\n";
public static void postData(String url, String filename, byte[] byteData) throws IOException
{
    URLConnection conn = null;
    InputStream is = null;
    OutputStream os = null;

    try
    {
        URL obj = new URL(url);
        System.out.println("url:" + obj);
        conn = obj.openConnection();
        conn.setDoOutput(true);

        String message1 = "";
        message1 += "-----------------------------4664151417711" + CrLf;
        message1 += "Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"" + filename + "\""
                + CrLf;
        message1 += "Content-Type: application/octet-stream" + CrLf;
        message1 += CrLf;

        String message2 = "";
        message2 += CrLf + "-----------------------------4664151417711--"
                + CrLf;

        conn.setRequestProperty("Content-Type",
                "multipart/form-data; boundary=---------------------------4664151417711");

        conn.setRequestProperty("Content-Length", String.valueOf((message1
                .length() + message2.length() + byteData.length)));

        os = conn.getOutputStream();

        os.write(message1.getBytes());

        int index = 0;
        int size = 1024;
        do
        {
            if ((index + size) > byteData.length)
            {
                size = byteData.length - index;
            }
            os.write(byteData, index, size);
            index += size;
        }
        while (index < byteData.length);

        os.write(message2.getBytes());
        os.flush();

        is = conn.getInputStream();

        char buff = 512;
        int len;
        byte[] data = new byte[buff];
        do
        {
            len = is.read(data);

            if (len > 0)
            {
                System.out.println(new String(data, 0, len));
            }
        }
        while (len > 0);

    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        try
        {
            os.close();
        }
        catch (Exception e)
        {
        }
        try
        {
            is.close();
        }
        catch (Exception e)
        {
        }
        try
        {
        }
        catch (Exception e)
        {
        }
    }
}

来自服务器的响应

url: http://127.0.0.1/cloudstore/upload.php?filename=tes%27t+.txt.zip
name of file user wants to save as: tes\'t file.txt.zip
actual save name: tes\'t file.txt.zip
Array
(
    [name] => tes't file.txt.zip
    [type] => application/octet-stream
    [tmp_name] => /tmp/phpgYl0QT
    [error] => 0
    [size] => 1274598
)

下面是我的php文件upload.php

<?php 

$target_path = "" . $_GET['filename'];
echo '  name of file user wants to save as: '.$target_path.'<br/>';
if (!file_exists(dirname($target_path))) {
    mkdir(dirname($target_path), 0777, true);
}

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
    echo "actual save name: ".basename( $_FILES['uploadedfile']['name'])." <br/>"; 
} else{ 
    echo "There was an error uploading the file, please try again!<br />"; 
}

print_r($_FILES['uploadedfile']);
?> 

什么会导致这个问题我认为是 php 试图清理输入?我将如何阻止这种情况发生?

【问题讨论】:

  • 一开始就不允许这些(字符)。使用str_replace()' 替换为下划线,或不替换。你也可以试试stripslashes()
  • @Fred-ii- stripslashes() 工作,出于某种原因,我以为我已经关闭了自动卫生。
  • 太好了,很高兴听到它。
  • @Fred-ii- 你能把答案贴出来让我接受吗?
  • 张贴,根据您的要求,干杯

标签: java php file-upload


【解决方案1】:

按照 OP 的要求,关闭问题。

使用stripslashes()

它有助于取消引用带引号的字符串,这是原因。

如果需要这样保存,它将把 tes\'t 更改为 tes't,如果需要的话。

来自manual

stripslashes() 的一个使用示例是当 PHP 指令 magic_quotes_gpc 处于打开状态时(在 PHP 5.4 之前它默认处于打开状态),并且您没有将此数据插入到需要的位置(例如数据库)逃脱。例如,如果您只是直接从 HTML 表单输出数据。

【讨论】:

  • 是的,我以为我关闭了magic_quotes_gpc,这就是为什么我对转义的原因感到困惑。
  • @user3045798 很高兴我们找到了问题的根源 :)
【解决方案2】:

如果打开了魔术引号,我通常更愿意关闭它。顺便说一句,它已经在较新的 PHP 版本中被弃用了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多