简体   繁体   English

如何使用PHP依次重命名文件夹中的文件?

[英]How to sequentially rename files in a folder using PHP?

I have files in a folder called 'thumbs'. 我在名为“ thumbs”的文件夹中有文件。 They have names based on how they were named / renamed by their original authors. 他们的名字基于其原始作者的命名/重命名方式。 I would like to rename them to be two digit sequentially and I managed to find this PHP code: 我想将它们重命名为两位数,然后设法找到以下PHP代码:

function sequentialImages($path, $sort=false) {
 $i = 1;
 $files = glob($path."/{*.gif,*.jpg,*.jpeg,*.png}",GLOB_BRACE|GLOB_NOSORT);

 if ( $sort !== false ) {
  usort($files, $sort);
 }

 $count = count($files);
 foreach ( $files as $file ) {
  $newname = str_pad($i, strlen($count)+1, '0', STR_PAD_LEFT);
  $ext = substr(strrchr($file, '.'), 1);
  $newname = $path.'/'.$newname.'.'.$ext;
  if ( $file != $newname ) {
   rename($file, $newname);  
  }
  $i++;
 }
}

The php to execute this code is called 'rename.php' and it is found in a folder called 'admin'. 执行此代码的php称为“ rename.php”,位于名为“ admin”的文件夹中。

Therefore they are as follows 因此,如下

  1. 'admin' folder (contains rename.php') 'admin'文件夹(包含rename.php')
  2. 'thumbs' folder (contains images with random names) 'thumbs'文件夹(包含具有随机名称的图像)

Both folders are on the same level. 这两个文件夹位于同一级别。

How can I execute 'rename.php' if both are in different folders. 如果两者都在不同的文件夹中,我如何执行“ rename.php”。

I tried to include $path = '../thumbs'; 我试图包括$path = '../thumbs'; but it did not function. 但它没有起作用。

Why isn't not working please? 为什么不工作呢?

I think I would start by checking if you're actually getting any errors reported at all. 我想我首先要检查您是否真的报告了任何错误。 Since you're saying that you only get a blank page without any errors it could be as simple as enabling error reporting to see what actually goes wrong. 因为您说的是空白页面没有任何错误,所以这就像启用错误报告以查看实际出了什么问题一样简单。

If, for instance, PHP doesn't have write access to the thumbs-folder you'll probably get a bunch of warnings when you try to rename the files. 例如,如果PHP对thumb-folder没有写权限,则在您尝试重命名文件时可能会收到一堆警告。 Check your php.ini and make sure that display_errors = On , run the script again and check if you get any helpful error messages. 检查您的php.ini并确保display_errors = On ,再次运行脚本并检查是否获得任何有用的错误消息。

Not sure if that helps you (or if display_errors is already set to on), but that would be the first step that I would try, which hopefully gives you a little more details about what's going wrong. 不知道这是否对您有帮助(或者是否已经将display_errors设置为on),但这将是我尝试的第一步,希望可以为您提供更多有关发生问题的详细信息。

At the top of your function add: 在函数顶部添加:

if( !is_dir($path) ) {
  die("error: $path is not a valid directory.");
  // or whatever error handling method you prefer
}

If, for some reason, parent paths are disabled on your server, or if there is some other issue with the path, you will be notified here. 如果由于某种原因在服务器上禁用了父路径,或者该路径存在其他问题,则会在此处通知您。

If that works fine, then step through your code and make sure that everything is as you expect it to be. 如果一切正常,请单步执行代码,并确保所有操作均符合您的预期。 eg: $files has the contents you expect, $newname is formatted properly, rename() is not returning false which indicates an error likely due to permissions, etc... 例如: $files具有您期望的内容, $newname格式正确, rename()没有返回false ,这表明可能是由于权限等导致的错误。

$path = realpath(__DIR__ . '/..') . '/thumbs';

realpath() function: http://php.net/manual/en/function.realpath.php realpath()函数: http : //php.net/manual/zh/function.realpath.php

DIR (and other magic constants): http://php.net/manual/en/language.constants.predefined.php DIR (和其他魔术常数): http : //php.net/manual/zh/language.constants.predefined.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM