douji4223 2014-04-05 17:31
浏览 55
已采纳

如何在php中将文件(1).ext重命名为file1.ext

I want to convert pdf files into text files using xpdf converter with php. The problem is the file names are file(1).pdf,file(2).pdf...The brackets are the problem.XPDF cannot read file names with brackets.So i want to change the name to file1.pdf,file2.pdf....(remove brackets) I tried to use rename() in php in every possible way with escaping but still get either of two errors

=System cannot find the path code 2 =Directory,file,volume syntax is wrong or something... code 123

Please somebody help me.I want to do it in PHP itself.

what i used is..

for($i=1;$i<=$_POST['number'];$i++)
{

    $filen2="\"d:/res/file(".$i.").pdf\"";
    $file="\"d:/res/files".$i.".pdf\"";
    rename($filen2,$file);
    echo "converting file ".$filen2."<br/>";
    shell_exec("D:/xpdf/bin32/pdftotext.exe $file");

} 

and...

for($i=1;$i<=$_POST['number'];$i++)
{

    $filen2="\"d:\es\\file(".$i.").pdf\"";
    $file="\"d:\es\\files".$i.".pdf\"";
    rename($filen2,$file);
    echo "converting file ".$filen2."<br/>";
    shell_exec("D:/xpdf/bin32/pdftotext.exe $filen2");

} 

right now i tried rename() on files without brackets...still it doesnt work...what could be wrong??XPDF worked for files without bracket without any problem

  • 写回答

1条回答 默认 最新

  • dongtuo1482 2014-05-11 06:12
    关注

    I solved the problem.Actually the problem was with my Windows.There was a f***** virus in it which prevented me from accessing advaned operations like changing PATH variable,opening control panel etc...Now i reinstalld Windows and every thing works just as expected..

    The syntax of rename() is rename(<path to file>,<path to the same file with new name>);

    for example to rename D:es\file(1).pdf to file1.pdf i used the following code rename('D:/res/file(1).pdf','D:/res/file1.pdf');.It worked perfectly well. I also tried rename('D:\es\\file(1).pdf','D:\es\\file1.pdf'); it also worked well.Wish I had reinstalled Windows earlier.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 Python如何后台操作Vmwake虚拟机键鼠
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容