how send $_FILES to other function?
<?php
function save(how get $_FILES["img"] ??)
{
//$img = $_FILES["img"]; How get $_FILES["img"] ??
$user_file = $_FILES['img']['name'];
$file_temp = $_FILES['img']['tmp_name'];
$new = "new/";
move_uploaded_file($file_temp, $new .$user_file.'');
echo "<br><b>OK<b>";
}
if(isset($_POST['SEND'])){
save($_FILES["img"]);
}
?>
The actual variable is $_FILES
; $_FILES['img']
is a value stored within that array. You can't pass that value to a function and store it in something named $_FILES['img']
, but you wouldn't really want to. Name it something like $img
, and use that:
function save($img)
{
$user_file = $img['name'];
$file_temp = $img['tmp_name'];
$new = "new/";
move_uploaded_file($file_temp, $new .$user_file.'');
echo "<br><b>OK<b>";
}
If you do a
var_dump($_FILES);
you can see all kinds of good information that is stored in the $_FILES array, including the uploaded file name and the temp location.
$_FILES - is a usual array which structure you can view by var_dump($_FILES);
You can work with it as with usual array.
Files is a (super) global. You can reference it from within a function just fine.
If you want to factor out some functionality, and invoke a function to act on just some part of the $_FILES superglobal array, you can do something like this:
function save($fileKey){
$file = $_FILES[$fileKey];
move_uploaded_file($file['tmp_name'],...);
}
save('img');
save('img2');
or
function save($arrayFile){
move_uploaded_file($arrayFile['tmp_name'],...);
}
save($_FILES['img');
save($_FILES['img2']);