SRL Archive - Web Development

How do I make this $_FILES PHP Array?


grats

2012-05-31 02:25:48

Well I have an array of 0, 1, 2, 3, 4 files (5 files total) that people can upload

[PHP]if ((($_FILES["attach"]["type"] == "application/pdf")
|| ($_FILES["attach"]["type"] == "application/msword")
|| ($_FILES["attach"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document"))
&& ($_FILES["attach"]["size"] < 8388608))[/PHP]

I am not sure how to use the foreach with $_FILES (don't link me, I already tried some stuff I read on php.net etc and failed to integrate it with my script) I was just wondering how to check the entire array with that instead of doing something like:
[PHP]if ((($_FILES["attach"]["type"][0] == "application/pdf")
|| ($_FILES["attach"]["type"][0] == "application/msword")
|| ($_FILES["attach"]["type"][0] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document"))
&& ($_FILES["attach"]["size"][0] < 8388608))[/PHP]
for 0.. and then obviously adding 1,2,3,4 in there too

thanks

Main

2012-05-31 02:29:12

send kyle a pm:)

grats

2012-05-31 02:33:39

Thanks, I kind of got the echo to learn it but can't figure out the file checking lol, will PM him

Kyle Undefined

2012-05-31 09:15:37

So you want to check the entire $_Files array for certain types of documents? Something like this should suffice, granted, you'll have to edit the ['tmp_name'] and the like to fit your needs.

Hope this helps. :)

[PHP]
<?php
$allowedExtensions = array("pdf", "csv", "doc", "rtf");
foreach ($_FILES as $file) {
if ($file['tmp_name'] > '') {
if (!in_array(end(explode(".", strtolower($file['name']))), $allowedExtensions)) {
die($file['name'].' is an invalid file type!');
}
}
}
?>
[/PHP]

grats

2012-05-31 19:23:13

So this only allows .pdf .csv .doc .rtf

and it outputs the files name (not its temp name) nad "is an invalid file type?

Also I'm not moving the files, I'm just uploading, attaching, emailing, and letting them delete as the tmp files usually do

will that work with this? and these do not need mime types so I can add .docx instead of that huge mime type I have in mine?

Kyle Undefined

2012-05-31 21:35:52

Yes, that just checks the file extension. You can also add a file size check and if it passes both of those, do what you want to do with it :)