Please tell me there is a tag <input name = "upload []" type = "file" multiple = "multiple" />
How to make a php condition if input has a file.
Thanks.
Asked
Active
Viewed 116 times
0

Владислав Самохин
- 149
- 6
-
Something like `if(!empty($_METHOD['upload'])){` – user3783243 Nov 19 '18 at 23:23
-
I suggest you read http://php.net/manual/en/features.file-upload.post-method.php – Nick Nov 20 '18 at 00:16
-
@user3783243 dont work... – Владислав Самохин Nov 20 '18 at 19:10
-
Please add your usage, and what behavior you saw with it? – user3783243 Nov 20 '18 at 19:33
-
@user3783243 if (!empty($_FILES['upload']['name'][0])) { – Владислав Самохин Nov 20 '18 at 20:16
-
...and what happens? Error, notice, other? Is `FILES` populated? Also, is the question `if input has a file`, or `if input is a PHP file`? – user3783243 Nov 20 '18 at 20:21
1 Answers
0
Somthing like this would do the trick
<?php
if(isset($_FILES['upload']['tmp_name']))
{
// Number of uploaded files
$num_files = count($_FILES['upload']['tmp_name']);
/** loop through the array of files ***/
for($i=0; $i < $num_files;$i++)
{
// check if there is a file in the array
if(!is_uploaded_file($_FILES['upload']['tmp_name'][$i]))
{
$messages[] = 'No file uploaded';
}
else
{
// copy the file to the specified dir
if(@copy($_FILES['upload']['tmp_name'][$i],$upload_dir.'/'.$_FILES['upload']['name'][$i]))
{
/*** give praise and thanks to the php gods ***/
$messages[] = $_FILES['upload']['name'][$i].' uploaded';
}
else
{
/*** an error message ***/
$messages[] = 'Uploading '.$_FILES['upload']['name'][$i].' Failed';
}
}
}
}
?>

Patrick Simard
- 2,294
- 3
- 24
- 38
-
Why `copy` and not http://php.net/manual/en/function.move-uploaded-file.php? I'd also recommend not using `@` for production code. If you have errors you should handle it appropriately – user3783243 Nov 20 '18 at 04:11