-1

I have been trying to upload a CSV and manipulate the data but I dont think its being uploaded properly. My code is:

HTML

        <form action="import_script.php" method="post" enctype="multipart/form-data">
            Please select a file in the form of a CSV
            <input type="file" name="file" id="fileToUpload">
            <input type="submit" value="Upload CSV" name="submit">
        </form>

PHP

    $csv_import = fopen($_FILES['file']['tmp_name'], 'r+');

    //Echo file details
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    $row = fgetcsv($csv_import, 8192);
    print_r($row);

The echoed output is:

Upload: Data_July_6_2017.csv

Type: 

Size: 0 Kb

Temp file:

Im trying to get it to upload previously generated csv file and read print the results of a fgetcsv() on the first row but I am getting no data from the file and the file info appears all wrong.

Kynan Pacheco
  • 207
  • 2
  • 3
  • 10
  • http://php.net/manual/es/function.move-uploaded-file.php – Hackerman Jul 10 '17 at 18:45
  • where is data manipulation? you just printed the file properties. – Just_Do_It Jul 10 '17 at 18:58
  • The file I am using to test this is a 10kb file that contains test data. When I upload it and use fgetcsv() There is no data there. Given the output of the echo functions above I am sure that the problem is with the file upload or the method of opening the file. The data manipulation is simple and I have already verified that it is correct. @Hackerman - I am unsure this will be of any use to me given that I don't want to actually save the file, I want to keep it as a temp file (this may be the root of my issue I'm not sure.) – Kynan Pacheco Jul 11 '17 at 14:01
  • @Just_Do_it - I have made some edits that may clarify my issue. – Kynan Pacheco Jul 11 '17 at 14:01
  • In your form try using this : instead of – Just_Do_It Jul 11 '17 at 14:20
  • Unfortunately the same output @Just_Do_It – Kynan Pacheco Jul 11 '17 at 14:25
  • Did you tried using $csv_import = fopen($_FILES['file']['tmp_name'], 'r'); instead of $csv_import = fopen($_FILES['file']['tmp_name'], 'r+'); – Just_Do_It Jul 11 '17 at 14:33
  • What have you tried other than what you have? The first google result when searching "php upload csv file" is [quite comprehensive](https://stackoverflow.com/a/5593870/1155833). Before your next question, please read [How to Ask](https://stackoverflow.com/help/how-to-ask) – rkeet Jul 11 '17 at 14:37
  • @Just_Do_It The results are the same. – Kynan Pacheco Jul 11 '17 at 14:45
  • @Nukeface I have viewed that question multiple times the issue with it is that it doesn't cover fgetcsv(). It is my understanding that fgetcsv is the easiest and most efficient way to code this. – Kynan Pacheco Jul 11 '17 at 14:47
  • well using the same code you have above I created a form on my local pc and this is what I get: Upload: users.csv Type: application/vnd.ms-excel Size: 30.490234375 Kb Temp file: C:\xampp\tmp\phpD42C.tmp Array ( [0] => Passwd [1] => [2] =>mar_id [3] => contact_phone_number [4] => country [5] => state [6] => postal_code [7] => city [8] => address [9] => [10] => [11] => ) – Just_Do_It Jul 11 '17 at 14:58
  • @Just_Do_It You were able to get output? This must mean this is a server issue not a code issue – Kynan Pacheco Jul 11 '17 at 15:00
  • yes getting output as stated above, what server are you using locally? – Just_Do_It Jul 11 '17 at 15:01
  • SME Server 9 @Just_Do_It – Kynan Pacheco Jul 11 '17 at 15:05
  • Server uploads the file to a tmp directory (upload_tmp_dir). If it doesn't have permission to do that, it throws the error. – Just_Do_It Jul 11 '17 at 15:10
  • apparently the temp dir wasnt set, after folloing these directions [link](https://wiki.contribs.org/Uploadtmpdir) I get output of Upload: P2OASys_Data_July_6_2017.csv Type: application/vnd.ms-excel Size: 9.9609375 Kb Temp file: /tmp/phpdlQYhW – Kynan Pacheco Jul 11 '17 at 15:14
  • 1
    great..finally it worked :) – Just_Do_It Jul 11 '17 at 15:15
  • @Just_Do_It Thanks for your help and patients! – Kynan Pacheco Jul 11 '17 at 15:18

1 Answers1

0

Are you seeking something like this:

$filename=$_FILES["file"]["tmp_name"];      


         if($_FILES["file"]["size"] > 0)
         {
            $file = fopen($filename, "r");
            while (($getData = fgetcsv($file, 10000, ",")) !== FALSE)
             {
               echo $getData[0];
               echo $getData[1];
             }
            fclose($file);  
         }
Just_Do_It
  • 821
  • 7
  • 20