2017-10-10 1 views
0

일회용, 일회용 CSV 형식 변환 스크립트 작업 중입니다. 한 공급자의 파일을 가져 와서 해당 필드를 다른 공급자의 형식으로 매핑하고 있습니다.fgetcsv 출력이 나타나지 않습니다.

지금까지, 나는 내가 처음의 것 1. 스트립 배열로 CSV 데이터를 넣어 헤더 행 2. 밖으로 3. 인쇄 내용 PHP 스크립트를 통해 실행하고자하는 CSV 파일이

Order Date (UTC Time Zone),Order Number,Customer Name,Customer Address,Customer Address2,City,State,Zip,Country,Product Name,Quantity,UPC,SKU,Shipping Carrier,Tracking Number 
2017-10-09,1000-000001-000001,John Doe,1234 ANY DR,"",ANY CITY,CA,90210,US,ACME® Product,1,,,"","" 

문제는 내 스크립트, 아래, 아무것도를 출력하지 않는다는 것이다 : 배열의 필드

CSV 파일이 샘플과 같은 (그래서 나는 그것이 작동하고 볼 수 있습니다). 단순히 배열이 있고 그 배열에 포함 된 데이터가 올바른 배열 인덱스에 들어 있는지 확인하기 만하면됩니다.

<?php 
// Remove headers from the first line of file "yyyymmdd.csv" and save it to an outfile named "yyyymmdd_clean.csv" 
ini_set('auto_detect_line_endings', true); 
$original = fopen("20171010.csv", "r"); 
$first = fgets($original,2048); #get first line. 
$outfile="20171010_clean.csv"; 
$o = fopen($outfile,"w"); 
while (!feof($original)) { 
    $buffer = fgets($original,2048); 
    // Save outfile 
    fwrite($o,$buffer); 
} 
// Close original file, but don't close outfile 
fclose($original); 

// Convert cleaned outfile into array of SourceLine[n],SourceField[n] 
while (($data = fgetcsv($o)) !== FALSE) { 
    echo "OrderID " . $data[0]; 
} 
// Now close the outfile. We're done. 
fclose($o); 
?> 

원격 개발 서버에서 실행 중입니다.

데이터의 첫 번째 필드를 화면에 표시하도록하려면 무엇을 변경해야합니까?

+1

출력 파일 포인터가 파일 끝에 있기 때문에 [rewind()] (http://www.php.net/manual/en/function.rewind.php)해야합니다. –

+1

"읽기/쓰기"모드가 아닌 "쓰기 전용"모드로 열기. 단순히 "w"가 아닌''w + ''가 필요합니다. –

+0

감사합니다. 그게 내가 놓친 거였어. –

답변

0

Mark Baker으로 명시된 바와 같이 문제는 내가 rewind()을 사용하지 않아서 읽기/쓰기 모드 (w+)로 열지 않고 있다는 것입니다.

관련 문제