0

I am facing serious problem over some days. Trying to explode each line from something.txt file and make an array. But array structure not like what I want. My code is given below

    for ( $i=0; $i<count($_FILES['data']['name']['Question']['uploadfiles']); $i++) {
        foreach(file($_FILES['data']['name']['Question']['uploadfiles'][$i]) as $line) {
            $LineData[] = explode("\n",$line);
        }
    }

Some of the file lose the proper array structure. Just like

    Array
    (
        [0] => Array
            (
                [1] => 
    A. Hemophilia A
    B. Hemophilia B
                [2] => C. Von Willebrand disease
    D. Idiopathic thrombocytopenic purpura
            )
     )

But I want the array structure like this:

Array
(
    [0] => Array
        (
            [1] => A. Hemophilia A
            [2] => B. Hemophilia B
            [3] => C. Von Willebrand disease
            [4] => D. Idiopathic thrombocytopenic purpura
        )
 )
Martin Bean
  • 38,379
  • 25
  • 128
  • 201
Subir Das
  • 57
  • 9

3 Answers3

2

Try using carriage return "\r" instead of line feed "\n".

According to this article, Mac is different from Windows and Linux in that respect.

Community
  • 1
  • 1
Miqi180
  • 1,670
  • 1
  • 18
  • 20
  • Thanks Miqi180 for your answer. I have seen there is little improvement. This is nested array structure. But I do not want to see nested Array ( [0] => Array ( [1] => Array ( [0] => A. Hemophilia A [1] => B. Hemophilia B ) ) ) – Subir Das Oct 22 '15 at 13:20
  • This is a long shot, but how about combining the two, i.e. "\n\r"? – Miqi180 Oct 22 '15 at 13:47
  • Are you absolutely sure your input file is not malformed/corrupt? And have you considered a string cleanup before the split? – Miqi180 Oct 22 '15 at 13:58
  • Yes my file is not corrupted. And those code are using to split. No cleanup – Subir Das Oct 22 '15 at 14:57
  • Sorry for the late reply. Your file may still be malformed, even though it is not corrupted, and sometimes non-printable (invisible) characters tend to mess things up, so a cleanup may indeed be warranted after all. – Miqi180 Oct 22 '15 at 23:26
1

Save below on a PHP file and run in your local server and test

<?php

    if(isset($_POST['submit'])){
        $contents = file_get_contents($_FILES['file']['tmp_name']);
        $filearray = explode("\n", $contents);
        echo '<pre>'; print_r($filearray);
    }

?>

<!DOCTYPE html>
<html>
<body>
    <form enctype="multipart/form-data" method="post" name="uploadForm">
        <input type="file" name="file"  />
        <input type="submit" name="submit" value="Add" />
    </form>
</body>
</html>
Anto S
  • 2,448
  • 6
  • 32
  • 50
0

The file function already splits in lines. There is no reason to explode anything, unless you want to split each line separately.

In your case, the file call is sufficient, no need even for foreach:

$lineData = file($_FILES['data']['name']['Question']['uploadfiles'][$i]);
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195