php简单实现批量上传图片的方法

本文实例讲述了php简单实现批量上传图片的方法。分享给大家供大家参考,具体如下:

  1. <?php
  2. function upload_multi($path,$photo,$i){
  3. $uploaddir = './'.$path;//文件存放目录
  4. if(!file_exists($uploaddir))//如果目录不存在就新建
  5. $uploaddir=mkdir($uploaddir);
  6. $piece = explode('.',$photo['name'][$i]);
  7. $uploadfile = $uploaddir . '/'.md5($piece[0]).'.'.$piece[1];
  8. $result = move_uploaded_file($photo['tmp_name'][$i], $uploadfile);
  9. if(!$result){
  10. exit('上传失败');
  11. }
  12. return basename($uploadfile);
  13. }
  14. if($_POST['tijiao']){
  15. extract($_POST);
  16. $i=0;
  17. foreach ($_FILES["pictures"]["error"] as $key => $error) {
  18. if ($error == UPLOAD_ERR_OK) {
  19. upload_multi($email,$_FILES["pictures"],$i);
  20. }
  21. $i++;
  22. }
  23. }
  24. ?>
  25. <script language="javascript">
  26. function go_up(){
  27. document.getElementById('new_up').innerHTML+='<input type="file" name="pictures[]" /><br>';
  28. }
  29. </script>
  30. <form action="index.php" method="post" enctype="multipart/form-data">
  31. //phpfensi.com
  32. <p>多图片上传</p><br>
  33. <input type="file" name="pictures[]"><br>
  34. <div ></div>
  35. <input type="button" "="" name="add_img" value="新增上传" onclick="go_up()"><br>
  36. <input type="submit" value="Send" name="tijiao"><br>
  37. </form>