2017-08-07 115 views
1

我目前使用PHP到创建文件上传系统。我从互联网上获得帮助并成功完成了这个项目。上传后可以保留原文件名

但问题是,上传后文件名会改变一些复杂的东西。例如: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!"; 
} 
} 

请提出可能的方式来解决这个问题。

问候

恶劣邦萨尔

+2

原来的文件名是在$ _FILES超级全局。请阅读关于上传文件主题的PHP手册。 http://php.net/manual/en/features.file-upload.php – GordonM

回答

0

你正在做的是分配随机值命名

$fileNameNew = uniqid('', true).".".$fileActualExt; 

做到这一点

$fileNameNew = $fileName; 
+0

非常感谢你,它运作良好 – user8427007

0

评论这条线

$fileNameNew = uniqid('', true).".".$fileActualExt; 

如何评论是

// $fileNameNew = uniqid('', true).".".$fileActualExt; 

您使用uniqid功能,这将产生新的文件名。

再加入

$fileNameNew = $fileName;