2015-10-27 70 views
0

任何人都可以幫助我解決這個文件上傳在php中的問題。我在線獲取未定義的索引!代碼分爲兩個獨立的文件。我嘗試了幾個代碼,仍然沒有工作。請幫忙。謝謝大家。 HTML文件上傳文件在PHP不工作

<form role="form" name="uploadPro" id="uploadPro" method="post" action="upload.php" enctype="multipart/form-data"> 
    <div class="col-lg-6"> 
     <div class="form-group"> 
       <label>Product Tags</label> 
       <textarea class="form-control" rows="2" name="pTags" id="PTags"></textarea> 
       <p class="help-block">Add Tags to your products to enable faster search.</p> 
      </div> 
      <div class="form-group"> 
       <label>Product Expiry Date</label> 
       <input type="text" name="pExpDate" id="datepicker" class="form-control"> 
       <p class="help-block">Until which date will product be displayed.</p> 
      </div> 
      <div class="form-group"> 
       <label>Upload Main Image</label> 
       <input type="file" name="mainPath" id="mainPath"> 
       <p class="help-block">This image will appear everywhere. Make sure it has a good quality to impress users and size greater than 400px X 400px. </p> 
      </div> 
      <div class="form-group"> 
       <label>Upload Sub Image 1</label> 
       <input type="file" name="sub1" id="sub1"> 
       <p class="help-block">Make sure image has a good quality to impress users and size greater than 400px X 400px. </p> 
      </div> 
      <div class="form-group"> 
       <label>Upload Sub Image 2</label> 
       <input type="file" name="sub2" id="sub2"> 
       <p class="help-block">Make sure image has a good quality to impress users and size greater than 400px X 400px. </p> 
      </div> 
      <div class="form-group"> 
       <label>Upload Sub Image 3</label> 
       <input type="file" name="sub3" id="sub3"> 
       <p class="help-block">Make sure image has a good quality to impress users and size greater than 400px X 400px. </p> 
      </div> 

      <input type="submit" value="Upload Image" name="submit" class="btn btn-default"> 
    </div> 

</form> 

PHP代碼

if(isset($_POST['submit'])){ 

    /*Uploading main image*/ 
    $filetmp = $_FILES["mainPath"]["tmp_name"];//getting error here 
    $filename = $_FILES["mainPath"]["name"];//getting error here 
    $filepath = "../uploads/".$filename; 
    move_uploaded_file($filetmp, $filepath); 

    /*Uploading sub image1*/ 
    $filetmp1 = $_FILES["sub1"]["tmp_name"];//getting error here 
    $filename1 = $_FILES["sub1"]["name"];//getting error here 
    $filepath1 = "../uploads/".$filename1; 
    move_uploaded_file($filetmp1, $filepath1); 

    /*Uploading sub image2*/ 
    $filetmp2 = $_FILES["sub2"]["tmp_name"];//getting error here 
    $filename2 = $_FILES["sub2"]["name"];//getting error here 
    $filepath2 = "../uploads/".$filename2; 
    move_uploaded_file($filetmp2, $filepath2); 

    /*Uploading sub image3*/ 
    $filetmp3 = $_FILES["sub3"]["tmp_name"];//getting error here 
    $filename3 = $_FILES["sub3"]["name"];//getting error here 
    $filepath3 = "../uploads/".$filename3; 
    move_uploaded_file($filetmp3, $filepath3); 
} 
+1

你得到什麼錯誤? –

+0

你在哪一行發生什麼錯誤? –

+0

請參閱我的PHP代碼。我添加的註釋//得到錯誤這裏 – Student

回答

0

首先,確保PHP被配置爲允許文件上傳。

在您 「的php.ini」 文件,搜索php.ini的file_uploads指令,並將其設置爲開:

file_uploads = On 

Error Messages Explained

PHP返回一個適當的錯誤代碼與文件一起陣列。錯誤代碼可以在PHP文件上傳過程中創建的文件數組的錯誤段中找到。換句話說,錯誤可能在$ _FILES ['userfile'] ['error']中找到。

+0

它的工作。我首先嚐試了一個基本示例,正確地上傳文件。但是這個不起作用。試圖找到解決方案,因爲2天! 感謝您的回覆:) – Student

+0

您可以解釋您所做的更改,以便其他遇到類似問題的用戶可以輕鬆修復。 – Thamilan

+0

我的意思是我嘗試上傳正確的第一個例子(意思是file_uploads已打開)。這一個不工作。 – Student

0

首先使用print_r($_FILES)來檢查r值是否發佈。

用於循環,而不是訪問索引,

if(isset($_POST['submit'])){ 

foreach ($_FILES as $row) { 
    $filetmp = $row['tmp_name']; //getting error here 
    $filename = $row["name"]; //getting error here 
    $filepath = "../uploads/" . $filename; 
    move_uploaded_file($filetmp, $filepath); 
} 
+0

我已經在我的PHP代碼中使用過這一個:if(isset($ _ FILES ['image'])){...} 什麼都沒有發生..錯誤在哪裏? – Student

+0

上傳目錄是否正確? –

+0

是的,目錄是正確的。 :) – Student

0

我可以檢查你的代碼在我的測試文件和工作文件,可能的問題是你的「的php.ini」文件。請給我你的錯誤,以便我可以識別。

+0

我改變了(isset($ _ POST ['submit']) if(isset($ _ FILES ['mainPath'])){...} 我得到一個沒有執行任何內容的空白頁面,即(isset($ _ FILES ['image']))沒有發生。你得到我的觀點 – Student

+0

@Student你檢查你的文件夾「上傳」圖像上傳與否? – Jalpa

0

我沒有改變任何東西。我重新啓動了我的Wamp服務器並在隱身窗口中運行該頁面。它的工作現在。 謝謝大家的幫助。這讓我很高興看到你們如何互相幫助。