2017-08-07 113 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;