6
6
*/
7
7
8
8
/************************************************************************/
9
- /* ajaxCRUD.class.php v8.82 */
9
+ /* ajaxCRUD.class.php v8.9 */
10
10
/* =========================== */
11
11
/* Copyright (c) 2013 by Loud Canvas Media (arts@loudcanvas.com) */
12
12
/* http://www.ajaxcrud.com by http://www.loudcanvas.com */
@@ -262,7 +262,7 @@ class ajaxCRUD{
262
262
263
263
//destination folder to be set for a particular field that allows uploading of files. the array is set as $field_name => $destination_folder
264
264
var $ file_uploads = array ();
265
- var $ file_upload_info = array (); //array[$field_name]['destination_folder'] and array[$field_name]['relative_folder ']
265
+ var $ file_upload_info = array (); //array[$field_name]['destination_folder'], array[$field_name]['relative_folder'], and array[$field_name]['permittedFileExts ']
266
266
var $ filename_append_field = "" ;
267
267
268
268
//array dictating that "dropdown" fields do not show dropdown (but text editor) on edit (format: array[field] = true/false);
@@ -727,12 +727,18 @@ function primaryKeyNotAutoIncrement(){
727
727
$ this ->primaryKeyAutoIncrement = false ;
728
728
}
729
729
730
- function setFileUpload ($ field_name , $ destination_folder , $ relative_folder = "" ){
730
+ //the forth optional param (permittedFileExts) was added in v8.9; it is an ARRAY of permitted file extensions allowed for upload; e.g. array("png", "jpg")
731
+ function setFileUpload ($ field_name , $ destination_folder , $ relative_folder = "" , $ permittedFileExts = "" ){
731
732
//put values into array
732
733
$ this ->file_uploads [] = $ field_name ;
733
734
$ this ->file_upload_info [$ field_name ]['destination_folder ' ] = $ destination_folder ;
734
735
$ this ->file_upload_info [$ field_name ]['relative_folder ' ] = $ relative_folder ;
735
736
737
+ //added in v8.9
738
+ if (is_array ($ permittedFileExts )){
739
+ $ this ->file_upload_info [$ field_name ]['permittedFileExts ' ] = $ permittedFileExts ;
740
+ }
741
+
736
742
//the filenames that are saved are not editable
737
743
$ this ->disallowEdit ($ field_name );
738
744
@@ -1014,10 +1020,15 @@ function doAction($action){
1014
1020
1015
1021
if ($ uploads_on ){
1016
1022
foreach ($ this ->file_uploads as $ field_name ){
1017
- $ file_dest = $ this ->file_upload_info [$ field_name ][destination_folder];
1023
+ $ file_dest = $ this ->file_upload_info [$ field_name ]['destination_folder ' ];
1024
+
1025
+ $ allowedExts = "" ;
1026
+ if (isset ($ this ->file_upload_info [$ field_name ]['permittedFileExts ' ])){
1027
+ $ allowedExts = $ this ->file_upload_info [$ field_name ]['permittedFileExts ' ];
1028
+ }
1018
1029
1019
1030
if ($ _FILES [$ field_name ]['name ' ] != '' ){
1020
- $ this ->uploadFile ($ insert_id , $ field_name , $ file_dest );
1031
+ $ this ->uploadFile ($ insert_id , $ field_name , $ file_dest, $ allowedExts );
1021
1032
}
1022
1033
}
1023
1034
}
@@ -1070,15 +1081,20 @@ function doAction($action){
1070
1081
if ($ action == 'upload ' && $ _REQUEST ['field_name ' ] && $ _REQUEST ['id ' ] != '' && is_array ($ this ->file_uploads ) && in_array ($ _REQUEST ['field_name ' ],$ this ->file_uploads )){
1071
1082
$ update_id = $ _REQUEST ['id ' ];
1072
1083
$ file_field = $ _REQUEST ['field_name ' ];
1073
- $ upload_folder = $ this ->file_upload_info [$ file_field ][destination_folder];
1084
+ $ upload_folder = $ this ->file_upload_info [$ file_field ][' destination_folder ' ];
1074
1085
1075
- $ success = $ this ->uploadFile ($ update_id , $ file_field , $ upload_folder );
1086
+ $ allowedExts = "" ;
1087
+ if (isset ($ this ->file_upload_info [$ file_field ]['permittedFileExts ' ])){
1088
+ $ allowedExts = $ this ->file_upload_info [$ file_field ]['permittedFileExts ' ];
1089
+ }
1090
+
1091
+ $ success = $ this ->uploadFile ($ update_id , $ file_field , $ upload_folder , $ allowedExts );
1076
1092
1077
1093
if ($ success ){
1078
1094
$ report_msg [] = "File Uploaded Sucessfully. " ;
1079
1095
}
1080
1096
else {
1081
- $ error_msg [] = "There was an error uploading your file. Check permissions of the destination directory (make sure is set to 777) . " ;
1097
+ // $error_msg[] = "There was an error uploading your file.";
1082
1098
}
1083
1099
1084
1100
}//action = upload
@@ -1092,7 +1108,7 @@ function doAction($action){
1092
1108
$ success = qr ("UPDATE $ this ->db_table SET $ file_field = \"\" WHERE $ this ->db_table_pk = $ delete_id " );
1093
1109
1094
1110
if ($ success ){
1095
- $ file_dest = $ this ->file_upload_info [$ file_field ][destination_folder];
1111
+ $ file_dest = $ this ->file_upload_info [$ file_field ][' destination_folder ' ];
1096
1112
1097
1113
unlink ($ file_dest . $ filename );
1098
1114
$ report_msg [] = "File Deleted Sucessfully. " ;
@@ -1191,12 +1207,22 @@ function createCSVOutput() {
1191
1207
}
1192
1208
1193
1209
//a file must have been "sent"/posted for this to work
1194
- function uploadFile ($ row_id , $ file_field , $ upload_folder ){
1210
+ function uploadFile ($ row_id , $ file_field , $ upload_folder , $ allowedExts = "" ){
1211
+ global $ report_msg , $ error_msg ;
1212
+
1195
1213
@$ fileName = $ _FILES [$ file_field ]['name ' ];
1196
1214
@$ tmpName = $ _FILES [$ file_field ]['tmp_name ' ];
1197
1215
@$ fileSize = $ _FILES [$ file_field ]['size ' ];
1198
1216
@$ fileType = $ _FILES [$ file_field ]['type ' ];
1199
1217
1218
+ if (is_array ($ allowedExts )){
1219
+ $ fileExt = strtolower (pathinfo ($ fileName , PATHINFO_EXTENSION )); //gets file ext (lowercase)
1220
+ if ( !in_array ($ fileExt , $ allowedExts )){
1221
+ $ error_msg [] = "Upload failed. Selected file was extention <b>. {$ fileExt }</b> but this is not an permitted file extension. " ;
1222
+ return false ;
1223
+ }
1224
+ }
1225
+
1200
1226
$ new_filename = make_filename_safe ($ fileName );
1201
1227
if ($ this ->filename_append_field != "" ){
1202
1228
if ($ _REQUEST [$ this ->filename_append_field ] != '' ){
@@ -1233,16 +1259,13 @@ function uploadFile($row_id, $file_field, $upload_folder){
1233
1259
call_user_func ($ this ->onFileUploadExecuteCallBackFunction , $ file_info_array );
1234
1260
}
1235
1261
1236
- }
1237
-
1238
- if ($ update_success ){
1239
- return true ;
1240
- //$report_msg[] = "File Uploaded.";
1262
+ if ($ update_success ) return true ;
1241
1263
}
1242
1264
else {
1243
- return false ;
1244
- //$error_msg[] = "There was an error uploading your file (or none was selected).";
1245
- }
1265
+ $ error_msg [] = "There was an error uploading your file. Check permissions of the destination directory (make sure is set to 777). " ;
1266
+ }
1267
+
1268
+ return false ;
1246
1269
}
1247
1270
1248
1271
function showTable (){
0 commit comments