我需要使用php交换文件名。例如,我有两个文件,第一个文件:image1.jpg和第二个文件:image2.jpg。我想交换文件名。因此,第一个文件将是名称image2.jpg,第二个文件将命名为image1.jpg;
我未能成功:
function swap($name1, $name2)
{
$tempName1 = "temporary1";
$tempName2 = "temporary2";
myRename($name1, $tempName1);
myRename($name2, $tempName2);
myRename($tempName1, $name2);
myRename($tempName2, $name1);
}
function myRename($oldTitle, $newTitle)
{
$oldDirectory = "images/".$oldTitle.".jpg";
$newDirectory = "images/".$newTitle.".jpg";
rename($oldDirectory, $newDirectory);
}
如何成功交换姓名?
答案 0 :(得分:0)
<?php
rename('images/image1.jpg','images/tmp.jpg');
rename('images/image2.jpg','images/image1.jpg');
rename('images/tmp.jpg','images/image2.jpg');
答案 1 :(得分:0)
function myRename($oldTitle, $newTitle)
{
$fullpath="C:\HD path to that file";//sth like that for full path
$oldDirectory = $fullpath."images/".$oldTitle.".jpg";
$newDirectory = $fullpath."images/".$newTitle.".jpg";
rename($oldDirectory, $newDirectory);
}
答案 2 :(得分:0)
这样的事情可以解决你的问题:
swap($file1, $file2, 'test/');
function swap ($name1, $name2, $dir = '') {
rename($name1, $name1 . '-tmp');
rename($name2, $name2 . '-tmp');
rename($name2 . '-tmp', $dir . $name1);
rename($name1 . '-tmp', $dir . $name2);
}
希望这有帮助!