fgetcsv

Out of memory error when reading a large file

房东的猫 提交于 2020-01-21 06:56:37
问题 I have a large csv that I want to parse and insert into my database. I have this PHP: $target = '../uploads/'.$f; $handle = fopen($target, "r"); $data = fgetcsv($handle, 0, ","); $rows = array(); while ($data !== FALSE) { $rows[] = $data; } fclose($handle); if (count($rows)) { foreach ($rows as $key => $value) { echo $value; } } Every time I try to run my script I get this error: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 35 bytes) Any ideas how to do

Data gets garbled when writing to csv with fputcsv() / fgetcsv()

我的未来我决定 提交于 2020-01-17 07:02:37
问题 There seems to be an encoding issue or bug in PHP with fputcsv() and fgetcsv(). The following PHP code: $row_before = ['A', json_encode(['a', '\\', 'b']), 'B']; print "\nBEFORE:\n"; var_export($row_before); print "\n"; $fh = fopen($file = 'php://temp', 'rb+'); fputcsv($fh, $row_before); rewind($fh); $row_after = fgetcsv($fh); print "\nAFTER:\n"; var_export($row_after); print "\n\n"; fclose($fh); Gives me this output: BEFORE: array ( 0 => 'A', 1 => '["a","\\\\","b"]', 2 => 'B', ) AFTER: array

Find if a value exist in a CSV file with PHP

走远了吗. 提交于 2020-01-11 06:21:50
问题 This is my code to check if a row of my .csv file contains a specific name, but it does not work. I think it has something to do with the if statement. $file_handle = fopen("sources.csv", "r"); while (!feof($file_handle) ) { $line_of_text = fgetcsv($file_handle, 1024); if ($line_of_text[0] = 'paul') { echo 'Found'; } } fclose($file_handle); I am trying to check in sources.csv files, for the name 'Paul' . I can't use a database like MySQL for technical reasons. 回答1: Since you haven't provided

Using curl as an alternative to fopen file resource for fgetcsv

試著忘記壹切 提交于 2020-01-01 03:10:09
问题 Is it possible to make curl, access a url and the result as a file resource? like how fopen does it. My goals: Parse a CSV file Pass it to fgetcsv My obstruction: fopen is disabled My chunk of codes (in fopen) $url = "http://download.finance.yahoo.com/d/quotes.csv?s=USDEUR=X&f=sl1d1t1n&e=.csv"; $f = fopen($url, 'r'); print_r(fgetcsv($f)); Then, I am trying this on curl. $curl = curl_init(); curl_setopt($curl, CURLOPT_VERBOSE, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, false); curl

fgetcsv skip blank lines in file

孤街醉人 提交于 2019-12-30 08:08:09
问题 I have this script that I did, it basically grabs all the files in my "logs" folder and merge them all in one array file, my only problem is that, sometimes the script breaks if there is blank line or empty line! how can I tell it to automatically skip blank empty lines and go to next? blank lines are not necessarily at the top or bottom! could be in the middle of the csv file <?php $csv = array(); $files = glob('../logs/*.*'); $out = fopen("newfile.txt", "w"); foreach($files as $file){ $in =

Converting CSV to array

柔情痞子 提交于 2019-12-29 05:25:11
问题 I have this array with airport codes and city names (around 3500 lines). code,city "Abilene, TX ",ABI "Adak Island, AK ",ADK "Akiachak, AK ",KKI "Akiak, AK ",AKI "Akron/Canton, OH ",CAK "Akuton, AK ",KQA "Alakanuk, AK ",AUK "Alamogordo, NM ",ALM I need to convert that file into a php array. This is my code so far: if(($handle = fopen('test.csv', 'r')) !== FALSE) { while (($data = fgetcsv($handle, 1000, ',', '"')) !== FALSE) { echo '<pre>'; print_r($data); echo '</pre>'; } fclose($handle); }

fgetcsv not reading enclosures

☆樱花仙子☆ 提交于 2019-12-24 11:28:44
问题 I am trying to read a CSV line using fgetcsv . However it seem not to care at all about enclosures. Here is what the line looks like: Super Administrator,"ROLE_SUPER_ADMIN, ROLE_GROUP_GUEST, ROLE_GROUP_WRITER, ROLE_USER_WRITER, ROLE_USER_GUEST" This is a valid line by CSV standards, and it should return the following with " as the enclosure and , as the delimiter: $l = fgetcsv($handle, 0, ',', '"'); array( [0] => 'Super Administrator', [1] => 'ROLE_SUPER_ADMIN, ROLE_GROUP_GUEST, ROLE_GROUP

Reading *.csv files from directory and showing the content of each file fails

£可爱£侵袭症+ 提交于 2019-12-23 09:15:34
问题 I need help with reading the folder and opening / outputting the csv data, I have used examples other people have written but none of them have worked for me. What I currently have is this, but it doesn't output the files: $files = scandir($PathToCreate.$version."/"); //scan the folder foreach($files as $file) { //for each file in the folder //The following is another example I found but does not output anything I just need to open each file and be able to output / target specific data $csv =

Converting csv files data to an array using php str_getcsv function

天涯浪子 提交于 2019-12-21 05:15:19
问题 I have string like which is created using fputcsv Date,Name,Hours 2013-01-02,"Test User",7:59 2013-01-03,"Test User",7:53 2013-01-04,"Test User",8:12 2013-01-07,"Test User",7:56 2013-01-08,"Test User",8:25 2013-01-09,"Test User",7:56 2013-01-10,"Test User",8:10 2013-01-11,"Test User",7:53 2013-01-14,"Test User",7:54 2013-01-15,"Test User",0:34 2013-04-01,"Test User",5:51 2013-04-02,"Test User",8:50 2013-04-03,"Test User",7:25 2013-04-04,"Test User",8:3 2013-04-05,"Test User","10:42:52[Not

Determine minimum and maximum of all CSV columns

こ雲淡風輕ζ 提交于 2019-12-19 04:07:20
问题 I need to find the minimum and maximum values of all the columns in CSV and use those values for my slider in my web page. Initially, I am determining the total columns in the CSV file and based on the total columns, I am creating those many sliders (like amazon's price slider). This is the piece of code for creating the sliders. <?php for ( $i = 1; $i < $totalcolumns; $i++ ) { echo '<input type="text" data-slider="true" data-slider-range="1,500" data-slider-step="1"/>'; } ?> In the above