随着文件上传,重命名文件的数量不断增加-PHP

随着文件上传,重命名文件的数量不断增加-PHP,php,file,upload,rename,auto-increment,Php,File,Upload,Rename,Auto Increment,我需要为上传的文件提供递增的数字。我正在使用SFWUpload。我有以下代码: mkdir("../../imagenes/".$codigo , 0777); $i = 1; $nombre = $codigo.'_'.$i.'.jpg'; move_uploaded_file($_FILES['Filedata']['tmp_name'], "../../imagenes/".$codigo."/".$nombre); chmod("../../imagenes/".$codigo."/".

我需要为上传的文件提供递增的数字。我正在使用SFWUpload。我有以下代码:

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,所以我不能使用foreach循环(我猜)

我需要脚本来检查文件是否存在并编写下一个。例如,如果存在101213_4.jpg,则写入101213_5.jpg

你能帮我怎么做吗。??我是php新手,什么都试过了(

提前谢谢


Roberto

以下是我使用的一个函数:

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;
}
函数cleanup\u name($name){//接受名称并将其清除。
$finalDir='/home/username/uploads';
#请转到所有小写字母以确保一致性
$name=strtolower($name);
//echo(“名称为$Name
”; $matches=split(“\”,$name); foreach($匹配为$key=>$value){ $exkey=$key; $exvalue=$value;//如果有多个句点,则会找到实际的扩展名。 //echo(“Key$Key |$exkey Value$Value |$exvalue
”; }
如果($exkey这里有一个我使用的函数:

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;
}
函数cleanup\u name($name){//接受名称并将其清除。
$finalDir='/home/username/uploads';
#请转到所有小写字母以确保一致性
$name=strtolower($name);
//echo(“名称为$Name
”; $matches=split(“\”,$name); foreach($匹配为$key=>$value){ $exkey=$key; $exvalue=$value;//如果有多个句点,则会找到实际的扩展名。 //echo(“Key$Key |$exkey Value$Value |$exvalue
”; }
如果($exkey那么……您可以尝试获取文件夹中当前的文件数,然后从那里获取
$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);

尝试该代码…

好吧…您可以尝试获取文件夹中当前的文件数,然后从那里获取
$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);


试试那个代码…

告诉我们你的“一切”尝试,然后我们可以从那里开始:)把数字保存在某个地方并使用那个数字“一切”是计数文件,检查文件是否存在,甚至使用会话,但没有任何效果。!:(是的,这就是想法。!重点是“如何”。我将尝试“鹰”编写的解决方案。告诉我们你的答案“everything”尝试,然后我们可以从那里开始:)将数字保存在某个地方并使用该数字“everything”是计数文件,检查文件是否存在,甚至使用会话,但没有任何效果。!:(是的,这就是想法。!重点是“如何”。我将尝试“eagle”编写的解决方案。使用注释,不要发送答案;-)问题是这里有循环..使用循环很容易编写增量代码,但在我的情况下,我不能使用循环。无论如何,谢谢!xDI认为您误解了文件的功能。它只循环足够找到正确的文件名,然后写入一个文件。
$suffix='';while(文件存在($finalDir./“$stem.$suffix.$extension)){if($suffix=''{$suffix='0';}否则{$suffix++;}
这是您真正需要的唯一部分,但我认为我的变量名在上下文中更有意义。如果两个人同时尝试上载,看起来您有竞争条件。请使用注释,不要发送答案;-)问题是这里有循环..使用循环很容易编写增量代码,但在我的情况下,我不能使用循环。无论如何,谢谢!xDI认为您误解了文件的功能。它只循环足够找到正确的文件名,然后写入一个文件。
$suffix='';while(文件存在($finalDir./“$stem.$suffix.$extension)){if($suffix=''{$suffix='0';}否则{$suffix++;}
这是您真正需要的唯一部分,但我认为我的变量名在上下文中更有意义。如果两个人同时尝试上载,看起来您有竞争条件。这很有效。!谢谢。!!我对这行代码做了此更改。!!$I=count(glob(“../../imagenes/“$codigo.”/*.jpg”)+1;谢谢!;)当两个人试图同时上传时会发生什么情况?我想了想……但是如何避免PHP进程之间的竞争条件?可能使用信号量?sem_get、sem_acquire等。这很有效。!谢谢。!!我对这行代码做了更改。!!$I=count../../imagenes/“$codigo.”/*.jpg”)+1;谢谢!;)当两个人试图同时上传时会发生什么?我想了想……但是如何避免PHP进程之间的竞争条件?可能使用信号量?sem_get、sem_acquire等。