<!DOCTYPE html> <html> <body> <form action="uploadImage.php" method="post" enctype="multipart/form-data"> Select image to upload: <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="Upload Image" name="submit"> <input type="text" value="" id="imagename"> </form> </body> </html>
<?php $target_dir = "uploads/"; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1; $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); // Check if image file is a actual image or fake image if(isset($_POST["submit"])) { $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); if($check !== false) { echo "File is an image - " . $check["mime"] . "."; $uploadOk = 1; } else { echo "File is not an image."; $uploadOk = 0; } } // Check if file already exists if (file_exists($target_file)) { echo "Sorry, file already exists."; $uploadOk = 0; } // Check file size if ($_FILES["fileToUpload"]["size"] > 500000000000) { echo "Sorry, your file is too large."; $uploadOk = 0; } // Allow certain file formats if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) { echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; $uploadOk = 0; } // Check if $uploadOk is set to 0 by an error if ($uploadOk == 0) { echo "Sorry, your file was not uploaded."; // if everything is ok, try to upload file } else { if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded."; //上传成功,和文件名称 } else { echo "Sorry, there was an error uploading your file."; } } ?>
<?php // 允许上传的图片后缀 $allowedExts = array("gif", "jpeg", "jpg", "png"); $temp = explode(".", $_FILES["fileToUpload"]["name"]); echo $_FILES["fileToUpload"]["size"]; $extension = end($temp); // 获取文件后缀名 if ((($_FILES["fileToUpload"]["type"] == "image/gif") || ($_FILES["fileToUpload"]["type"] == "image/jpeg") || ($_FILES["fileToUpload"]["type"] == "image/jpg") || ($_FILES["fileToUpload"]["type"] == "image/pjpeg") || ($_FILES["fileToUpload"]["type"] == "image/x-png") || ($_FILES["fileToUpload"]["type"] == "image/png")) && ($_FILES["fileToUpload"]["size"] < 204800000) // 小于 200 kb && in_array($extension, $allowedExts)) { if ($_FILES["fileToUpload"]["error"] > 0) { echo "错误:: " . $_FILES["fileToUpload"]["error"] . "<br>"; } else { echo "上传文件名: " . $_FILES["fileToUpload"]["name"] . "<br>"; echo "文件类型: " . $_FILES["fileToUpload"]["type"] . "<br>"; echo "文件大小: " . ($_FILES["fileToUpload"]["size"] / 1024) . " kB<br>"; echo "文件临时存储的位置: " . $_FILES["fileToUpload"]["tmp_name"] . "<br>"; // 判断当前目录下的 upload 目录是否存在该文件 // 如果没有 upload 目录,你需要创建它,upload 目录权限为 if (file_exists("uploads/" . $_FILES["fileToUpload"]["name"])) { echo $_FILES["fileToUpload"]["name"] . " 文件已经存在。 "; } else { //$name = $_POST['name']; $time = date("Y_m_d")."_".time() ; $img = basename($_FILES["fileToUpload"]["name"]); $imgfull=$_FILES["fileToUpload"]["name"]; $targetDir = "uploads/"; $extensionName=explode('.',$imgfull); //echo $extensionName[1]; //$file_name = $time."_".$img; $file_name = $time.".".$extensionName[1]; $targetFilePath = $targetDir . $file_name; move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $targetFilePath); echo "rename:".$targetFilePath; // 如果 uploads 目录不存在该文件则将文件上传到 uploads 目录下 不改名 //move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "uploads/" . $_FILES["fileToUpload"]["name"]); // echo "文件存储在: " . "uploads/" . $_FILES["fileToUpload"]["name"]; } } } else { echo "非法的文件格式"; } ?>
标签:FILES,name,fileToUpload,upload,echo,file,PHP From: https://www.cnblogs.com/geovindu/p/17377426.html