用php实现多文件上传、多图片上传的一例代码,学习下php上传多文件的思路与方法
实现:
1,在同级目录下建立upload文件夹
2,保存以下代码为文件upload.php。
实现:
1,在同级目录下建立upload文件夹
2,保存以下代码为文件upload.php。
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <?php
- $uptypes = array(
- //上传文件的ContentType格式
- 'image/jpg', 'image/jpeg', 'image/png', 'image/pjpeg', 'image/gif', 'image/bmp', 'image/x-png', 'application/msword', //doc
- 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', //docx
- 'application/vnd.openxmlformats-officedocument.presentationml.presentation', //pptx
- 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', //xlsx
- 'text/plain');
- /**
- * php多文件、多图片上传
- * by www.fzzixue.com
- */
- $max_file_size = 2000000; //上传文件大小限制, 单位BYTE
- $dir = "upload/"; //上传文件路径
- if ($_SERVER['REQUEST_METHOD'] == 'POST') {
- $file = $_FILES['upfile']['name'];
- foreach ($file as $key => $item) {
- if ($item != '') {
- if (!is_uploaded_file($_FILES['upfile']['tmp_name'][$key])) //是否存在文件
- {
- echo "图片不存在!";
- exit;
- }
- if ($max_file_size < $_FILES['upfile']['size'][$key]) //检查文件大小
- {
- echo "文件太大!";
- exit;
- }
- if (!file_exists($dir)) {
- mkdir($dir);
- }
- $filename = $_FILES['upfile']['tmp_name'][$key];
- $image_size = getimagesize($filename);
- $pinfo = pathinfo($file[$key]);
- $ftype = $pinfo['extension'];
- $destination = $dir . time() . $file[$key];
- if (file_exists($destination) && $overwrite != true) {
- echo "同名文件已经存在了";
- exit;
- }
- if (!move_uploaded_file($filename, $destination)) {
- echo "移动文件出错";
- exit;
- }
- $pinfo = pathinfo($destination);
- $fname = $pinfo['basename'];
- echo " <font color=red>已经成功上传</font><br>文件名: <font color=blue>" . $dir . $fname . "</font><br>";
- echo " 宽度:" . $image_size[0];
- echo " 长度:" . $image_size[1];
- echo "<br> 大小:" . $_FILES['upfile']['size'] . " bytes";
- }
- echo "<br>图片预览:<br>";
- echo "<img src="" . $destination . "" width=" . ($image_size[0] * (1 / 4)) . " height=" . ($image_size[1] * (1 / 4));
- echo " alt="图片预览:\r文件名:" . $destination . "\r上传时间:">";
- echo "<br>";
- }
- }
- ?>
- <form method="post" enctype="multipart/form-data" action="" name="ff" id="ff" >
- <input type="file" name="upfile[]" />
- <input type="file" name="upfile[]" />
- <label>
- <input type="submit" name="submit" id="submit" value="按钮"/>
- </label>
- </form>
"小礼物走一走,来肥宅自学平台支持我"
评论区
共条评论
- 这篇文章还没有收到评论,赶紧来抢沙发吧~
首页上页1下页尾页