-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnewfiles.php
104 lines (87 loc) · 2.66 KB
/
newfiles.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT.'/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
$Directory = "./sites/default/files/maps";
// $existing_pagenode = node_load(139);
// print_r($existing_pagenode);
// open the Directory
$dhandle = dir($Directory);
// define an array to hold the files
$files = array();
if ($dhandle) {
// loop through all of the files
while (false !== ($fname = $dhandle->read())) {
// if the file is not this file, and does not start with a '.' or '..',
// then store it for later display
if (($fname != '.') && ($fname != '..') &&
($fname != basename($_SERVER['PHP_SELF']))) {
$file = $Directory."/".$fname;
// Get the town name out of file name
$fname = substr($fname, 9);
$fname = substr($fname, 0,-19);
$fname = str_ireplace("_", " ", $fname);
$fname = ucwords($fname);
$PlaceName = $fname;
// check if term already in:
$suburb = taxonomy_get_term_by_name($PlaceName);
if (empty($suburb)) {
$suburb = new stdClass();
$suburb->name= $PlaceName;
$suburb->vid = 3;
taxonomy_term_save($suburb);
$suburb = taxonomy_get_term_by_name($PlaceName);
} else {
continue;
}
$numb = array_keys($suburb);
$suburbTermID = $numb[0];
// Create the node:
$newnode = new stdClass();
// Set the Title
$newnode->title = "Flood Flag Map - ".$PlaceName;
$newnode->body = "";
$newnode->uid = 3; // Nicks user;
$newnode->type = 'article';
$newnode->status = 1;
$newnode->promote = 0;
$newnode->field_tags['und'][0]['tid']= 36;
$newnode->field_suburb['und'][0]['tid'] = $suburbTermID;
// have to add in a suburb first??
// Get the file size
$details = stat($file);
$filesize = $details['size'];
$dest = 'public://user_files/';
// Copy the file to the Drupal files directory
$name = basename($file);
// Build the file object
$file_obj = new stdClass();
$file_obj->filename = $name;
$file_obj->uri = $file;
$file_obj->filemime = file_get_mimetype($name);
$file_obj->filesize = $filesize;
$file_obj->filesource = $name;
$file_obj->uid = 3; // Nick
$file_obj->status = FILE_STATUS_PERMANENT;
$file_obj->timestamp = time();
$file_obj->list = 1;
$file_obj->new = true;
// TODO do a file move
$file_obj = file_copy($file_obj,$dest);
// Attach the file object to your node
$newnode->field_file['und'][0]['fid'] = $file_obj->fid;
$newnode->field_file['und'][0]['display'] = 1;
node_save($newnode);
$newpath = strtolower($newnode->title);
$newpath = str_replace(" - ", "-", $newpath);
$newpath = str_replace(" ", "-", $newpath);
$path = array();
$path['source']=$newnode->uri['path'];
$path['alias'] = "article/".$newpath;
path_save($path);
//content_insert($newnode);
unset($newnode);
unset($file_obj);
}
}
}