上传后无法保留原始文件名
Cant keep original FileName after Upload
我目前正在使用 PHP 创建文件上传系统。我从互联网上获得了帮助,并成功地完成了这个项目。
但问题是上传后 FileName 会变得很复杂。例如:5987fff5b3dd83.21913884。
有没有办法在上传后保留原来的名字。当前位置在 Localhost 但稍后我必须将其上传到我的网站。
index.php
代码
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit" name="submit">UPLOAD</button>
</form>
</body>
</html>
upload.php
代码
<?php
if (isset($_POST['submit'])) {
$file = $_FILES['file'];
$fileName = $file['name'];
$fileTmpName = $file['tmp_name'];
$fileSize = $file['size'];
$fileError = $file['error'];
$fileType = $file['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png', 'pdf');
if (in_array($fileActualExt, $allowed)) {
if ($fileError === 0) {
if ($fileSize < 1000000) {
$fileNameNew = uniqid('', true).".".$fileActualExt;
$fileDestination = 'uploads/'.$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);
echo "OK";
header("Location: ab.html");
} else {
echo "Your file is too big!";
}
} else {
echo "There was an error uploading your file!";
}
} else {
echo "You cannot upload files of this type!";
}
}
请提出解决问题的可能方法。
此致
严酷的班萨尔
评论这一行
$fileNameNew = uniqid('', true).".".$fileActualExt;
评论方式是
// $fileNameNew = uniqid('', true).".".$fileActualExt;
您正在使用将生成新文件名的 uniqid 函数。
然后添加
$fileNameNew = $fileName;
你正在做的是给名字分配随机值
$fileNameNew = uniqid('', true).".".$fileActualExt;
这样做
$fileNameNew = $fileName;
我目前正在使用 PHP 创建文件上传系统。我从互联网上获得了帮助,并成功地完成了这个项目。
但问题是上传后 FileName 会变得很复杂。例如:5987fff5b3dd83.21913884。
有没有办法在上传后保留原来的名字。当前位置在 Localhost 但稍后我必须将其上传到我的网站。
index.php
代码<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit" name="submit">UPLOAD</button>
</form>
</body>
</html>
upload.php
代码<?php
if (isset($_POST['submit'])) {
$file = $_FILES['file'];
$fileName = $file['name'];
$fileTmpName = $file['tmp_name'];
$fileSize = $file['size'];
$fileError = $file['error'];
$fileType = $file['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png', 'pdf');
if (in_array($fileActualExt, $allowed)) {
if ($fileError === 0) {
if ($fileSize < 1000000) {
$fileNameNew = uniqid('', true).".".$fileActualExt;
$fileDestination = 'uploads/'.$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);
echo "OK";
header("Location: ab.html");
} else {
echo "Your file is too big!";
}
} else {
echo "There was an error uploading your file!";
}
} else {
echo "You cannot upload files of this type!";
}
}
请提出解决问题的可能方法。
此致
严酷的班萨尔
评论这一行
$fileNameNew = uniqid('', true).".".$fileActualExt;
评论方式是
// $fileNameNew = uniqid('', true).".".$fileActualExt;
您正在使用将生成新文件名的 uniqid 函数。
然后添加
$fileNameNew = $fileName;
你正在做的是给名字分配随机值
$fileNameNew = uniqid('', true).".".$fileActualExt;
这样做
$fileNameNew = $fileName;