php脚本,无法将命令应用于生成的文件

时间:2015-09-18 11:15:54

标签: php command-line rename

我有一个非常简单的代码,但最后的命令我不能将它应用于生成的文件,我无法重命名该文件,我不知道为什么。这不是重命名问题,因为如果我更改$cmd ="cp '$textfile''$file'"$cmd ="mv '$textfile''$file'"的重命名,则它也不起作用。我最好用代码解释一下:

<?php
// the original file to work with
$file = "COPYING.odt";

//change the extension of doc|pdf|docx|odt|rtf files to txt   
$txtfile = preg_replace('"\.(doc|pdf|docx|odt|rtf)$"', '.txt', $file);

//convert the odt file into txt and name it with the original name but txt extension
$cmd = "/usr/bin/unoconv -f txt '$file' -o '$textfile'";
shell_exec($cmd);

//store the file in the originals files folder
 rename("$file", "orig/$file");

//rename the generated txt file with the original name and extension "COPYING.odt"
rename("$textfile", "$file");


echo "conversion of file <em>$file</em> done";
?>

有什么建议吗?感谢。

更新我忘记告诉我的一件事是我将此脚本应用于上传的文件,因此我不能给出$ file的静态名称,而是变量$ file。

更新2 关注@AlanMachado建议我已为重命名更改了 mv 命令,此重命名有效,但下一次无效。

1 个答案:

答案 0 :(得分:0)

对不起伙计们,我用这个脚本已经有三天了,这就是为什么我在Stackoverflow中问这个问题,但刚才我意识到这是一个TYPO错误,是的:

$txtfile = preg_replace('"\.(doc|pdf|docx|odt|rtf)$"', '.txt', $file);

txtfile没有e在txt中,而

rename("$textfile", "$file");

带文字e的文本文件,我真的很抱歉。

无论如何,我不知道为什么这个命令的结果是具有正确名称的文件:

$cmd = "/usr/bin/unoconv -f txt '$file' -o '$textfile'";