-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_post2.php
253 lines (212 loc) · 10.6 KB
/
index_post2.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
/*******************************************************
* PLR-Importer V1 Alpha
* Concept by Michael Scott McGinn
* Co-developed by: Michael Scott McGinn and Ed Reel
* Copyright 2022 by GeekZoneHosting.Com, LLC
* Licenced: MIT
********************************************************/
//The section extracts the files from the .zip and creates a folder to store the .txt files
echo "Uncompressing zip files.<br/>";
$filecounter=0;
$tag="";
$tagName="";
$processedfile=Array();
$combinedfiles=Array();
if (isset($_POST["btn_zip"])) {
$output = '';
if ($_FILES['zip_file']['name'] != '') {
$file_name = $_FILES['zip_file']['name'];
$array = explode(".", $file_name);
$name = $array[0];
echo $name."<br/>";
$tag=$name;
$ext = $array[1];
if ($ext == 'zip') {
$auto_generated = time() . rand(111, 999);
$path = 'upload/' . $auto_generated;
$location = $path . $file_name;
if (move_uploaded_file($_FILES['zip_file']['tmp_name'], $location)) {
$zip = new ZipArchive;
if ($zip->open($location)) {
$zip->extractTo($path);
$zip->close();
}
unlink($location);
}
}
}
//if ($_FILES['zip_file']['name'] = ''){die; }
//if ($_FILES['zip_file']['name'] != ''){$filecounter=+1;}
$filecounter=+1;
}
//End of File Extraction.
//All files should be extracted from the .zip file at this point and in the created folder.
//Code added to display progress. When we get the code working we can comment this out.
$domain="https://plrimporter.com/";//hard coded but would need to change when it gets installed on another domain.
echo $filecounter." .zip files upcompressed<br/>";
echo "==================<br/>The Path to your files is: ".$location."<br/>=============<br/>";
// die;
// End of progress display
//Grabs the list of file paths for each file in the directory and loops through them putting them into $file
//Everything happens in this loop to parse each file line by line in to $data array and create the .csv file
$line_counter=0;//display the array position line is put in
// pull all fiels with the .txt extention and ignore the rest.
foreach(glob($path.'/*.txt') as $file) {
echo "<br/>====== processing file name ==========<br/>".$file."<br>========================<br/>";
$single_txt_file = fopen($file, "r");
//$first_line = true;
//while (!feof($single_txt_file)) {
//$line = fgets($single_txt_file);// grabs one line of the file that the pointer is pointing to as a string $line
$fullfile=file_get_contents($file);
//Code to split up the file
$title = strtok($fullfile, "\n");
$content = trim(substr($fullfile, strpos($fullfile, "\n") + 1));
$tagName=$tag;
//$processedfile=$title.','.$content.','.$tagName;
//$processedfile=$title.$content.$tagName;
//$processedfile=$title;
//$processedfile+=$content;
//$processedfile+=$tagName;
array_push($processedfile,$title);
array_push($processedfile,$content);
array_push($processedfile,$tag);
echo "<h1> processedfile array print_r contains</h1>";
print_r($processedfile);
echo "<br/><h1>Title=</h1>".$title."<br/><h1>Content=</h1>".$content."<br/><h1>tagName=</h1>".$tagName."<br/>";
// end of test code
$title="";
$content="";
//push processed file into combinedfiles array
array_push($combinedfiles,$processedfile);
//incriment linecounter to keep track of the number of files processed
$line_counter+=1;
//Clear the array for another file.
$processedfile=Array();
}
//Report that we are done combining the files.
echo "<br/><h1>Close single_txt_file </h1>";
fclose($single_txt_file);
//Report out results of processing
echo "<h1>Processed: ".$line_counter." files. <h1>";
echo "<h1>combinedfiles array print_r contains:</h1>";
print_r($combinedfiles);
echo "<br/><h1>End Combining File</h1>";
//Write combinedfiles to a .csv file
echo "<h1>Write combinedfiles into .csv file</h1>";
$filename = $auto_generated.'.csv';
echo "<br/>==================================================<br/>";
echo "name of file csv will be written into:".$filename;
echo "<br/>===================================================<br/>";
$f = fopen($filename, 'w');// open csv file for writing
if ($f === false) {
die('<h1>Error opening the file</h1> ' . $filename);
}
//Convert combinedfiles into array main_data
$main_data=$combinedfiles;
// write each row of main_data to a .csv file generated and stored in filename var.
$counter=0;
foreach ($main_data as $row) {
fputcsv($f,$row);
//fwrite($f,$row);
$counter+=1;
echo "<br/><h1>Row # ".$counter.":</h1><h1>========== print_r contains =============</h1>".print_r([$row])."<h1>================================</h1>";
}
// close the file
fclose($f);
echo "<br/>================ Processing Completed =====================<br/>";
echo "Your result is here:".$filename;
//End writing combinedfiles to .csv file
//Display link to download .csv file
echo "<h1>Display Link to download .csv file.</h1>";
//End of program display message to user.
echo "<h1>Processing Complete</h1>";
//die;//Stop the program here.
//End of new refactor
// rest of the old code
/*
if($first_line){
echo "<br/>============ data[".$line_counter."] =============================</br>";
echo "<h1>$line</h1>";// if this is the first line of the file then this is the title
echo "===============================================================<br/>";
$first_line = false;
}else{
//$line_counter++;
echo "<br/>======== data[".$line_counter."] ================<br/>";
echo $line . "<br>===============================<br/>";//This is the rest of the body of the article one line at a time.
}
$data[] = $line;//puts line into the array $data[]
//if($first_line$){data[]+=",";}
$line_counter++;
}
$arraylength=count($data);
echo "<br/><h1>data contains:".$arraylength."elements. -data[3]=".$data[3]."=print_r=".print_r($data)."<br/></h1>";
$new_data[0] = "";
foreach($data as $da){
if(trim($da) == ""){
}
else{
$new_data[0] .= $da;
}
}
echo "<br/>========= array dump new_data[0] array ========<br/>";
print_r($new_data[0]);
echo "<br/>==============================<br/>";
$main_data = [
['title', 'body','tag'],
['My Title', $new_data[0]]
];
$filename = $auto_generated.'.csv';
echo "<br/>==================================================<br/>";
echo "name of file csv will be written into:".$filename;
echo "<br/>===================================================<br/>";
$f = fopen($filename, 'w');// open csv file for writing
if ($f === false) {
die('Error opening the file ' . $filename);
}
// write each row at a time to a file
$counter=0;
foreach ($main_data as $row) {
fputcsv($f,$row,",");
$counter+=1;
echo "<br/>Row:".$counter.":<br/>=======================<br/>".print_r([$row])."<br/>=================<br>";
}
// close the file
fclose($f);
}
echo "<br/>================ Processing Completed =====================<br/>";
echo "Your result is here:".$filename;
*/
?>
<html>
<body>
<h1>SUCCESS! PLR Importer has converted your files into a combined .csv file.</h1>
<h2>Click the file below to download to your local pc.</h2>
<h3>You can then import them to Drupal or any other Content Management System with a .csv importer.</h3>
<h1>Click this:><a href="<?php echo $filename; ?>"><?php echo $filename; ?></a></h1>
<hr>
<p>Suggested Modules for Drupal to give you .csv importing ability:</p>
<a href="https://drupal.org/project/csv_importer" target="_blank">CSV Importer</a>
<br>
<p>Suggested Plugin for WordPress to give you .csv importing ability:</p>
<a href="hhttps://wordpress.org/plugins/wp-all-import/" target="_blank">Wp All Import</a>
<p>Suggested Drupal Web Hosting:</p>
<a href="https://mtbn.net/drupal-web-hosting/">MTBN.NET Drupal Web Hosting</a>
<br/>
<p>Suggested WordPress Web Hosting:</p>
<a href="https://mtbn.net/wordpress-hosting/">MTBN.NET WordPress Web Hosting</a>
<h2>Instructions for using your .csv file</h2>
<ol>
<li>After you have downloaded your file you may need to edit it in a spreadsheet to add you header row to name your columes so your CMS .csv importer tool can map the data to the correct fields in your Cms</li>
<li>You will need to insert a row at the top of your sheet and add the field names you want your data to import into.</li>
<li>We did this so you were free to name the fields however you needed to for your .csv importer tool.</li>
<li>You will need to read the documentation of the .csv importing tool you are using to understand how to name your header row so it will work.</li>
<li>You will need to read your .csv importer tool documentation to learn how to set it up correctly.</li>
<li>Be sure to save your sheet in .csv format so you keep it in a proper format for your importer tool.</li>
</ol>
<h2>Video tutorials coming soon. Join our git hub project and watch this project for code updates. <a href="https://github.com/waptug/PLR-Importer" target="_blank">PLR-Importer</a></h2>
<h2>This project is looking for angle funding and additional developers to join our team.</h2>
<h2>Thank you to Michael McGinn at GeekZoneHosting.Com and MTBN.NET for being our first angle funder.</h2>
<h2>Development Team:Concept and SaaS application coding by Michael McGinn. Drupal Module Development by Ed Reel.</h2>
</body>
</html>