Skip to content

Instantly share code, notes, and snippets.

@soyuka
Last active December 23, 2015 23:39
Show Gist options
  • Save soyuka/6711885 to your computer and use it in GitHub Desktop.
Save soyuka/6711885 to your computer and use it in GitHub Desktop.
Prestashop images upgrade between < 1.4 (not 1.4) and 1.5, creates the folders according to the pictures names.
<?php
/**
* @author Antoine Bluchet
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
* Transforms old pictures id-img_id-mode.jpg to /i/m/g/i/d/mode.jpg
* for example 101-415-default.jpg to /4/1/5/default.jpg
*
* Usage : replace $path and launch the script
**/
$path = './img/p'; //Path to your pictures
$copy = true; //Copy, set to false to move the pictures /!\ At your own risk /!\
//Directory separator
define('DS', '/');
//no time limit
set_time_limit(0);
if ($handle = opendir($path)) {
while (false !== ($entry = readdir($handle))) {
if($entry !== '.' && $entry != '..') {
//Explode picture name
$array = explode('-', $entry);
//Unset the first ID
unset($array[0]);
if(isset($array[1])) {
//Get the numbers which are the directories
if(preg_match_all("/\d+/", $array[1], $matches))
$nb = str_split($matches[0][0]);
$dir = $path . DS . implode(DS, $nb);
//Create dirs recursivly
if(!file_exists($dir))
mkdir($dir, 0755, true);
//Copy file path
$copy = $dir . DS . implode('-', $array);
if(!file_exists($copy))
if($copy === true)
copy($path . DS . $entry, $copy);
else
rename($path . DS . $entry, $copy);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment