-1

I'm trying to rename some jpegs in a single directory. The code half works in that they are renamed with the correct filenames but for some reason the new filenames are surrounded with double quotes which makes them inaccessible from my web pages.

Any help appreciated!

$i = 10000;

foreach ($imgArray as $v) {

    $html_file_name = basename($v).PHP_EOL;
    $html_file_name =  str_replace(range(0,9),'', $html_file_name);

    $path = pathinfo($v, PATHINFO_DIRNAME);

    $target = ++$i . $html_file_name;

    rename ($v, $path . '/' . $target);

}

OK so here's the var_dump($imgArray):

array(3) { [0]=> string(47) "../img/gallery/this-is-the-first/10002-vddf.jpg" [1]=> string(51) "../img/gallery/this-is-the-first/10001-vfdssddf.jpg" [2]=> string(50) "../img/gallery/this-is-the-first/10003-vddsvsf.jpg" }

Serialized:

a:3:{i:0;s:47:"../img/gallery/this-is-the-first/10002-vddf.jpg";i:1;s:51:"../img/gallery/this-is-the-first/10001-vfdssddf.jpg";i:2;s:50:"../img/gallery/this-is-the-first/10003-vddsvsf.jpg";}
Pee Lee
  • 3
  • 3
  • i gues u could use str.replace('"','\'', $string); – gabrjan Oct 17 '12 at 11:57
  • Your code does not contain the cause for that phenomenon. – phant0m Oct 17 '12 at 11:57
  • I thought about that but I was wondering why it is doing the double quotes in the first place, if I new why the maybe I could implement a nicer solution – Pee Lee Oct 17 '12 at 12:00
  • @PeeLee: The string `$html_file_name` is likely to contain such quotes. So you either need to remove them or even better, don't put them in there in the first place. Improve your HTML parsing. http://php.net/strings – hakre Oct 17 '12 at 12:01
  • What does `$imgArray` look like .. ?? – Baba Oct 17 '12 at 12:01
  • @phant0m, that's pretty much all the code... hmm – Pee Lee Oct 17 '12 at 12:02
  • You need to look at `$imgArray`. – phant0m Oct 17 '12 at 12:03
  • @hakre $html_file_name looks like:../img/gallery/this-is-the-first/10001-vfdssddf.jpg – Pee Lee Oct 17 '12 at 12:05
  • @Baba $imgArray looks like:Array ( [0] => ../img/gallery/this-is-the-first/10002-vddf.jpg [1] => ../img/gallery/this-is-the-first/10001-vfdssddf.jpg [2] => ../img/gallery/this-is-the-first/10003-vddsvsf.jpg ) – Pee Lee Oct 17 '12 at 12:05
  • can you `serialize($imgArray)` and put it here .. i would like to test your array myself – Baba Oct 17 '12 at 12:06
  • @PeeLee: Use `var_dump` instead of `print_r`. Also don't post that in comments, but edit your question. Apart from that your code looks horrorful. I bet you make a lot of mistakes in other places, too. So better do some early error checking and proper debugging to be on the safe side. – hakre Oct 17 '12 at 12:07

1 Answers1

0

You have new line issue in file name

  $html_file_name = basename($v).PHP_EOL;
                                    ^-------- Appending End of Line to File Name

All you need is

$i = 10000;
foreach ($imgArray as $v) {
    rename($v, pathinfo($v, PATHINFO_DIRNAME) . '/' . (++$i . str_replace(range(0,9),'',  basename($v))));
}
Baba
  • 94,024
  • 28
  • 166
  • 217