如果该文件名已存在,则将数字添加到 fopen 链接

发布于 2024-12-10 18:32:01 字数 387 浏览 0 评论 0原文

基本上,我想在每次文件已经存在时继续添加数字。因此,如果 $url.php 存在,请将其设置为 $url-1.php。如果 $url-1.php 存在,则将其设为 $url-2.php,依此类推。

这是我已经想出的,但我认为它只会在第一次时起作用。

if(file_exists($url.php)) {
    $fh = fopen("$url-1.php", "a");
    fwrite($fh, $text);
} else {
    $fh = fopen("$url.php", "a");
    fwrite($fh, $text);
}
fclose($fh);

Basically, I want to continue adding numbers each time the file already existed. So if $url.php exists, make it $url-1.php. If $url-1.php exists, then make it $url-2.php, and so forth.

This is what I already came up with, but I think it'll only work the first time.

if(file_exists($url.php)) {
    $fh = fopen("$url-1.php", "a");
    fwrite($fh, $text);
} else {
    $fh = fopen("$url.php", "a");
    fwrite($fh, $text);
}
fclose($fh);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无名指的心愿 2024-12-17 18:32:01

对于这样的场景,我使用 while 循环。

$filename=$url;//Presuming '$url' doesn't have php extension already
$fn=$filename.'.php';
$i=1;
while(file_exists($fn)){
   $fn=$filename.'-'.$i.'.php';
   $i++;
}
$fh=fopen($fn,'a');
fwrite($fh,$text);
fclose($fh);

尽管如此,这个方向的解决方案并不能很好地扩展。您不想定期检查超过 100 个 file_exists

I use while loops for scenarios like this.

$filename=$url;//Presuming '$url' doesn't have php extension already
$fn=$filename.'.php';
$i=1;
while(file_exists($fn)){
   $fn=$filename.'-'.$i.'.php';
   $i++;
}
$fh=fopen($fn,'a');
fwrite($fh,$text);
fclose($fh);

All that said, this direction of solutions does not scale well. You do not want to be checking over a 100 file_exists routinely.

不打扰别人 2024-12-17 18:32:01

使用带有计数器变量 $i 的 while 循环。继续增加计数器直到 file_exists() 返回 false。此时,while 循环退出,您可以使用 $i 的当前值对文件名调用 fopen()

if(file_exists("$url.php")) {
  $fh = fopen("$url-1.php", "a");
  fwrite($fh, $text);
} else {
  $i = 1;
  // Loop while checking file_exists() with the current value of $i
  while (file_exists("$url-$i.php")) {
    $i++;
  }

  // Now you have a value for `$i` which doesn't yet exist
  $fh = fopen("$url-$i.php", "a");
  fwrite($fh, $text);
}
fclose($fh);

Use a while loop with a counter variable $i. Keep incrementing the counter until file_exists() returns false. At that point, the while loop exits and you call fopen() on the filename with the current value for $i;

if(file_exists("$url.php")) {
  $fh = fopen("$url-1.php", "a");
  fwrite($fh, $text);
} else {
  $i = 1;
  // Loop while checking file_exists() with the current value of $i
  while (file_exists("$url-$i.php")) {
    $i++;
  }

  // Now you have a value for `$i` which doesn't yet exist
  $fh = fopen("$url-$i.php", "a");
  fwrite($fh, $text);
}
fclose($fh);
虫児飞 2024-12-17 18:32:01

我一直在寻找类似的东西,并根据我的需要扩展了沙德的答案。我需要确保文件上传不会覆盖服务器上已存在的文件。
我知道它还没有“保存”,因为它不能处理没有扩展名的文件。但也许这对某人有一点帮助。

        $original_filename = $_FILES["myfile"]["name"];
        if(file_exists($output_dir.$original_filename))
        {

            $filename_only = substr($original_filename, 0, strrpos($original_filename, "."));
            $ext = substr($original_filename, strrpos($original_filename, "."));

            $fn = $filename_only.$ext;
            $i=1;
            while(file_exists($output_dir.$fn)){
               $fn=$filename_only.'_'.$i.$ext;
               $i++;
            }
        }
        else
        {
            $fn = $original_filename;
        }

i was looking for something similar like this and extended Shad's answer for my needs. i need to make sure that a fileupload doesn't overwrite files which already exist on a server.
i know it's not "save" yet, cause it doesn't handle files without extension. but maybe it is a little help for somebody.

        $original_filename = $_FILES["myfile"]["name"];
        if(file_exists($output_dir.$original_filename))
        {

            $filename_only = substr($original_filename, 0, strrpos($original_filename, "."));
            $ext = substr($original_filename, strrpos($original_filename, "."));

            $fn = $filename_only.$ext;
            $i=1;
            while(file_exists($output_dir.$fn)){
               $fn=$filename_only.'_'.$i.$ext;
               $i++;
            }
        }
        else
        {
            $fn = $original_filename;
        }
已下线请稍等 2024-12-17 18:32:01
<?php
$base_name = 'blah-';
$extension = '.php';
while ($counter < 1000 ) {
    $filename = $base_name . $counter++ . $extension; 
    if ( file_exists($filename) ) continue;
}
$fh = fopen($filename, "a");
fwrite($fh, $text);
fclose($fh);
<?php
$base_name = 'blah-';
$extension = '.php';
while ($counter < 1000 ) {
    $filename = $base_name . $counter++ . $extension; 
    if ( file_exists($filename) ) continue;
}
$fh = fopen($filename, "a");
fwrite($fh, $text);
fclose($fh);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文