PHP重命名目录中的文件夹

时间:2014-07-04 08:54:52

标签: php explode file-rename

如何让$filename成为一条特定路径?不是特定的文件夹? 我想重命名名为output的文件夹中的几个文件夹。

我试过了:

$fileName = '/path/folder/output';

这是我的原始代码:

<?php

        $fileName = '351437-367628';
        $newNametemp = explode("-",$fileName);
        if(is_array($newNametemp)){
            $newName = $newNametemp[0];
            print_r($newName); // lar navnet stå att etter første bindestrek
            rename($fileName, $newName);
        }
?>

2 个答案:

答案 0 :(得分:0)

这是一个示例脚本(我认为)您正在寻找的内容。它遍历一个目录,重命名所有子目录。 将代码保存到 demo.php ,然后 chmod + x demo.php &amp;&amp; ./ demo.php

如果你更喜欢面向对象的方法,或者需要任何更改,请告诉我,我会修改代码。


#!/usr/bin/php 
<?php


//root directory
$outputDir = '/path/folder/output';

//get all subdirectories in the root directory
$dirs = scandir($outputDir);
if ($dirs === FALSE) {
  echo "scandir() failed.\n";
  exit(1);
}

//loop through each subdirectory and rename.
foreach($dirs as $dir) {
  $newName = "${dir}.tmp";  //rename as needed; here we append ".tmp" to the subdirectory.
  $success = rename($dir, $newName);
  if (!$success) 
    echo "Rename of $dir failed.\n";  //there was an error during rename, handle it.
}

答案 1 :(得分:0)

$file_path = "uploads/";
$input = $_POST['name_of _the_folder_You WIsh']; // this is the new folder you'll create
$file_path .= $input . '/';
if (!file_exists($file_path)) {
mkdir($file_path);
}
chmod($file_path, 0777);

$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
    echo "success";
} else{
    echo "fail";
}