php - 在将图像上传到文件夹之前如何更改图像名称?
问题描述
有人可以帮助我只需要在将图像上传到文件夹之前更改图像名称。无论用户在这里输入什么:
<input name="yname" placeholder="Your Name"class="input-group-field" >
文件名将更改为给定名称。html
<form action="upload.php" method="post" enctype="multipart/form-data">
<h3>Select image to upload:<br></br></h3>
<input name="yname" placeholder="Your Name"class="input-group-field" ><br>
<input type="file" class="button" id="fileToUpload" name="fileToUpload">
<input type="submit" class="button" value="Upload Images" name="submit">
</form>
<button class ="button" onclick="window.location.href = 'showimgs.php';">show images</button>
php
<?php
$target_dir = "photos/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
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;
}
}
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 5000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
Echo "<b><button class=button onclick=location.href='images.html'>Click here to go back</button></b>";
?>
我是初学者,这不是我自己的 php 代码。编辑。\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\\\\\\\\\\\\\
解决方案
我不知道您要做什么或为什么要这样做,因为您没有提供有关要更改的内容或原因的完整详细信息,但这里是代码:
<?php
$filename = "YOUR_FILE_NAME";
$target_dir = "photos/";
$target_file = $target_dir . $filename;
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
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;
}
}
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 5000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
Echo "<b><button class=button onclick=location.href='images.html'>Click here to go back</button></b>";
?>
推荐阅读
- docker - Docker Microsoft 包哈希和不匹配
- angular - 如何根据环境替换 Angular 8.3+ 中的 index.html?
- c# - 如何获取父节点子节点的子节点值
- php - 使用 PHP 和 WSDL 下载数据不起作用,但在 SoapUI 中它可以工作
- presto - 尝试在画面中连接 presto
- postgresql - ORDER BY 表达式必须出现在选择列表中
- javascript - asp.net 中 fullCalendar 的 json 问题
- object - 无法将 OBJ MTL 和纹理文件导入 Cinema 4d
- java - 编辑 ObservableList 中的多个元素,并且仅在最后一次更改后触发更改侦听器
- python - 图像到文本解码器