2017-04-13 25 views
0

我在PHP下面的代碼:在php文件發佈不同的文件類型,以不同的PHP塊

if (isset($_FILES)) { 
    foreach($_FILES as $index => $picture) 
    { 
    echo $_FILES; 
    } 
    } 

JavaScript代碼來發布表單數據:

$(".update").click(function(){ 

    $.ajax({ 
    url: 'submit.php', 
    type: 'POST', 
    contentType:false, 
    processData: false, 
    data: function(){ 
     var data = new FormData(); 
     jQuery.each(jQuery('#file')[0].files, function(i, file) { 
      data.append('file-'+i, file); 
     }); 
     data.append('body' , $('#body').val()); 
     return data; 
     }(), 
     success: function(result) { 
     alert(result); 
     }, 
     error: function(xhr, result, errorThrown){ 
     alert('Request failed.'); 
     } 
     }); 
     $('#picture').val(''); 
     $('#body').val(''); 
     }); 

HTML表單

<form enctype="multipart/form-data" method="post" id="myform"> 
<textarea name="body" id="body" class="texarea" placeholder="type here"> 
</textarea> 

<label for="photo" class="custom-file-upload"> 
<i class="fa fa-picture-o"style="font-size:20px;color:pirple;"></i> 
</label> 
<input id="file" name="picture[]" type="file" multiple/> 
//this is my part of interest now 
<label for="docs" class="docsmsg"> 
<i class="fa fa-file" style="font-size:20px;color:red;"></i> 
</label> 
<input id="docs" name="document[]" type="file" multiple/> 
</form> 

PHP代碼沒有收到具有特定名稱的文件屬性的形式,但任何文件無論是照片,文檔或不同的名稱。 if(isset($ _ FILES))

但是,這不是我想要的。在我的表單中,我需要兩個甚至三個文件輸入選項,就像上面的選項一樣,除了另一個選項僅用於上載文檔,最後一個僅用於上傳視頻...請注意放置註釋的位置...說......「這是我感興趣的部分。」這是我想添加的新增功能。

目前只能上傳照片......但我對三個文件輸入選項感興趣......一個用於照片,視頻和文檔。

起初我試圖區分使用這個PHP代碼不同:

if (isset($_FILES[file])) { 
    //do something 
    } 

,但沒有奏效。它沒有做'東西'。直到我在方括號中刪除[文件]描述的最後,它纔開始執行上面提到的「某事」。

我的問題確實是基於我想要達到的....這是有不同類型的文件單獨多個文件輸入,我如何讓PHP實際確定哪些表單輸入字段的文件正在連接,或者我想要處理哪些文件類型。 現在的代碼,哪個輸入字段文件正在附加,但最後,如果(isset($ _ FILES))指令都來到那個沒關係......

+0

''_FILES [file]'' - 變量文件必須包含$ _FILES數組的有效索引。要測試一個特定的文件類型,你必須得到文件名,並測試它的擴展名或MIME類型。一般來說,在網絡上做這些事情有很多例子,特別是。 –

+0

看看這篇文章:http://php.net/manual/en/features.file-upload.post-method.php –

+0

這個鏈接可能不適合我....記住,我想要得到具有特定名稱的多個文件被髮送到php文件....多個文件來自一個輸入字段而不是多個輸入字段.......兩個多個輸入字段用於具有不同名稱的兩種不同類型的文件。 ..每個字段必須一次接受該文件類型的多個上傳。 – maraj

回答

2

Hello_ mate,

首先我在這裏看到一個錯誤if (isset($_FILES[file]))這將永遠不會工作,因爲你需要像這樣的報價if (isset($_FILES['file']))。然後,當你POST表單,數據與name屬性,而不是id到來,所以,如果你有這樣的HTML

<input id="file" name="picture[]" type="file" multiple/> 
<input id="docs" name="document[]" type="file" multiple/> 

PHP,你會怎麼做:

if(isset($_FILES['picture'])){ 
    foreach($_FILES['picture'] as $fileData){ 
     // process your picture files here 
    } 
} 

if(isset($_FILES['document'])){ 
    foreach($_FILES['document'] as $fileData){ 
     // process your document files here 
    } 
} 

不是

if(isset($_FILES['file'])){ 
} 

if(isset($_FILES['docs'])){ 
} 

我希望你明白這一點。祝你好運:)

注意

有想法,你應該檢查type屬性時手動驗證文件類型。當您選擇了多個文件,你會在下面的結構中接收數據:

[picture] => Array 
(
    [name] => Array 
     (
      [0] => icon.ico 
      [1] => some-png.png 
     ) 

    [type] => Array 
     (
      [0] => image/x-icon 
      [1] => image/png 
     ) 

    [tmp_name] => Array 
     (
      [0] => \tmp\php3F40.tmp 
      [1] => \tmp\php3F41.tmp 
     ) 

    [error] => Array 
     (
      [0] => 0 
      [1] => 0 
     ) 

    [size] => Array 
     (
      [0] => 370070 
      [1] => 370070 
     ) 

) 

所以,你可以舉例來說做這樣的事情:

$imgsAllowedTypes = array('image/jpeg', 'image/png', 'image/gif', 'image/bmp'); 

if(isset($_FILES['picture'])){ 
    // This is an array of uploaded files 
    $pictures = $_FILES['picture']; 
    $fileNames = $pictures['name']; 
    foreach($fileNames as $k => $fileName){ 
     // Here you can access other properties of the file by index - $k 
     $fileType = $pictures['type'][$k]; 
     $tmpName = $pictures['tmp_name'][$k]; 
     $error = $pictures['error'][$k]; 
     $size = $pictures['size'][$k]; 
     if(!in_array($fileType, $imgsAllowedTypes)){ 
      // Invalid image 
      continue; 
     } 

     // upload 
     move_uploaded_file($tmpName, 'path/where/you/save/files/filename.extension'); 
    } 
} 

編輯

現在沒事了感謝你的評論我可以承認,我沒有檢查過你的javascript代碼,當我沒有,我可以說這是錯誤的,如果你想我寫的代碼片段工作,你應該有以下的JavaScript:

$.ajax({ 
    type: 'POST', 
    cache: false, 
    contentType: false, 
    processData: false, 
    data: function() { 
     // Note the change is here 
     // You should have the same names set in the FormData object 
     // as you are waiting in the PHP script 
     var data = new FormData(); 
     $.each($('#file')[0].files, function (i, file) { 
      data.append('picture[' + i + ']', file); 
     }); 

     $.each($('#docs')[0].files, function (i, file) { 
      data.append('document[' + i + ']', file); 
     }); 
     return data; 
    }(), 
    success: function (result) { 
     alert(result); 
    }, 
    error: function (xhr, result, errorThrown) { 
     alert('Request failed.'); 
    } 
}); 

詳細內容請閱讀

+0

只是一個小小的提示--PHP實際上會將文件和文檔數組索引轉換爲字符串並觸發警告 - 或者至少它在第5版中表現得很好,所以代碼實際上可以工作(儘管有警告)。這絕對是不好的做法,不幸的是仍然支持PHP 7. – Shane

+0

閱讀你的解決方案後,我嘗試了下面的代碼,但仍然沒有運氣:php <?如果(isset($ _ FILES ['picture'])){$ _FILES ['picture'] as fileData){ echo $ fileData; } } ?>和html表格

現在有什麼錯誤?該文件不回顯。我也沒有改變上面的JavaScript,因爲你沒有確定一個問題。 – maraj

+0

@maraj嘗試'echo'

'.print_r($fileData, true).'
';''而不是'echo $ fileData'你不能直接'echo'數組' – codtex

0

正如前光標的評論,我相信將contentType必須設置爲在AJAX請求「的multipart/form-data的」爲好。

現在,轉到您關於PHP方面的問題。這實際上取決於您想如何構建上傳代碼,以確定如何編寫代碼 - 顯然,這很明顯。

所以這就是我會做的。

假設我們有一個要上傳文件的端點 - 我看你沒有在表單中給出一個,所以爲簡單起見,我將其命名爲'upload.php'。

我有3種類型的字段。圖片,文件和視頻。 HTML標記將非常簡單。

<form method="POST" enctype="multipart/form-data"> 
    <input type="file" name="picture[]" /> <!-- 1 or more --> 
    <input type="file" name="video[]" /> <!-- 1 or more --> 
    <input type="file" name="document[]" /> <!-- 1 or more --> 
</form> 

當然,你會想擴大這個非常基本的形式。每個輸入字段可以複製更多的上傳字段。

現在,到PHP。我會省略JavaScript,因爲看起來你已經掌握了這一點。

<?php 
    if (empty($_FILES)) { 
     // We could output an error here if we like... 
     // Let's just end execution. 
     die(); 
    } 

    // Will we process any pictures? 
    if (!empty($_FILES['picture'])) { 
     // Yes, we are processing a picture upload. 
     // _FILES[picture] is an array of arrays, let us iterate! 
     foreach ($_FILES['picture'] as $picture) { 
      // In here, we process each picture. 

      // Make sure the upload is ok 
      if ($picture['error'] !== UPLOAD_ERR_OK) { 
       // Process the error 
      } 

      // Before we save this, we should perform some MIME sniffing... 
      // I'll leave the implementation to you, just DON'T use picture['type'], as it is unreliable 
      $verified = false; 
      if (!$verified) { 
       // Note the error then break to continue processing other photos. 
       break; 
      } 

      // We should move the file to our uploads directory 
      move_uploaded_file($picture['tmp_name'], 'OUR_UPLOADS_DIR/myfile.jpg'); 

      // Maybe here you can note a successful upload? 
     } 
    } 

    // How about some documents? 
    if (!empty($_FILES['document'])) { 
     foreach ($_FILES['document'] as $doc) { 
      // Here, we should basically do the same as with the picture. 
      // - Check for an error 
      // - Sniff the MIME type to ensure it's a safe file 
      // - Move the file 
     } 
    } 

    // Finally, we can do the same for the videos 
    if (!empty($_FILES['video'])) { 
     // You get the idea :) 
    } 

    // Now, we should actually output a response to the AJAX request, 
    // so we may notify the client of what actually happened. 
    // Success, or failure? I'll leave this up to you, to choose how 
    // best to communicate back to the client. 

正如您所看到的,在PHP中處理文件可能非常簡單。但要做到這一點,確實需要一些適當的基礎。由於很多代碼都非常相似,所以創建輔助函數可能會比較有用,而不是輸入相同的代碼3次。

+0

出於某種原因,當我嘗試此解決方案時,點擊提交按鈕後,頁面刷新。我沒有看到基於我作爲問題一部分發布的javascript代碼彈出。怎麼了? – maraj

+0

我認爲你能夠使Javascript的工作,因爲你似乎有處理。您應該將JavaScript附加到表單上的提交事件。 – Shane

相關問題