2014-02-23 153 views
0

我正在編寫上傳一個excel文件的代碼,並在網頁上顯示其全部內容。但是無論何時用戶單擊提交按鈕它顯示錯誤- 「您的文件類型是:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet 文件類型必須是文本(.txt)或msword(.doc)。」如何上傳一個excel文件,並通過php在網頁上顯示其所有行和列php

以下是我的代碼。

<?php 
if(isset($_POST['submit1'])) { 
// $_FILES is the array auto filled when you upload a file and submit a form. 
$userfile_name = $_FILES['file1']['name']; // file name 
$userfile_tmp = $_FILES['file1']['tmp_name']; // actual location 
$userfile_size = $_FILES['file1']['size']; // file size 
$userfile_type = $_FILES['file1']['type']; 
$userfile_error = $_FILES['file1']['error']; // any error!. get from here 

// Content uploading. 
    $file_data = ''; 
if (!empty($userfile_tmp)) 
{ 
    $file_data=base64_encode(@fread(fopen($userfile_tmp,'r'), filesize($userfile_tmp))); 

} 

switch (true) 
     { 
     // Check error if any 
      case ($userfile_error == UPLOAD_ERR_NO_FILE): 
     case empty($file_data): 
     echo 'You must select a document to upload before you can save this page.'; 
     exit; 
     break; 
     case ($userfile_error == UPLOAD_ERR_INI_SIZE): 
     case ($userfile_error == UPLOAD_ERR_FORM_SIZE): 
    echo 'The document you have attempted to upload is too large.'; 
    break; 

    case ($userfile_error == UPLOAD_ERR_PARTIAL): 
    echo 'An error occured while trying to recieve the file. Please try again.'; 
     break; 

     } 

    if(!empty($userfile_tmp)) 
     { 
    // only MS office and text file is accepted. 
     if(!(($userfile_type=="application/msword") || ($userfile_type=="text/plain") ||  ($userfile_type=="application/vnd.ms-excel"))) 
     {echo 'Your File Type is:'. $userfile_type; 
     echo '<br>File type must be text(.txt) or msword(.doc).'; 

     exit; 
     } 
    } 
    echo filesize($userfile_tmp); 
    } 
    ?> 
    <HTML> 
    <HEAD> 
    <TITLE> PHP File Upload Script </TITLE> 

    </HEAD> 
    <BODY> 
    <form name="profile" method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>" target="_self" enctype="multipart/form-data" > 
    <P align ="center"><input type="hidden" name="MAX_FILE_SIZE" value="1000000"> 

<input name="file1" type="file" accept="application/vnd.openxmlformats-  officedocument.spreadsheetml.sheet" /> 

<input type="submit" name="submit1" value="Submit" /> 
</P> 

</form> 

</BODY> 
</HTML> 

請幫忙。 預先感謝您。

回答

0

您需要添加以下你如果狀況以及 因爲你正在檢查application/vnd.ms-excelapplication/msword,但文件是有它的頭不同的

"application/vnd.openxmlformats-officedocument.spreadsheetml.sheetyour"所以,如果你要是喜歡

if(($userfile_type=="application/vnd.openxmlformats-officedocument.spreadsheetml.sheetyour") || ($userfile_type=="application/msword") || ($userfile_type=="text/plain") || 
($userfile_type=="application/vnd.ms-excel")){ 
// code 
} 
添加這裏面

因爲服務器將根據它收到的頭文件解釋文件類型。

如果你想在你的頁面上顯示Excel文件內容,那麼你應該考慮使用PHPExcel。這是非常容易使用。

鏈接:https://phpexcel.codeplex.com/

例子:https://phpexcel.codeplex.com/wikipage?title=Examples&referringTitle=Home

+0

不工作..它給垃圾值 – user2900761

+0

什麼是不工作的。 phpexcel? –

相關問題