【问题标题】:Rename files increasing as they upload - PHP重命名文件在上传时增加 - PHP
【发布时间】:2012-03-24 14:21:02
【问题描述】:

我需要在上传时给文件增加数量。我正在使用 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新手,什么都试过了! :(

提前致谢

罗伯托

【问题讨论】:

  • 向我们展示您的“一切”尝试,然后我们可以从那里开始 :)
  • 将号码保存在某处并使用该号码
  • “everithing”是计数文件,检查文件是否存在,甚至使用会话,但没有任何效果。! :(
  • 是的,就是这个想法!关键是“如何”。我要试试“eagle”写的解决方案。

标签: php file upload rename auto-increment


【解决方案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;
}

特别注意带有以下内容的部分:“while (file_exists...”

【讨论】:

  • 使用评论,不要发送答案;-)
  • 问题是你这里有循环。使用循环很容易编写增量代码,但就我而言,我不能使用循环。不管怎么说,还是要谢谢你。! xD
  • 我认为您误解了文件的作用。它只循环到找到正确的文件名,然后写入一个文件。 $suffix = ''; while (file_exists($finalDir."/".$stem.$suffix.$extension)) { if ($suffix == '') { $suffix = '0'; } else { $suffix++; } } 这确实是您需要的唯一部分,但我认为我的变量名在上下文中会更有意义。
  • 如果两个人尝试同时上传,您似乎有竞争条件。
【解决方案2】:

嗯...您可以尝试获取文件夹中的当前文件数并从那里获取$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 = count(glob("../../imagenes/".$codigo."/*.jpg")) + 1;谢谢。! ;)
  • 两个人同时尝试上传会发生什么?
  • 我考虑过...但是您如何避免 PHP 进程之间的竞争条件?也许使用信号量? sem_get、sem_acquire 等
猜你喜欢
  • 2014-01-15
  • 2013-09-21
  • 1970-01-01
  • 2021-10-25
  • 2020-08-29
  • 2020-05-19
  • 2016-02-28
  • 2012-05-04
  • 1970-01-01
相关资源
最近更新 更多