2

我无法fgetcsv()使用来自的变量$_POST

(  delimiter = (string)$_POST['delimiter'];)

如果我指定,我会工作:$delimiter = "\t";

但不是当我使用: $delimiter = (string)$_POST['delimiter'];

  1. HTML

    <html>
    <body>
    <form enctype="multipart/form-data" action="upload.php" method="post">
            1) Select a delimiter.<br />
            <input type="radio" name="delimiter" value="\t"checked="yes" /> Tab<br />
            <input type="radio" name="delimiter" value=" "/> Space<br />
            <input type="radio" name="delimiter" value=","/> Comma<br />
            <input type="radio" name="delimiter" value=";"/> Semicolon<br /><br />
            <input type="hidden" name="MAX_FILE_SIZE" value="1000">
            2) Send this file: <input name="userfile" type="file"><br /><br />
            3) <input type="submit" value="Send Info">
    </form>
    <br />
    </body>
    </html>
    
4

3 回答 3

3

Already $_POST is a string. Why do you need (string) in front of the $_POST? Just remove it. Just have:

delimiter = $_POST['delimiter'];

This should work.


Also, you should not use fgetcsv, as you aren't fetching a file. You need str_getcsv.

str_getcsv($_POST["csv"], $_POST['delimiter']);
于 2013-02-14T16:16:01.957 回答
1

为什么不让它像这样万无一失:

 $options = array(0=> "\t", 1 => "\n");
 $delimiter = $options[ (int) $_POST['delimiter'] ];

并使用您的选项的键而不是分隔符本身。更加安全和万无一失。

于 2013-02-14T16:19:23.647 回答
0

我遇到了这个问题并通过以下代码解决了它:

$delimiter = $_POST['delimiter'];
$delimiter = str_replace("\\t","\t",$delimiter);

很晚了,但希望它可以帮助那些为这个谜团疯狂的人。

于 2016-07-23T00:51:17.210 回答