2012-03-07 82 views
1

我需要爲上傳的文件添加更多的文件數。我使用SFWUpload。我有這樣的代碼:上傳時重命名文件 - PHP

mkdir("../../imagenes/".$codigo , 0777); 
$i = 1; 
$nombre = $codigo.'_'.$i.'.jpg'; 
move_uploaded_file($_FILES['Filedata']['tmp_name'], "../../imagenes/".$codigo."/".$nombre); 
chmod("../../imagenes/".$codigo."/".$_FILES['Filedata']['name'], 0777); 

的$ codigo是代碼,例如101213,所以我需要的圖片,像101213_1.jpg,101213_2.jpg,101213_3.jpg等上傳。

問題是,SWFUpload運行每個圖片的php ONCE,所以我不能使用foreach循環(我猜)。

我需要腳本來檢查文件是否存在並寫入下一個。例如,如果存在101213_4.jpg,則編寫101213_5.jpg。

你能幫我嗎我該怎麼做。我是PHP的新手,並嘗試了一切。 :(

在此先感謝

羅伯託

+0

向我們展示你的「一切」的嘗試,那麼,我們可以從那裏開始:) – 2012-03-07 02:31:43

+0

保存在某個地方的數量和使用人數 – undone 2012-03-07 02:32:16

+0

的「everithing」是算文件,檢查如果文件存在,甚至使用會話,但沒有任何工作。 :( – 2012-03-07 03:02:13

回答

0

那麼...你可以嘗試獲取文件夾中的當前文件數並從那裏獲得$i

mkdir("../../imagenes/".$codigo , 0777); 
$i = count(glob("../../imagenes/".$codigo.'_*.jpg')) + 1; 
$nombre = $codigo.'_'.$i.'.jpg'; 
move_uploaded_file($_FILES['Filedata']['tmp_name'], "../../imagenes/".$codigo."/".$nombre); 
chmod("../../imagenes/".$codigo."/".$nombre, 0777); 

嘗試代碼...

+0

這工作。謝謝。!!我對這行代碼做了這個改變。 $ i = count(glob(「../../ imagenes /」。codigo。「/ *。jpg」))+ 1; 謝謝。 ;) – 2012-03-07 03:13:57

+0

當兩個人同時嘗試上傳時會發生什麼? – 2012-03-07 17:50:12

+0

我想到了......但是如何避免PHP進程之間的競爭條件?也許使用信號量? sem_get,sem_acquire等 – eagleoneraptor 2012-03-07 19:52:59

1

下面是我用一個函數:

function cleanup_name($name){ //accepts name and cleans it up. 
$finalDir='/home/username/uploads'; 

# Go to all lower case for consistency 
$name = strtolower($name); 
//echo("Name is $name<br>"); 

$matches=split('\.',$name); 
foreach ($matches as $key=>$value){ 
$exkey=$key; 
$exvalue=$value; //if there is more than one period, this will find the actual extension. 
//echo("Key $key|$exkey Value $value|$exvalue<br>"); 
} 

if ($exkey<1){die('The file must have an extension.');} 
$extension=".".$exvalue; 
$loop=0; 
while ($loop<($exkey)){ 
if ($loop<($exkey-1)){$matches[$loop]=".".$matches[$loop];} // this puts extra periods back into the string, but the borrowed code will replace them with underscores. 
$stem.=$matches[$loop]; 
$loop++; 
} 

//echo("Stem is $stem<br>"); 
//echo("Extension is $extension<br>"); 

# Convert whitespace of any kind to single underscores 
$stem = preg_replace('/\s+/', '_', $stem); 

# Remove any remaining characters other than A-Z, a-z, 0-9 and _ 
$stem = preg_replace('/[^\w]/', '', $stem); 

# Make sure the file extension has no odd characters 
if (($extension != '') && 
    (!preg_match('/^\.\w+$/', $extension))) 
{ 
    echo("odd characters in extension"); 
    //die("Bad file extension"); 
    return FALSE; 
} 

$safeExtensions = array(
'.zip', 
'.psd', 
'.pdf', 
'.jpg', 
'.jpeg', 
'.gif', 
'.rar', 
'.gz', 
'.ai', 
'.eps', 
'.bmp', 
'.pub', 
'.xls', 
'.doc', 
'.wpd', 
'.rtf', 
'.tiff', 
'.tif', 
'.pcx', 
'.ttf', 
'.png', 
'.txt', 
'.mp3', 
'.avi', 
'.mov', 
'.wav' 
); 

if (!in_array($extension, $safeExtensions)) { 
    echo("Extension &quot;$extension&quot; not approved."); 
    //die("File extension not approved"); 
    return FALSE; 
} 

# Search for a unique filename by adding a number to the 
# stem (first we try without, of course) 

$suffix = ''; 
while (file_exists($finalDir."/".$stem.$suffix.$extension)) { 
    if ($suffix == '') { 
    $suffix = '0'; 
    } else { 
    $suffix++; 
    } 
} 

# Put the full name back together 
$name = "$stem$suffix$extension"; 
return $name; 
} 

要特別注意這個部分: 「雖然(file_exists ......」

+0

使用註釋,不要發回答;-) – undone 2012-03-07 02:35:16

+0

問題是你有循環這裏..使用循環很容易編碼增量,但在我的情況下,我不能使用循環。不管怎麼說,還是要謝謝你。! xD – 2012-03-07 03:04:32

+0

我認爲你誤解了文件的功能。它只循環足以找到正確的文件名,然後寫入一個文件。 '$ suffix =''; while(file_exists($ finalDir。「/」。$ stem。$ suffix。$ extension)){if($ suffix ==''){$ suffix ='0'; } else {$ suffix ++; }}'這實際上是你需要的唯一部分,但我認爲我的變量名在上下文中會更有意義。 – TecBrat 2012-03-07 03:21:58