2013-06-21 73 views
0

我在項目中使用uploadify,並且我已經將php腳本重命名爲單個文件,但是我不確定如果在一個文件上傳多個文件時如何重複該過程時間?Uploadify重命名多個文件

我的PHP腳本低於...

$targetFolder = '/img/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; 

    $fileParts = pathinfo($_FILES['Filedata']['name']); 
    $unique_hash = hash_hmac("md5", file_get_contents($_FILES['Filedata']['name']), SALT); 
    $targetFile = rtrim($targetPath,'/') . '/' . $unique_hash .'-'.$_POST['userId'].'.'. $fileParts['extension']; 


    #$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.'; 
    } 
} 
+0

使用'foreach'循環 – Perry

+0

Foreach通過什麼? –

回答

0

當你使用AJAX,你可以通過一個只有一個發送請求對每個文件,那麼你不會有使用一個foreach或您發送的形式在一次,並使用下面的代碼。

當你把所有文件一起,你將使用在你的形式是這樣的:​​這樣你就可以的foreach像這樣的文件:

foreach ($_FILES as $file) 
{  
$uploadfile = $uploaddir . basename($file['name']); 
if (!move_uploaded_file($file["tmp_name"], $uploadfile)) 
{ 
    echo set_e('error','Image ['.$i.'] not uploaded',''); 
} 
} 
0

您是否嘗試過做一個以上? Uploadify使用jQuery ajax來處理文件?它可能看起來是同時發生的,但它通過ajax一次處理一個文件。它會多次調用您的upload.php,因爲隊列中有文件。所以,如果你的函數適用於一個文件,那麼剩下的就是這樣。您只需確保每次調用該函數時都會在目錄中編寫一個唯一的文件名,看起來像是這樣。 Uploadify處理循環遍歷每個文件名稱,然後調用您的上傳腳本。

http://www.uploadify.com/demos/

看與Chrome或螢火蟲網絡控制檯並上傳一堆照片與演示。你會看到爲每個文件調用upload.php腳本。

僅供參考, 我曾經使用uploadify,但我更改爲PLUPLOAD http://www.plupload.com/。支持多次上傳,但更好的記錄和更好的API使用更容易。