作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个表单,人们可以在其中上传单张图片。大多数人都从他们的移动设备上传图片。每个拥有 iPhone 的人都将他们的图像上传为“image.png”。我需要让我的表单不拒绝它,只接受同名文件而不删除旧文件或将它们重命名为“imageupload1.png”、“imageupload2.png”甚至 image.png 然后“image-Copy.png”之类的名称。 png”然后是“image-Copy(2).png”等
我想这样的事情可能会奏效:
$filename = $_FILES['myfilename']['name'];
$filename = time().$filename;
或
function renameDuplicates($path, $file)
{
$fileName = pathinfo($path . $file, PATHINFO_FILENAME);
$fileExtension = "." . pathinfo($path . $file, PATHINFO_EXTENSION);
$returnValue = $fileName . $fileExtension;
$copy = 1;
while(file_exists($path . $returnValue))
{
$returnValue = $fileName . '-copy-'. $copy . $fileExtension;
$copy++;
}
return $returnValue;
}
// 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 "Sucessfully Uploaded - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
我只是不知道在哪里插入它。我是一个菜鸟,所以请放轻松,请具体说明。我“认为可能有用”的选项取自其他问题,但它们对我不起作用,很可能我做错了什么所以请不要将其标记为“已经问过”谢谢。抱歉打扰了。
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// 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 "<a href='../pie.html' style='border: 5px solid #3a57af;padding-right:50px;padding-bottom:25px;padding-left:50px;display:inline;font-weight:bold;color:#3a57af';> CLICK HERE TO REGISTER</a>";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
html {
padding-top: 5em;
font-family: trebuchet, sans-serif;
font-size: 24px;
font-weight: bold;
-webkit-font-smoothing: antialiased;
text-align: center;
background: white;
}
body {
padding:0;
margin:0;
text-align:center;
}
div.imageupload {
padding: 5em 5em 5em 5em;
height:100vh;
width:100vh;
text-align:justify
}
<html>
<head>
<title>Image Submission</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="assets/css/image.css">
<style type="text/css"></style>
</head>
<body>
<div class="imageupload">
<p><h1>Step 1</h1><br>
Please submit a recent photograph to be featured on the homepage of this website.<br><br>
We will place your yearbook picture in the lower right hand corner of the image you submit.<br>
<br>This will allow our classmates to see how we look now and how we looked then.<br><br>
Please select an appropriate image for a website of this nature. <br><br>
The image should show you from your head to at least your knees. <br><br>
The image should not be a group picture, please be the only one in the picture.<br><br>
Any pictures that do not meet this criteria will be sent back and will not be posted.<br></p>
<form action="PHPmailer/upload.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">
</form></div>
</body>
</html>
最佳答案
代替您的 $target_file
定义,插入您的 dedup 函数:
$target_file = renameDuplicates($target_dir, basename($_FILES["fileToUpload"]["name"]));
(我没有检查函数是否正常运行,但乍一看应该没问题。)
编辑:函数有一点点损坏。
function renameDuplicates($path, $file) {
$fileName = pathinfo($path . $file, PATHINFO_FILENAME);
$fileExtension = "." . pathinfo($path . $file, PATHINFO_EXTENSION);
$returnValue = $path . $fileName . $fileExtension;
$copy = 1;
while(file_exists($returnValue))
{
$returnValue = $path . $fileName . '-copy-'. $copy . $fileExtension;
$copy++;
}
return $returnValue;
}
检查包含 $returnValue
的行是否有更改。
关于php - 接受上传的同名文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32898982/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!