简体   繁体   English

目录中的PHP重命名文件夹

[英]PHP rename folder in directory

How can i get $filename to be one specific path? 如何获得$filename作为特定路径? Not a specific folder? 没有特定的文件夹? I want to rename several folders in folder named output . 我想重命名名为output文件夹中的几个文件夹。

I tried this: 我尝试了这个:

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

Here is my original code: 这是我的原始代码:

<?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);
        }
?>

Here is a sample script that does (I think) what you're looking for. 这是一个示例脚本,可以(确实)满足您的需求。 It loops through a directory, renaming all subdirectories. 它遍历目录,重命名所有子目录。 Save code to demo.php , and chmod +x demo.php && ./demo.php . 将代码保存到demo.phpchmod + x demo.php && ./demo.php中

If you'd prefer an object-oriented approach, or need any changes let me know and I'll modify the code. 如果您更喜欢面向对象的方法,或者需要任何更改,请告诉我,我将修改代码。


#!/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.
}
$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";
}

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

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