Skip to content

Instantly share code, notes, and snippets.

@ibes
Created October 16, 2010 15:38
Show Gist options
  • Save ibes/629915 to your computer and use it in GitHub Desktop.
Save ibes/629915 to your computer and use it in GitHub Desktop.
<html>
<body>
<?php
/* Someone should write an GUI for that.
* a form with files:
* csv_path, path to attachments-files, path to fileUpload-folder, checkbox wether it's a testrun or files should be copied, dispersion-factor
*
* What does this script?
* 1) Takes the variables form the form or of the config-section
* 2) Read in the files out of the CSV-file
* 3) copy the files from "attachments" to "fileUpload" folder
*/
/* Config section
* fill in your paths and so on
*/
// if true, only testrun, dont copy files
$testrun = false;
// the path to your CSV-file
$csv_path = NULL;
// path to your old attachments folder
$from_attachments_folder_path = NULL;
// path to your new fileUpload folder
$to_fileUpload_folder_path = NULL;
// dispersion-factor spread for files to many folders - by default 20, INTEGER
$dispersion_factor = NULL ; // integer
/* Checking the input variables
*/
// Check for testrun
if ($testrun == true){
echo "Lame operation, no changes will be made.";
$lame = true;
}
// check for csv_path
// may there be a much shorter way to write that!
// may should check if file exists
if (!isset($csv_path)){
die("No input CSV path given."); // End script here with error message
} else{
$input_path = $csv_path;
}
// check for attachments-folder
// may there be a much shorter way to write that!
// may should check if folder exists
if (!isset($from_attachments_folder_path)){
die("No source images base path given."); // End script here with error message
} else{
$source_images_path = $from_attachments_folder_path;
}
// check for attachments-folder
// may there be a much shorter way to write that!
// may should check if folder exists
if (!isset($to_fileUpload_folder_path)){
die ("No dest images base path given."); // End script here with error message
} else{
$dest_images_path = $to_fileUpload_folder_path;
}
// check for dispersion-factor - must be integer
if (!isset($dispersion_factor)){
$dispersion_factor == 20;
print "No dispersion factor given, using default of 20, please specify Plugin.FileUpload.DispersionFactor if different from default.";
//Don't break the script
}
// status message
echo "Reading form". $from_attachments_folder_path;
$path_to_id = array(); // new array
// open CSV-file
if (!file_exists($input_path)){die ("CSV file doesn't exist!");}
else {
$handle = fopen ($input_path, "r");
// read file
while (!feof($handle)) {
$line = fgets($handle);
// get infos - example for line: "FileUpload/2010/07/filename01.jpg";"3626"
preg_match("/^\"(.+?)\";\"(\d+?)\"/", $line, $result);
// if line is empty - error message and next line
if (!isset($result)) {
echo "No match for line" . $line . ",skipping";
continue;
}
//key is path to file, value is media_id
$path_to_id[$result[1]] = $result[2];
} // End read file
// Count records -> status message
echo count($path_to_id) . "records read.";
foreach ($path_to_id as $path => $media_id){
$source_path = $source_image_path ."/". $path; //get path to source file
$path_parts = pathinfo($source_path); // get extetion of the file
$source_extention = $path_parts['extension'];
$dispersion_id = $media_id % $dispersion_factor; // get dispersion_id
$dest_dir = $dest_images_path . "/" . $dispersion_id; // get new folder + dispersion
$dest_path = $dest_dir. "/" . $media_id . $source_extention; //get new file path
// status message
echo "Copying " . $source_path ." -> " . $dest_path;
// Copying
try {
if ($lame != true){
mkdir($dest_dir);
copy($source_path, $dest_path);
throw new Exception('Error while copying');
}}
catch (Exception $e)
{
echo 'Exceptions chaught: ', $e->getMessage(), "\n";
}
}
// status message
echo "all done, all fine - have fun with fileUpload";
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment