steve-t Posted November 30, 2006 Share Posted November 30, 2006 Further to an earlier thread that was unresolved, I need a script that checks if an uploaded file has either a .PDF or .PPT file extension and if not displays an error message.Any help will be VERY much appreciated! :) Link to comment https://forums.phpfreaks.com/topic/29005-uploading-files/ Share on other sites More sharing options...
Daniel0 Posted November 30, 2006 Share Posted November 30, 2006 [code]<?php$arr = explode('.',$filename);$ext = $arr[count($arr)-1];if(in_array(strtolower($ext),array('pdf','pdt')){ // do stuff}else { echo "Invalid filetype";}?>[/code]This should work. Link to comment https://forums.phpfreaks.com/topic/29005-uploading-files/#findComment-132873 Share on other sites More sharing options...
The Little Guy Posted November 30, 2006 Share Posted November 30, 2006 You will need to change name to the value of name from your form.[CODE]<?phpif($_FILES['name']['type'] == 'application/PDF' || $_FILES['name']['type'] == 'application/PPT'){ //Upload the files}else{ //Don't upload the files}?>[/CODE] Link to comment https://forums.phpfreaks.com/topic/29005-uploading-files/#findComment-132877 Share on other sites More sharing options...
craygo Posted November 30, 2006 Share Posted November 30, 2006 you could put the extensions into an array and some variables[code]$limit_ext = "yes"; //do you want to limit the extensions of files uploaded$ext_count = "2"; //total number of extensions in array below$extensions = array(".pdf", ".ppt"); //List extensions you want files uploaded to be[/code]Then check if they fall into the array[code]$ext = strrchr($file_name,'.');if (($limit_ext == "yes") && (!in_array($ext,$extensions))) {echo "File is wrong type";}else{// place your code here}?>[/code]Ray Link to comment https://forums.phpfreaks.com/topic/29005-uploading-files/#findComment-132880 Share on other sites More sharing options...
Daniel0 Posted November 30, 2006 Share Posted November 30, 2006 The Little Guy: It is different what mime-type a browser will send. That might work in some browsers, but maybe not in all. Link to comment https://forums.phpfreaks.com/topic/29005-uploading-files/#findComment-132896 Share on other sites More sharing options...
The Little Guy Posted November 30, 2006 Share Posted November 30, 2006 I thought that was a server thing, but after looking at the manual, I guess not, maybe this:[CODE]<?phpfunction file_extension($filename){ $path_info = pathinfo($filename); return $path_info['extension'];}if(file_extension($_FILES['name'])=="PDF" || file_extension($_FILES['name'])=="PPT"){//upload file}else{//dont upload}?>[/CODE] Link to comment https://forums.phpfreaks.com/topic/29005-uploading-files/#findComment-132912 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.