如何更改客户端选择的图片的名称并以不同的名称保存在服务器中
How to change the name of a picture selected by client and save it in Server with a different name
我有以下代码可以将用户通过输入类型=文件发送的图像上传到我的服务器。
我的问题是:如何更改文件的名称?
示例:假设用户上传以下文件:"mypicture.jpg" 我想用我存储的变量名称保存它,比如 $username where $username = John 所以即使是用户选择 "mypicture.jpg" 文件将作为 John.jpg
保存在我的目录 (/var/www/html/images/) 中
<?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>
非常感谢
$temp = explode(".", $_FILES["file"]["name"]);
$newfilename = $username . '.' . end($temp);
move_uploaded_file($_FILES["file"]["tmp_name"], "/var/www/html/images/" . $newfilename);
我有以下代码可以将用户通过输入类型=文件发送的图像上传到我的服务器。
我的问题是:如何更改文件的名称?
示例:假设用户上传以下文件:"mypicture.jpg" 我想用我存储的变量名称保存它,比如 $username where $username = John 所以即使是用户选择 "mypicture.jpg" 文件将作为 John.jpg
保存在我的目录 (/var/www/html/images/) 中<?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>
非常感谢
$temp = explode(".", $_FILES["file"]["name"]);
$newfilename = $username . '.' . end($temp);
move_uploaded_file($_FILES["file"]["tmp_name"], "/var/www/html/images/" . $newfilename);