2017-04-13 52 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