英文:
How i can change the image name given in input box before uploading it to folder?
问题
我只需要在上传到文件夹之前更改图像名称。 无论用户在这里输入什么:
<input name="yname" placeholder="Your Name" class="input-group-field">
图像名称都将更改为给定的名称。
HTML
<form action="upload.php" method="post" enctype="multipart/form-data">
<h3>选择要上传的图像:<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="上传图像" name="submit">
</form>
<button class="button" onclick="window.location.href = 'showimgs.php';">显示图像</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 "文件是图像 - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "文件不是图像。";
$uploadOk = 0;
}
}
if (file_exists($target_file)) {
echo "抱歉,文件已存在。";
$uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 5000000) {
echo "抱歉,您的文件太大。";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "抱歉,只允许 JPG、JPEG、PNG 和 GIF 文件。";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "抱歉,您的文件未上传。";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "文件" . basename( $_FILES["fileToUpload"]["name"]). "已上传。";
} else {
echo "抱歉,上传文件时出错。";
}
}
echo "<b><button class=button onclick=location.href='images.html'>点击这里返回</button></b>";
?>
我是初学者,这不是我自己的 PHP 代码。
英文:
I just need to change the image name before uploading it to folder.
Whatever user type in here:
<input name="yname" placeholder="Your Name"class="input-group-field" >
the image name will be changed to given name.
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>";
?>
I am beginner and this is not my own php code.
答案1
得分: 1
以下是您要翻译的内容:
尝试类似以下方式(它还将确保文件名是唯一的):
$temp = explode(".", $_FILES["file"]["name"]);
$newfilename = round(microtime(true)) . '_' . $_POST['yname'] . '.' . end($temp);
move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $newfilename);
然后,您还可以使用$newfilename
的值将详细信息推送到您的数据库(如果有的话)。
来源:https://stackoverflow.com/questions/18705639/how-to-rename-uploaded-file-before-saving-it-into-a-directory
英文:
Try this something like this (It will also make sure the file name is unique)
$temp = explode(".", $_FILES["file"]["name"]);
$newfilename = round(microtime(true)) . '_' . $_POST['yname']. '.' . end($temp);
move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $newfilename);
You can then also use the $newfilename
value to push the details to your Database (If you have one)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论