2009-11-05 69 views
2

例如,我有一個變量「$ foo」的,包括所有我想在CSV文件中顯示的數據:如何使用PHP創建一個CSV文件(上傳)

$foo = "some value,another value,last value"; 

我的目標就是:

  1. 創建一個名爲 「some.csv」,其內容等於$ foo的

  2. 上傳 「some.csv」 我的服務器CSV文件。

這怎麼辦?

更新:下面是我的確切代碼。

$foo = "some value,another value,last value"; 
$file = 'some_data.csv'; 
file_put_contents($file, $foo); 
+0

所以youre在您的家裏跑PHP和你想要把它上傳到你的服務器? – Galen 2009-11-05 15:55:47

+0

不,我正在將我的php腳本上傳到服務器,該服務器將由時間作業運行。該腳本應創建一個csv文件並將其添加到同一個主機空間/服務器上的一個目錄中。 – edt 2009-11-06 12:31:13

回答

2

數1:

file_put_contents("foobar.csv", $yourString); 

數2:

$c = curl_init("http://"...); 
curl_setopt($c, CURLOPT_POSTFIELDS, array('somefile' => "@foobar.csv")); 
$result = curl_exec($c); 
curl_close($c); 
print_r($result); 

注意文件名

+0

不要忘記檢查以確保cURL擴展已安裝。 – 2009-11-05 16:24:03

2

fputcsv()

如果$ foo的已經CSV格式。您可以使用file_put_contents()

您不指定上傳方法。下面是一個例子使用FTP(不安全):

$foo = '...csv data...'; 
$username = "myUser"; 
$password = "myPassword"; 
$url = "myserver.com/file.csv"; 
$hostname= "ftp://$username:[email protected]$url"; 
file_put_contents($hostname, $foo); 
0

要創建CSV,您需要將您的字符串分解爲數組,然後遍歷它。之後,您可以將該文件保存到Web服務器帳戶可以在服務器上訪問的任何目錄。下面是一個例子...

//variables for the CSV file 
$directory = '/sampledir/'; 
$file = 'samplefile.csv'; 
$filepath = $directory.$file; 

//open the file 
$fp = fopen("$filepath",'w+'); 

//create the array 
$foo = "some value,another value,last value"; 
$arrFoo = explode(',',$foo); 

//loop through the array and write to the file 
$buffer = ''; 
foreach($arrFoo AS $value) { 
    $buffer .= $value."\r\n"; 
} 
fwrite($fp,$buffer); 

//close the file 
fclose($fp); 

您的文件現在將寫入到$directory設置在$file設置文件名的目錄。

-Justin

1

如何使用PHP(工作守則

查詢庫

<?php 
class query{ 

function mysql_query_string($string){ 
    $enabled = true; 
    $htmlspecialchars = false; # Convert special characters to HTML entities 
    /**************************************************************** 
    The translations performed are: 

    '&' (ampersand) becomes '&amp;' 
    '"' (double quote) becomes '&quot;' when ENT_NOQUOTES is not set. 
    ''' (single quote) becomes '&#039;' only when ENT_QUOTES is set. 
    '<' (less than) becomes '&lt;' 
    '>' (greater than) becomes '&gt;' 

    *****************************************************************/ 

    if($htmlspecialchars){ 
    # Convert special characters to HTML entities 
    $string = htmlspecialchars($string, ENT_QUOTES); 
    } 
    else{ 
    /**************************************************************** 
    '"' (double quote) becomes '&quot;' 
    ''' (single quote) becomes '&#039;' 
    ****************************************************************/ 
    //$string = str_replace('"',"&quot;",$string); 
    //$string = str_replace("'","&#039;",$string); 
    } 

    if($enabled and gettype($string) == "string"){ 
     # Escapes special characters in a string for use in a SQL statement 
     return mysql_real_escape_string(trim($string)); 
    } 
    elseif($enabled and gettype($string) == "array"){ 
    $ary_to_return = array(); 
    foreach($string as $str){ 
     $ary_to_return[]=mysql_real_escape_string(trim($str)); 
    } 
     return $ary_to_return; 
    } 
    else{ 
     return trim($string); 
    } 
    } 
} 
?> 

呼叫上傳CSV文件伏安法

public function csvFileSubmitData(){ 

    $this->load->library('query'); 
    $query=new query(); 
    $root = DIR_PATH.'public/administrator/csv/'; 

    $fileToUpload= (isset($_FILES['fileToUpload']) and $_FILES['fileToUpload']['size'] > 0 and 
    $_FILES['fileToUpload']['error'] == 0) ? $_FILES['fileToUpload'] : ""; 

     if(is_array($fileToUpload)){ # CHECK UPLOADED FILE 1 FOR VALIDATION 
      $fileToUpload['name'] = str_replace(" ","_",$fileToUpload['name']); 
      $fileToUpload['name'] = str_replace("&","and",$fileToUpload['name']); 
      # CHECK FILE TYPE IF IT IS IMAGE JPG,GIF,PNG ETC 
      $fnarr = explode(".", $fileToUpload['name']); 
     } 

    $rand = rand(1000,10000); 
    $filecsv = $rand."_".$fileToUpload['name']; 
    $file1 = $root.$filecsv; 
    move_uploaded_file($fileToUpload['tmp_name'],$file1); 

    $fieldseparator = ","; 
    $lineseparator = "\n"; 
    $csvfile = $file1; 
    $addauto = 0; 
    $save = 0; 
    $outputfile = "output.sql"; 
    if(!file_exists($csvfile)) { 
     echo "File not found. Make sure you specified the correct path.\n"; 
     exit; 
    } 
    $file = fopen($csvfile,"r"); 

    if(!$file) { 
     echo "Error opening data file.\n"; 
     exit; 
    } 

    $size = filesize($csvfile); 

    if(!$size) { 
     echo "File is empty.\n"; 
     exit; 
    } 

    $csvcontent = fread($file,$size); 

    fclose($file); 

    $lines = 1; 
    $queries = ""; 
    $linearray = array(); 
    $values = ""; 
    $m =0; 
    $linestext = split($lineseparator,$csvcontent); 

    foreach($linestext as $line){ 
    if($m++==0){ 
     continue; 
    } 

    $lines++; 
    $line = trim($line," \t"); 
    if($line == ''){ 
     break; 
    } 
    $linearray = explode($fieldseparator,$line); 

    $topicname = $linearray[0]; 
    $question = $linearray[1]; 
    $answer1 = $linearray[2]; 

    if(isset($linearray[1]) and $linearray[1] != ''){ 

       $topicname = $query->mysql_query_string($linearray[0]); 
       $question = $query->mysql_query_string($linearray[1]); 
       $answer_type = $query->mysql_query_string($linearray[2]); 
    } 
    //Save Csv data in your table like this 
    //query(insert into topics SET `topic`='".$topicname."',`question`='".$question."'); 
    }} 

如果您正在使用Codeignitor Framework,所以這段代碼太容易了整合,沒有硬&快速規則,你也可以使用這個代碼普通的PHP以及.....

感謝名單 AbdulSamad