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

如何在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

我想使用带有php的xpdf转换器将pdf文件转换为文本文件。 问题是文件名是文件 (1).pdf,file(2).pdf ...括号是问题.XPDF无法用括号读取文件名。所以我想将名称更改为file1.pdf,file2.pdf ....(删除 括号) 我尝试在php中使用rename()以各种可能的方式进行转义但仍然得到两个错误之一 p>

=系统找不到路径代码2 =目录,文件 ,音量语法是错误的或什么...代码123 p>

请有人帮助我。我想用PHP本身做。 p>

我是什么 用于... p>

  for($ i = 1; $ i&lt; = $ _ POST ['number']; $ i ++)
 {
 
 $ filen2 =  “\”d:/ res / file(“。$ i。”)。pdf \“”; 
 $ file =“\”d:/ res / files“。$ i。”。pdf \“”; \  n重命名($ filen2,$ file); 
 echo“转换文件”。$ filen2。“&lt; br /&gt;”; 
 shell_exec(“D:/xpdf/bin32/pdftotext.exe $ file”);  
 
} 
  code>  pre> 
 
 

和... p>

  for($ i = 1; $ i&lt;  = $ _ POST [ '号'  ]; $ i ++)
 {
 
 $ filen2 =“\”d:\
es \\ file(“。$ i。”)。pdf \“”; 
 $ file =“\”d:  \
es \\ files“。$ i。”。pdf \“”; 
重命名($ filen2,$ file); 
 echo“转换文件”。$ filen2。“&lt; br /&gt;”; \  n shell_exec(“D:/xpdf/bin32/pdftotext.exe $ filen2”); 
 
} 
  code>  pre> 
 
 

现在我尝试对文件重命名() 没有括号......仍然没有用......有什么可能是错的?XPDF为没有支架的文件工作没有任何问题 p> div>

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.