2016-09-30 71 views
-1

我正在使用HTML表單上傳文件。然後我使用文件數組將它們轉換爲變量,然後我試圖讀取這個變量。我想從txt文件中讀取數據,但是file_get_contents拒絕讀取uaing數組,因此我如何讀取它我的代碼在這裏

我的代碼是在這裏:

if(isset($_POST['upload'])) { 
    $image = $_FILES['sfile']; 
    $contents = file_get_contents($image); 
    $links = explode(',',$contents); 
    echo $links[0]; 
} 

它從下面的表格

<html> 
 
    <head> 
 
    <title> Trial </title> 
 
    </head> 
 
    <body> 
 
    <form align="center" method="post" action="example.php" enctype="multipart/form-data"> 
 
     Upload File Here : <input type="file" name="sfile"><br> 
 
     <input type="submit" name ="upload" value="Upload"> 
 
    </form> 
 
    </body> 
 
</html>

+1

小心顯示代碼的輸入,比如$ _FILES ['sfile']是什麼? 什麼是輸出/錯誤? –

+0

試用 <形式ALIGN = 「中心」 的方法= 「POST」 行動= 「使用example.php」 ENCTYPE = 「多部分/格式數據」> 文件上傳這裏:<輸入類型= 「文件」 NAME = 「sfile」>

+0

輸出說的file_get_contents需要代替陣列 –

回答

1

您應該tmp_name讀它,請參閱下面的代碼:

if (!empty($_FILES['sfile'])) { 
    $sfile = $_FILES['sfile']; 
    if ($sfile['error'] != UPLOAD_ERR_OK) { 
     // output error here 
    } else { 
     $contents = file_get_contents($sfile['tmp_name']); 
     $links = explode(',', $contents); 
     echo $links[0]; 
    } 
} 

$_FILES有這個陣列格式:

$_FILES['myfile']['name'] - the original file name 
$_FILES['myfile']['type'] - the mime type 
$_FILES['myfile']['size'] - the file size 
$_FILES['myfile']['tmp_name'] - temporary filename 
$_FILES['myfile']['error'] - error code 

錯誤代碼,從http://php.net/manual/en/features.file-upload.errors.php

UPLOAD_ERR_OK 值:0;沒有錯誤,文件上傳成功。

UPLOAD_ERR_INI_SIZE值:1;上傳的文件超過php.ini中的 upload_max_filesize指令。

UPLOAD_ERR_FORM_SIZE值:2;上傳的文件超出了HTML表單中指定的 MAX_FILE_SIZE指令。

UPLOAD_ERR_PARTIAL值:3;上傳的文件僅部分上傳了 。

UPLOAD_ERR_NO_FILE值:4;沒有文件上傳。

UPLOAD_ERR_NO_TMP_DIR值:6;缺少臨時文件夾。在PHP 5.0.3中引入了 。

UPLOAD_ERR_CANT_WRITE值:7;無法將文件寫入磁盤。 在PHP 5.1.0中引入。

UPLOAD_ERR_EXTENSION值:8; PHP擴展停止上傳文件 。 PHP不提供確定哪個擴展導致文件上傳停止的方法;使用 phpinfo()檢查已加載的擴展名列表可能會有所幫助。在PHP 5.2.0中引入。

+0

謝謝☺這就是我一直在尋找:) –

相關問題