douzhan8652 2014-11-27 07:52
浏览 231
已采纳

使用数组进行多文件上传

I have a website where people can upload an image file. Now i would like to allow users to upload multiple image files. I find it a bit confusing to make my functions accept arrays. Here is what I have :

The UI part of it:

<input type="file" id="imagefile" name="imagefile[]" multiple>

The handler function and used for logging to a txt file:

if(getPostPutField('imagefilename',$imagefilename))
$count=1;
foreach ($_FILES['imagefilename']['tmp_name'] as $file)
{
$imagefilename = urldecode($imagefilename);
$uploaded = handleUploadedImageFile($KBID, $_FILES['imagefile'], $imagefilename);
if ($uploaded)
{
$changesForEveryone[] .= "Image File Uploaded: " . $imagefilename."<br>";
$extraMsg .= "<br>Image File Uploaded: " . $imagefilename."<br>";
}
$count++;
}

The part where the file is really uploaded:

function handleUploadedImageFile($KBID,$file,$imagefilename=null)
{
$result = '';      
if(empty($imagefilename))
$imagefilename = $file['name'];
if($file['size'] > 0)
{
$dir = PROBLEMIMAGES."/".sprintf("%05d",$KBID)."/";
$result = getUploadedFile($file['tmp_name'],$dir,$imagefilename);
}
else
{
$result = false;
}
return $result;
}

How do i do this properly?

  • 写回答

1条回答 默认 最新

  • dongweicha6077 2014-11-27 08:19
    关注

    If you have to upload all the files in a single request you can look at this answer here
    but in order to create a better user experience it will be better to upload the files one by one using ajax take a look at the "jQuery File Upload Demo" (you can find there also the server side code in php)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,