Skip to content
This repository was archived by the owner on Nov 18, 2025. It is now read-only.

Commit b5f5818

Browse files
committed
✨ add file, hasFile, storeFile methods
Signed-off-by: otengkwame <[email protected]>
1 parent abbc69f commit b5f5818

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

framework/core/Input.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,88 @@ public function getPost($index, $xss_clean = false)
302302
return $this->get_post($index, $xss_clean);
303303
}
304304

305+
/**
306+
* Handle $_FILES
307+
*
308+
* @param string $index
309+
* @return array|string
310+
*/
311+
public function file($index = '')
312+
{
313+
if ($index !== '') {
314+
return isset($_FILES[$index]);
315+
}
316+
317+
return isset($_FILES);
318+
}
319+
320+
/**
321+
* Check if file to upload is not empty
322+
*
323+
* @param string $file
324+
* @return bool
325+
*/
326+
public function hasFile($file)
327+
{
328+
return (empty($file['name']))
329+
? true
330+
: false;
331+
}
332+
333+
/**
334+
* Upload file to a given destination
335+
*
336+
* @param mixed $file
337+
* @param string $path
338+
* @param string $name
339+
* @return string
340+
*/
341+
public function storeFile($file, $path = '', $name = null)
342+
{
343+
if (empty($file)) {
344+
return '';
345+
}
346+
347+
$tempfile = $file['tmp_name'];
348+
$filepath = realpath($path);
349+
$filename = '';
350+
351+
if ($name !== null) {
352+
353+
$extension = pathinfo($file['name'], PATHINFO_EXTENSION);
354+
355+
$filename = $name . '.' . $extension;
356+
357+
$targetfile = $filepath . $file['name'];
358+
$targetfile = $filepath . DIRECTORY_SEPARATOR . $filename;
359+
}
360+
361+
if ($name === null) {
362+
363+
$filename = random_bytes(2) . str_shuffle('file') . random_bytes(16);
364+
$filename = bin2hex($filename);
365+
366+
$targetfile = $filepath . DIRECTORY_SEPARATOR . $filename;
367+
}
368+
369+
move_uploaded_file($tempfile, $targetfile);
370+
371+
return $targetfile;
372+
}
373+
374+
/**
375+
* Is this file uploaded with a POST request?
376+
*
377+
* hard dependency on the `is_uploaded_file` function.
378+
*
379+
* @return bool
380+
*/
381+
public function isUploadedFile($fieldname)
382+
{
383+
$file = $_FILES[$fieldname];
384+
return is_uploaded_file($file['tmp_name']);
385+
}
386+
305387
// --------------------------------------------------------------------
306388

307389
/**

0 commit comments

Comments
 (0)