2017-04-11 89 views
0

我有一個單一的圖像上傳字段,我試圖得到它時,它上傳重命名上傳的圖片

重命名的形象,如果用戶上傳的圖像稱爲picture.jpg

將其重命名爲O1CCJDSXBOM2 .JPG

$file = $_FILES['photo']['name']; 
$file_loc = $_FILES['photo']['tmp_name']; 
$file_size = $_FILES['photo']['size']; 
$folder = "../uploads/"; 
+0

$ target_dir =「..uploads /」; $ target_file = $ target_dir。 「O1CCJDSXBOM2.jpg」; $ imageFileType = pathinfo($ target_file,PATHINFO_EXTENSION); move_uploaded_file($ _ FILES [「fileToUpload」] [「tmp_name」],$ target_file) – JYoThI

+0

以及...請閱讀[w3schools關於文件上傳的教程](https://www.w3schools.com/php/php_file_upload.asp )並閱讀[move_uploaded_file](https://www.w3schools.com/php/func_filesystem_move_uploaded_file.asp)? –

+0

一切工作正常,它上傳我只需要重命名它 – Case

回答

0

在上傳功能,您可以重命名這樣

$target_dir = "..uploads/"; 
$target_file = $target_dir."O1CCJDSXBOM2.jpg"; 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file) 

move_uploaded_file(第一個參數是你上傳的文件,第二放慢參數是要移動與文件名ex: uploads/O1CCJDSXBOM2.jpg文件)

安全提示:

1)檢查圖像文件是實際圖像或僞像

2)檢查文件已經存在

3)檢查文件大小

4)允許某些文件格式

5)檢查文件大小

0

假設要重命名與原來的名字上傳的文件,嘗試這樣的事情:

$tmp_filename = realpath($_FILES['upload']['tmp_name']); 
if ($_FILES['upload']['error'] > 0) { 
    echo "Error: " . $_FILES['upload']['tmp_name'] . "<br />" . $_FILES['upload']['error'][$i]; 
} else { 
    $new_filename = realpath("../uploads") . DIRECTORY_SEPARATOR . $_FILES['upload']['name']; 

    if (move_uploaded_file($tmp_filename, $new_filename)) { 
     echo "file moved to " . $new_filename; 
    } else { 
     echo "error moving uploaded file."; 
    } 
} 

更多的信息,特別是安全見move_uploaded_fileHandling file uploads