重命名文件夹中的所有文件

发布于 2024-12-13 04:26:12 字数 1305 浏览 1 评论 0原文

我让我的 iPhone 应用程序从我的数据库中删除选定的图像,在删除我想要的图像后,它需要重命名文件夹中剩余的所有其他图像。假设我的文件夹有 3 个图像:img1、img2、img3 等。假设我删除了 img1,我继续 unlink() 它,现在我需要将 img2 重命名为 img1,然后将 img3 重命名为 img2。

这是我现在的代码

<?php
$dbc = mysql_connect('hostname', 'login', 'password');
if(!$dbc){
die('not connected : ' . mysql_error());
}

$db_selected = mysql_select_db("database", $dbc);
if(!$db_selected){
die("could not connect to DB : " . mysql_error());  
}

$username = $_GET['username'];
$image = $_GET['imagename'];

$check = mysql_query("SELECT image FROM members WHERE username='$username'");
$findMem = mysql_num_rows($check);

if($findMem > 0){

while($row = mysql_fetch_array($check) or die(mysql_error())){
$images = $row["image"];
$images = $images - 1;
unlink("./$image");

mysql_query("UPDATE members SET image='$images'
WHERE username='$username'");
}

$path = "$username/default/";


$files = glob("{$path}/{*.jpg,*.jpeg,*.png}", GLOB_BRACE);


function filetime_callback($a, $b)
{
  if (filemtime($a) === filemtime($b)) return 0;
  return filemtime($a) > filemtime($b) ? -1 : 1; 
} 

// Then sort with usort()
usort($files, "filetime_callback");

}

?>

如您所见,我设置了一个函数,它将按创建日期对所有文件进行排序。

我仍然希望创建的日期保持不变,我只需要重命名文件夹内的文件。可能有 50 张图像,所以它必须是某种循环。

感谢您的帮助:)

I am having my iPhone app delete a selected image off of my database, after it deletes the image i want, it needs to rename all of the other images left in the folder. So say my folder has 3 images: img1, img2, img3 and so on. Say i delete img1, i go ahead and unlink() it and now i need to rename img2 to img1 and then img3 to img2.

Here is my code for right now

<?php
$dbc = mysql_connect('hostname', 'login', 'password');
if(!$dbc){
die('not connected : ' . mysql_error());
}

$db_selected = mysql_select_db("database", $dbc);
if(!$db_selected){
die("could not connect to DB : " . mysql_error());  
}

$username = $_GET['username'];
$image = $_GET['imagename'];

$check = mysql_query("SELECT image FROM members WHERE username='$username'");
$findMem = mysql_num_rows($check);

if($findMem > 0){

while($row = mysql_fetch_array($check) or die(mysql_error())){
$images = $row["image"];
$images = $images - 1;
unlink("./$image");

mysql_query("UPDATE members SET image='$images'
WHERE username='$username'");
}

$path = "$username/default/";


$files = glob("{$path}/{*.jpg,*.jpeg,*.png}", GLOB_BRACE);


function filetime_callback($a, $b)
{
  if (filemtime($a) === filemtime($b)) return 0;
  return filemtime($a) > filemtime($b) ? -1 : 1; 
} 

// Then sort with usort()
usort($files, "filetime_callback");

}

?>

As you see i have a function set up that will order all of the files by the date created.

I still want the dates created to stay the same, i just need to rename the files inside the folder. There could be 50 images, so its going to have to be some sort of loop.

Thanks for the help :)

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

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

发布评论

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

评论(1

酷到爆炸 2024-12-20 04:26:12

请参阅http://php.net/manual/en/function.rename.php

不过,您可能应该考虑依赖其他一些机制来排序文件。我建议您在数据库中添加一个 order by 字段并基于该字段进行排序。这样,您就可以对文件进行排序,而与您指定的名称无关。

See http://php.net/manual/en/function.rename.php

You should probably look at relying on some other mechanism for ordering your files though. I'd suggest that you add an order by field in your database and sort based on that. That way you can have your files sorted independent of the name that you give them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文