//此代码可帮助我将图片提交到我的图片文件夹中。但是,由于函数basename,文件保留其原始名称。如何在文件中添加特定名称?喜欢logo.png?
<?php
if(isset($_POST['submit'])) {
//PROCESS FORM DATA
$tmp_file = $_FILES['file_upload']['tmp_name'];
$target_file = basename($_FILES['file_upload']['name']);
$upload_dir = "images";
//Move file
//False als die niet correct is
if (move_uploaded_file($tmp_file, $upload_dir."/".$target_file)) {
$message = "File upload succesfull";
} else {
$error = $_FILES['file_upload']['error'];
$message = $upload_errors[$error];
}
}
?>
Html表格:
<form action="upload_logo.php" enctype="multipart/form-data" method="POST">
Select .png logo to upload:
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
<input type="file" name="file_upload" />
<input type="submit" name="submit" value="Upload" />
</form>
答案 0 :(得分:0)
只需替换此行:
if (move_uploaded_file($tmp_file, $upload_dir."/".$target_file)) {
这一个:
if (move_uploaded_file($tmp_file, $new_file_name)) {
答案 1 :(得分:0)
我使用此代码。
date
它始终动态生成文件的唯一名称。它内部有unique id
和{{1}}。他们每次都会改变。您无需在此手动更改文件名。
答案 2 :(得分:0)
您的代码看起来几乎就在那里 - 只需生成您想要的名称,抓取原始文件扩展名并附加新名称
<?php
if(isset($_POST['submit'])) {
$tmp_file = $_FILES['file_upload']['tmp_name'];
$target_file = 'logo.' . pathinfo( $_FILES['file_upload']['name'], PATHINFO_EXTENSION );
$upload_dir = "images";
if (move_uploaded_file($tmp_file, $upload_dir."/".$target_file)) {
$message = "File upload succesfull";
} else {
$error = $_FILES['file_upload']['error'];
$message = $upload_errors[$error];
}
}
?>