2012-09-30 79 views
0

我正在使用uploadify,但我不太清楚如何編輯php來重命名上傳的文件。Uploadify - 重命名文件

基本上,用戶最多可以上傳4個文件,它們應該被命名爲像1-img-1,1-img-2,1-img-3,1-img-4 - 第一個數字是用戶id(可以通過POST訪問)。

這裏的uploadify PHP腳本:

<?php 
/* 
UploadiFive 
Copyright (c) 2012 Reactive Apps, Ronnie Garcia 
*/ 

// Set the uplaod directory 
$uploadDir = '/img/listing_images/'; 

// Set the allowed file extensions 
$fileTypes = array('jpg', 'jpeg', 'gif', 'png'); // Allowed file extensions 

$verifyToken = md5('unique_salt' . $_POST['timestamp']); 

if (!empty($_FILES) && $_POST['token'] == $verifyToken) { $i++; 
    $tempFile = $_FILES['Filedata']['tmp_name']; 
    $uploadDir = $_SERVER['DOCUMENT_ROOT'] . $uploadDir; 
    $targetFile = $uploadDir . $_FILES['Filedata']['name']; 

    // Validate the filetype 
    $fileParts = pathinfo($_FILES['Filedata']['name']); 
    if (in_array(strtolower($fileParts['extension']), $fileTypes)) { 
    // Save the file 
    move_uploaded_file($tempFile, $targetFile); 
    echo 1; 

} else { 

    // The file type wasn't allowed 
    echo 'Invalid file type.'; 

} 
} 
?> 

只是想知道,如果有人可以幫告訴我我怎麼會重命名上傳的文件?

+0

您的腳本將允許您的服務器完全泄露,特別是如果$ uploadDir位於您的文檔根目錄中。你的「安全」不過是。用戶提供的文件名進行過濾是**不夠**。 –

回答

1

生成一個唯一的密鑰,安全的文件使用該名稱,未經測試的例子:

$fileParts = pathinfo($_FILES['Filedata']['name']); 
$unique_hash = hash_hmac("md5", file_get_contents($_FILES['Filedata']['name']), SALT); 
$targetFile = $uploadDir . $unique_hash . $fileParts['extension']; 
0

只要改變$的TargetFile變量值和移動的申報$ &分配fileParts這樣的:

$fileParts = pathinfo($_FILES['Filedata']['name']); 
$targetFile = $uploadDir . '1-img-' . $i . $fileParts['extension']; 
+1

太棒了。它正在重命名這些文件,儘管它在第一次上傳圖像後似乎停止了。 –

+0

嗯。我猜每個文件都在一個單獨的請求中發佈,所以文件名都被設置爲相同的值($ i始終爲1)。每個文件會覆蓋前一個文件,看起來只有一個文件被上傳。客戶端需要發送一個計數器,然後代碼可能看起來像'$ counter = $ _FILES ['Filedata'] ['counter']; $ targetFile = $ uploadDir。 '1-img-'。 $櫃檯。 $ fileParts ['extension'];' –

+0

Marc B提供了一個很好的觀點,但安全性是一個單獨的話題。 http://bit.ly/SeHa6Z有一些必要的保護您的上傳。 –

0

這裏的原uploadify.php

<?php 
/* 
Uploadify 
Copyright (c) 2012 Reactive Apps, Ronnie Garcia 
Released under the MIT License <http://www.opensource.org/licenses/mit-license.php> 
*/ 

// Define a destination 
$targetFolder = '/uploads'; // Relative to the root 

$verifyToken = md5('unique_salt' . $_POST['timestamp']); 

if (!empty($_FILES) && $_POST['token'] == $verifyToken) { 
$tempFile = $_FILES['Filedata']['tmp_name']; 
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder; 
$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name']; 

// Validate the file type 
$fileTypes = array('jpg','jpeg','gif','png'); // File extensions 
$fileParts = pathinfo($_FILES['Filedata']['name']); 

if (in_array($fileParts['extension'],$fileTypes)) { 
    move_uploaded_file($tempFile,$targetFile); 
    echo '1'; 
} else { 
    echo 'Invalid file type.'; 
} 
} 
?> 

我只是這樣做。

$fileParts = pathinfo($_FILES['Filedata']['name']); 
$targetFile = rtrim($targetPath,'/') . '/' .rand_string(20).'.'.$fileParts['extension']; 

其中rand_string()是一個函數,可以產生隨機的刺激。

這個人每次上傳文件都會生成不同的名稱(隨機)。希望這可以幫助!