使用html5和php上传多个文件 [英] Uploading multiple files using html5 and php

查看:126
本文介绍了使用html5和php上传多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML5多重属性设置的文件上传表单。不过,表单仍然只能上传单个文件。我需要在PHP中创建某种循环函数吗?还有另一种方法吗?

这是我的代码...



形式:

 < form action =<?php $ _SERVER ['PHP_SELF ']→> method =postenctype =multipart / form-data> 
< input type =filemultiple =multiplename =file []id =file/>
< input name =submittype =submitvalue =Submit/>
< / form>

php:

 < code $<?php 
if(isset($ _ POST ['submit'])){
foreach($ _ FILES ['newsImage'] as $ file){
if ((($ _FILES [file] [type] ==image / jpeg)
||($ _FILES [file] [type] ==image / pjpeg) ))
{
if($ _FILES [file] [error]> 0)
{
echoReturn Code:。 $ _FILES [file] [error]。 < br />;




$ b if(file_exists(upload /。$ _FILES [file] [name]))
{
echo $ _FILES [file] [name]。 已经存在。 ;


$ $ b $ move_uploaded_file($ _ FILES [file] [tmp_name],
upload /。$ _FILES [file] [名称]);
回声存储在:。 上传/。 $ _FILES [ 文件] [ 名称];





echoInvalid file;
}
}
}
?>


解决方案

  for $ i = 0; $ i< count($ _ FILES ['newsImage'] ['name']); $ i ++){
//句柄上传
}


I have a file upload form set up with the HTML5 multiple attribute.

However, the form still only uploads a single file. Do i need to create some sort of a looping function in the php or is there another way of doing this?

Here's my code...

form:

<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data">        
       <input type="file" multiple="multiple" name="file[]" id="file" />
       <input name="submit" type="submit" value="Submit" />    
</form>

php:

<?php
if(isset($_POST['submit'])) {           
foreach($_FILES['newsImage'] as $file){ 
if ((($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {


    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
}
}
?>

解决方案

for ($i = 0; $i < count($_FILES['newsImage']['name']); $i++) {
    // handle upload
}

这篇关于使用html5和php上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆