Question

I am attempting to insert the data from an uploaded file into a single dimension array.

The file is as such that there is one student number to a line like so:

392232,
392231,
etc

this is the most common way I've found online:

while (($line = fgetcsv($file, 25, ',')) !== FALSE) {
        //$line is an array of the csv elements
        print_r($line);
        }

However form what I understand this will create an array ($line) for each row. Which is not what I want.

that aside I tried this to see if it is working and my code is not printing out the array after using ftgetcsv(). The file is successfully uploading.

here is my code:

    if(isset($_FILES['csv_file']) && is_uploaded_file($_FILES['csv_file']['tmp_name'])){

    //create file name
    $file_path = "csv_files/" . $_FILES['csv_file']['name'];

    //move uploaded file to upload dir
    if (!move_uploaded_file($_FILES['csv_file']['tmp_name'], $file_path)) {
        //error moving upload file
        echo "Error moving uploaded file";
    }

    print_r($_FILES['csv_file']);

    $file = fopen('$file_path', 'r');

    while (($line = fgetcsv($file, 25, ',')) !== FALSE) {
    //$line is an array of the csv elements
    print_r($line);
    }

    //delete csv file
    unlink($file_path);
}

First off, can anyone obviously see why it wouldnt work to at least print them as seperate arrays of data (each row).

Second, is it possible to set it so that it creates a 1d array of all rows in the file?

Many thanks,

Was it helpful?

Solution

Question 1 is because of

print_r($_FILES['csv_file']);
$file = fopen('$file_path', 'r');

should be:

$file = fopen($file_path, 'r');

and for Question 2, check out the array_push

OTHER TIPS

1st Question:

This line will actually try to open a file called '$file_path' because you're using single quotes (so it doesn't expand to the value of the variable). You can just remove the quotes.

$file = fopen('$file_path', 'r');

$file is null after this.

2nd Question:

If all you want to do is convert a file into an array by lines you can use one of these instead:
file() - get whole file into a 1D array of lines of the file (closest to what you want)
fgets() - get a string per line per call; keep calling this until it returns false to get each line one at a time
file_get_contents() - get the whole file into a string and process as you like

According to PHP.net $line has to return as array. "returns an array containing the fields read." But if you are sure it's contains only one student number you can use $line[0] to get the first line value (Ignoring the ",")

Here are some general comments on your code:

  1. You are passing the file path into the fopen() function incorrectly. The variable should not be surrounded with single quotes.
  2. Since you are deleting the CSV file after processing it, moving it is unnecessary. Simply use $_FILES['csv_file']['tmp_name'] as the path to the file.
  3. Since there is only one entry per row in your CSV file, simply access the first element of the array that is returned from fgetcsv(): $numbers[] = $line[0];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top