2012-03-28 84 views
0

我想通過使用time()更改上傳文件的名稱,當前時間,但我似乎無法找出插入新時間名稱的位置?Uploadify - 將臨時名稱更改爲時間並上傳 - PHP

有人可以試試看嗎?

<?php 
if (!empty($_FILES)) { 
    $newName = time(); <-- Should be the new temp name, insted of the uploaded one. 
    $tempFile = $_FILES['Filedata']['tmp_name']; 
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_GET['folder'] . '/'; 
    $targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name']; 

    move_uploaded_file($tempFile,$targetFile); 
} 

echo '1'; 
?> 
+0

'$的TargetFile = str_replace函數( '//', '/',$ TARGETPATH)。 $ _FILES ['Filedata'] ['name']'這一行...改變任何你需要的..'$ _FILES ['Filedata'] ['name']'從這裏改變.. – Red 2012-03-28 09:45:54

回答

1

$_FILES['Filedata']['name']有你的文件名,您可以替換,但首先你需要得到文件的擴展名,如果它是不一樣的所有的時間。

$p = pathinfo($_FILES['Filedata']['name']); 
$newName = time() . "." . $p['extension']; 
$targetFile = str_replace('//','/',$targetPath) . $newName; 
+0

這對我來說很完美,謝謝:) – 2012-03-29 06:13:51

0
// Get the extension of the uploaded file .. 
$ext = end(explode('.', $_FILES['Filedata']['name'])); 
// Set the target location to your filename, plus the extension 
$targetFile = str_replace('//','/',$targetPath) . $newName . '.' . $ext; 
0

的代碼應該是:

<?php 
if (!empty($_FILES)) { 
    $newName = time(); <-- Should be the new temp name, insted of the uploaded one. 
    $tempFile = $_FILES['Filedata']['tmp_name']; 
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_GET['folder'] . '/'; 
    $targetFile = str_replace('//','/',$targetPath) . $newName . '.EXT'; 

    move_uploaded_file($tempFile,$targetFile); 
} 

echo '1'; 
?> 
+0

這工作正常,但我不能得到延長工作壽。我可以去xxx ['name'] ['extension']嗎? – 2012-03-28 10:01:36

0

$_FILES['Filedata']['name']替換與time()