我有以下代码上传到我的服务器的图像,而不是用户通过输入type = file发送的图像。
我的问题是:如何更改文件名?
示例:让我们说比用户上传以下文件:" mypicture.jpg"我想用我已存储的变量的名称保存它,比如$ username其中$ username = John所以即使是用户选择" mypicture.jpg"该文件将保存在我的目录(/ var / www / html / images /)中,如John.jpg
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$uploaddir = '/var/www/html/images/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
pg_close();
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="3000000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
非常感谢
答案 0 :(得分:1)
$temp = explode(".", $_FILES["file"]["name"]);
$newfilename = $username . '.' . end($temp);
move_uploaded_file($_FILES["file"]["tmp_name"], "/var/www/html/images/" . $newfilename);