【发布时间】:2017-08-07 07:44:43
【问题描述】:
我目前正在使用 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!";
}
}
请提出解决问题的可能方法。
问候
严酷的班萨尔
【问题讨论】:
-
原始文件名在 $_FILES 超全局中。请阅读有关上传文件主题的 PHP 手册。 php.net/manual/en/features.file-upload.php
标签: php html file-upload filenames