我需要有上传文件的网络表单。我希望用原始名称保存上传的文件。
我有这个PHP代码:
<form action="" enctype="multipart/form-data" method="POST">
<input type="file" name="file" /> <br />
<input type="submit" value="Upload" />
</form>
<?php
if ( ($_SERVER['REQUEST_METHOD'] == 'POST') ) {
$fieldname = $_REQUEST['file'];
$tmpfile = $_FILES[$fieldname]['tmp_name'];
$info = pathinfo($tmpfile);
$filename = 'newfile.' . $info['extension'];
$uploaddir = 'uploads/';
$uploadfile = $uploaddir . $filename;
if(move_uploaded_file($tmpfile, $uploadfile)) {
echo $uploadfile;
}
}
?>
但在用户上传文件后,我在服务器上有tmp_name
个文件。
我需要的是:用户上传文件photo123.jpg
- 我希望服务器上有photo123.jpg
。用户上传firefox.exe
- 我想要firefox.exe
答案 0 :(得分:1)
试试这个:
$fieldname = $_FILES['file']['name'];
而不是$fieldname = $_REQUEST['file'];
答案 1 :(得分:1)
尝试使用$_FILES['file']['name']
if ( ($_SERVER['REQUEST_METHOD'] == 'POST') ) {
$tmpfile = $_FILES['file']['tmp_name'];
$filename = $_FILES['file']['name'];
$uploaddir = 'uploads/';
$uploadfile = $uploaddir . $filename;
if(move_uploaded_file($tmpfile, $uploadfile)) {
echo $uploadfile;
}
}