I can't get fgetcsv()
to work with a variable from $_POST
( delimiter = (string)$_POST['delimiter'];)
I works if I specify: $delimiter = "\t";
but not when I use: $delimiter = (string)$_POST['delimiter'];
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>
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']);
Why not make it foolproof like this:
$options = array(0=> "\t", 1 => "
");
$delimiter = $options[ (int) $_POST['delimiter'] ];
And use the keys of your options instead of the delimiter itself. Safer and foolproof.
I came across this issue and resolve it by following code:
$delimiter = $_POST['delimiter'];
$delimiter = str_replace("\\t","\t",$delimiter);
Pretty late, but hope it help someone being crazy about this mystery.