php简单实现批量上传图片的方法
本文实例讲述了php简单实现批量上传图片的方法。分享给大家供大家参考,具体如下:
- <?php
- function upload_multi($path,$photo,$i){
- $uploaddir = './'.$path;//文件存放目录
- if(!file_exists($uploaddir))//如果目录不存在就新建
- $uploaddir=mkdir($uploaddir);
- $piece = explode('.',$photo['name'][$i]);
- $uploadfile = $uploaddir . '/'.md5($piece[0]).'.'.$piece[1];
- $result = move_uploaded_file($photo['tmp_name'][$i], $uploadfile);
- if(!$result){
- exit('上传失败');
- }
- return basename($uploadfile);
- }
- if($_POST['tijiao']){
- extract($_POST);
- $i=0;
- foreach ($_FILES["pictures"]["error"] as $key => $error) {
- if ($error == UPLOAD_ERR_OK) {
- upload_multi($email,$_FILES["pictures"],$i);
- }
- $i++;
- }
- }
- ?>
- <script language="javascript">
- function go_up(){
- document.getElementById('new_up').innerHTML+='<input type="file" name="pictures[]" /><br>';
- }
- </script>
- <form action="index.php" method="post" enctype="multipart/form-data">
- //phpfensi.com
- <p>多图片上传</p><br>
- <input type="file" name="pictures[]"><br>
- <div ></div>
- <input type="button" "="" name="add_img" value="新增上传" onclick="go_up()"><br>
- <input type="submit" value="Send" name="tijiao"><br>
- </form>