2012-12-21 33 views
0

我正在與一個PHP郵件程序,其中模板將選擇與HTML瀏覽按鈕來獲取數據發送郵件。文件數據獲取使用PHP和HTML瀏覽按鈕

我想在一個變量來獲取數據.. 無法獲取數據..

Warning: fopen(filename.txt) [function.fopen]: failed to open stream: No such file or directory in PATH/php_mailer\sendmail.php on line 18 
Cannot open file: filename.txt 

HTML

<form name="addtemplate" id="addtemplate" method='POST' 
     action='' enctype="multipart/form-data"> 
      <table style="padding-left:100px;width:100%" border="0" cellspacing="10" cellpadding="0" id='addtemplate'> 
       <span id='errfrmMsg'></span> 
       <tbody> 
        <tr> 
         <td class="field_text"> 
          Select Template 
         </td> 
         <td> 
          <input name="template_file" type="file" class="template_file" id="template_file" required> 
         </td> 
        </tr> 
        <tr> 
         <td> 
          <input id="group_submit" value="Submit" type="submit" name='group_submit' /> 
         </td> 
        </tr> 
       </tbody> 
      </table> 
    </form> 

PHP代碼

if(isset($_POST['group_submit'])){ 
     if ($_FILES["template_file"]["error"] > 0) { 
      echo "Error: " . $_FILES["template_file"]["error"] . "<br>"; 
     } 
     else { 
      echo $templFile = $_FILES["template_file"]["name"] ; 
      $templFileHandler = fopen($templFile, 'r') or die('Cannot open file: '.$templFile); //open file for writing ('w','r','a')... 
      echo $templFileData = fread($templFileHandler,filesize($templFile)); 
     } 
    } 
+0

我不知道你在問什麼。請澄清你的問題。 –

+0

你確切的問題是什麼? – GBD

+0

是否有錯誤訊息? – looper

回答

3

它沒有w ork,因爲$_FILES['template_file']['name']是瀏覽器發送給服務器的本地文件名;閱讀你需要$_FILES['template_file']['tmp_name']而不是上傳的文件:

echo $templFile = $_FILES["template_file"]["tmp_name"] ; 
echo $templFileData = file_get_contents($templFile); 

我還使用file_get_contents()這有效地取代fopen()fread()fclose()。上面的代碼不檢查失敗對file_get_contents()部分以任何理由,這會:

if (false === ($templFileData = file_get_contents($_FILES["template_file"]["tmp_name"]))) { 
    die("Cannot read from uploaded file"); 
} 
// rest of your code 
echo $templFileData; 
+0

很好.. thanx。它解決了.. –

4

請更換$ _FILES [ 「template_file」] [ 「名稱」]到$ _FILES [ 「template_file」] [「tmp_name」]

相關問題