@@ -127,6 +127,24 @@ class CI_Input
127127 protected $ security ;
128128 protected $ uni ;
129129
130+ /**
131+ * File Upload variables
132+ *
133+ * @var
134+ */
135+ protected $ tempfile ;
136+ protected $ error ;
137+ protected $ filepath ;
138+ protected $ filesize ;
139+ protected $ extension ;
140+ protected $ originalName ;
141+ protected $ originalMimeType ;
142+ protected $ givenName ;
143+ protected $ rawname ;
144+ protected $ isUploadedFile = false ;
145+
146+
147+
130148 // --------------------------------------------------------------------
131149
132150 /**
@@ -246,6 +264,50 @@ public function post($index = null, $xss_clean = false)
246264 return $ this ->_fetch_from_array ($ _POST , $ index , $ xss_clean );
247265 }
248266
267+ /**
268+ * Verify if an item from the POST array exists
269+ *
270+ * @param mixed $index Index for item to be checked from $_POST
271+ * @param bool $xss_clean Whether to apply XSS filtering
272+ * @return bool
273+ */
274+ public function has ($ index = null , $ xss_clean = false )
275+ {
276+ $ exists = $ this ->_fetch_from_array ($ _POST , $ index , $ xss_clean );
277+
278+ if ($ exists ) {
279+ return true ;
280+ }
281+
282+ return false ;
283+ }
284+
285+ /**
286+ * Fetch only items from the POST array
287+ *
288+ * @param mixed $indexes Indexes for item to be fetched from $_POST
289+ * @param bool $xss_clean Whether to apply XSS filtering
290+ * @return mixed
291+ */
292+ public function only (array $ indexes = [], $ xss_clean = false )
293+ {
294+ return $ this ->_fetch_from_array ($ _POST , $ indexes , $ xss_clean );
295+ }
296+
297+ /**
298+ * Fetch all except items given form the POST array
299+ *
300+ * @param array $indexes
301+ * @param bool $xss_clean
302+ * @return void
303+ */
304+ public function except (array $ indexes = [], $ xss_clean = false )
305+ {
306+ $ post = array_diff_key ($ _POST , array_flip ($ indexes ));
307+
308+ return $ this ->_fetch_from_array ($ post , null , $ xss_clean );
309+ }
310+
249311 // --------------------------------------------------------------------
250312
251313 /**
@@ -310,11 +372,11 @@ public function getPost($index, $xss_clean = false)
310372 */
311373 public function file ($ index = '' )
312374 {
313- if ($ index !== '' ) {
375+ if (isset ( $ _FILES [ $ index]) ) {
314376 return $ _FILES [$ index ];
315377 }
316378
317- return '' ;
379+ return [] ;
318380 }
319381
320382 /**
@@ -334,66 +396,208 @@ public function files()
334396 * @param string $file
335397 * @return bool
336398 */
337- public function hasFile ($ file )
399+ public function hasFile ($ file = '' )
338400 {
401+
402+ if ($ file !== '' && isset ($ _FILES [$ file ])) {
403+ $ file = $ _FILES [$ file ];
404+ }
405+
339406 return (empty ($ file ['name ' ]))
340407 ? false
341408 : true ;
342409 }
343410
411+ /**
412+ * Is this file uploaded with a POST request?
413+ *
414+ * hard dependency on the `is_uploaded_file` function.
415+ *
416+ * @return bool
417+ */
418+ public function isUploadedFile ($ fieldname )
419+ {
420+ $ file = $ _FILES [$ fieldname ];
421+ return is_uploaded_file ($ file ['tmp_name ' ]);
422+ }
423+
424+ /**
425+ * Verify uploaded file is true
426+ *
427+ * @return bool
428+ */
429+ public function isValid ()
430+ {
431+ return is_uploaded_file ($ this ->tempfile ) ? true : false ;
432+ }
433+
434+ /**
435+ * Retrieve all file data for easy manipulation
436+ *
437+ * @param array $file
438+ * @param string $name
439+ * @param string $path
440+ * @return CI_Input
441+ */
442+ public function filedata ($ file = [], $ name = null , $ path = '' )
443+ {
444+ if (empty ($ file )) {
445+ return '' ;
446+ }
447+
448+ $ this ->tempfile = $ file ['tmp_name ' ];
449+ $ this ->error = $ file ['error ' ];
450+ $ this ->filepath = ($ path ) ? realpath ($ path ) : realpath (WRITABLEPATH . 'uploads ' );
451+ $ this ->extension = pathinfo ($ file ['name ' ], PATHINFO_EXTENSION );
452+ $ this ->originalName = $ file ['name ' ];
453+ $ this ->originalMimeType = $ file ['type ' ];
454+ $ this ->filesize = $ file ['size ' ];
455+ $ this ->rawname = substr ($ this ->originalName , 0 , strrpos ($ this ->originalName , '. ' ) - strlen ($ this ->extension ));
456+
457+ $ filename = '' ;
458+
459+ if ($ name !== null ) {
460+ $ filename = $ name . '. ' . $ this ->extension ;
461+ }
462+
463+ if ($ name === null ) {
464+ $ filename = random_bytes (2 ) . str_shuffle ('file ' ) . random_bytes (16 );
465+ $ filename = bin2hex ($ filename ) . '. ' . $ this ->extension ;
466+ }
467+
468+ $ this ->givenName = $ filename ;
469+
470+ return $ this ;
471+ }
472+
344473 /**
345474 * Upload file to a given destination
346475 *
347476 * @param mixed $file
348477 * @param string $path
349478 * @param string $name
350- * @return string
479+ * @return CI_Input
351480 */
352- public function storeFile ($ file = [], $ path = '' , $ name = null )
481+ public function upload ($ file = [], $ path = '' , $ name = null )
353482 {
354483 if (empty ($ file )) {
355484 return '' ;
356485 }
357486
358- $ tempfile = $ file ['tmp_name ' ];
359- $ filepath = realpath ($ path );
487+ $ this ->tempfile = $ file ['tmp_name ' ];
488+ $ this ->filepath = ($ path ) ? realpath ($ path ) : realpath (WRITABLEPATH .'uploads ' );
489+ $ this ->extension = pathinfo ($ file ['name ' ], PATHINFO_EXTENSION );
490+ $ this ->originalName = $ file ['name ' ];
491+ $ this ->originalMimeType = $ file ['type ' ];
492+ $ this ->filesize = $ file ['size ' ];
493+ $ this ->rawname = substr ($ this ->originalName , 0 , strrpos ($ this ->originalName , DOT )-strlen ($ this ->extension ));
494+
360495 $ filename = '' ;
361496
362497 if ($ name !== null ) {
498+ $ filename = $ name . '. ' . $ this ->extension ;
499+ }
500+
501+ if ($ name === null ) {
502+ $ filename = random_bytes (2 ) . str_shuffle ('file ' ) . random_bytes (16 );
503+ $ filename = bin2hex ($ filename ) . '. ' . $ this ->extension ;
504+ }
505+
506+ $ this ->givenName = $ filename ;
507+
508+ $ this ->move ($ this ->filepath , $ this ->givenName );
363509
364- $ extension = pathinfo ($ file ['name ' ], PATHINFO_EXTENSION );
510+ return $ this ;
511+ }
365512
366- $ filename = $ name . '. ' . $ extension ;
513+ /**
514+ * Move file from location to destination
515+ *
516+ * @param string $filepath
517+ * @param string $filename
518+ * @return bool
519+ */
520+ public function move ($ filepath , $ filename )
521+ {
522+ $ targetfile = $ filepath . DIRECTORY_SEPARATOR . $ filename ;
367523
368- $ targetfile = $ filepath . $ file ['name ' ];
369- $ targetfile = $ filepath . DIRECTORY_SEPARATOR . $ filename ;
524+ if (move_uploaded_file ($ this ->tempfile , $ targetfile )) {
525+ $ this ->isUploadedFile = true ;
526+ return true ;
370527 }
371528
372- if ($ name === null ) {
529+ return false ;
530+ }
373531
374- $ filename = random_bytes (2 ) . str_shuffle ('file ' ) . random_bytes (16 );
375- $ filename = bin2hex ($ filename );
532+ /**
533+ * Upload file size
534+ *
535+ * @return string
536+ */
537+ public function size ()
538+ {
539+ return $ this ->filesize ;
540+ }
376541
377- $ targetfile = $ filepath . DIRECTORY_SEPARATOR . $ filename ;
378- }
379-
380- move_uploaded_file ($ tempfile , $ targetfile );
381-
382- return $ targetfile ;
542+ /**
543+ * Upload file path
544+ *
545+ * @return string
546+ */
547+ public function path ()
548+ {
549+ return $ this ->filepath ;
383550 }
384551
385552 /**
386- * Is this file uploaded with a POST request?
387- *
388- * hard dependency on the `is_uploaded_file` function.
389- *
390- * @return bool
391- */
392- public function isUploadedFile ($ fieldname )
393- {
394- $ file = $ _FILES [$ fieldname ];
395- return is_uploaded_file ($ file ['tmp_name ' ]);
396- }
553+ * Upload file extension
554+ *
555+ * @return string
556+ */
557+ public function extension ()
558+ {
559+ return $ this ->extension ;
560+ }
561+
562+ /**
563+ * Upload file original name
564+ *
565+ * @return string
566+ */
567+ public function originalName ()
568+ {
569+ return $ this ->originalName ;
570+ }
571+
572+ /**
573+ * Upload file given name
574+ *
575+ * @return string
576+ */
577+ public function filename ()
578+ {
579+ return $ this ->givenName ;
580+ }
581+
582+ /**
583+ * Upload file rawname
584+ *
585+ * @return string
586+ */
587+ public function rawname ()
588+ {
589+ return $ this ->rawname ;
590+ }
591+
592+ /**
593+ * Upload file mime type
594+ *
595+ * @return string
596+ */
597+ public function mimetype ()
598+ {
599+ return $ this ->originalMimeType ;
600+ }
397601
398602 // --------------------------------------------------------------------
399603
@@ -555,6 +759,25 @@ public function set_cookie($name, $value = '', $expire = 0, $domain = '', $path
555759 setcookie ($ prefix . $ name , $ value , $ setcookie_options );
556760 }
557761
762+ /**
763+ * Alias To Method Above
764+ *
765+ * @param string|mixed[] $name Cookie name or an array containing parameters
766+ * @param string $value Cookie value
767+ * @param int $expire Cookie expiration time in seconds
768+ * @param string $domain Cookie domain (e.g.: '.yourdomain.com')
769+ * @param string $path Cookie path (default: '/')
770+ * @param string $prefix Cookie name prefix
771+ * @param bool $secure Whether to only transfer cookies via SSL
772+ * @param bool $httponly Whether to only makes the cookie accessible via HTTP (no javascript)
773+ * @param string|null $samesite The SameSite cookie setting (Possible values: 'Lax', 'Strict', 'None', null, default: null)
774+ * @return void
775+ */
776+ public function setCookie ($ name , $ value = '' , $ expire = 0 , $ domain = '' , $ path = '/ ' , $ prefix = '' , $ secure = null , $ httponly = null , $ samesite = null )
777+ {
778+ $ this ->set_cookie ($ name , $ value , $ expire , $ domain , $ path , $ prefix , $ secure , $ httponly , $ samesite );
779+ }
780+
558781 // --------------------------------------------------------------------
559782
560783 /**
0 commit comments