ballouta Posted June 7, 2009 Share Posted June 7, 2009 Hello Kindly I want to validate an uploaded file before making any proccess on it. 1) The name should be Numbers ONLY (no spaces/letters or signs) 2) The name should be 15 digits 3) The file extension should me .ask ONLY Many thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/161261-solved-checking-file-name/ Share on other sites More sharing options...
Adam Posted June 7, 2009 Share Posted June 7, 2009 You could use the following code to determine they have a valid filename: if (!preg_match('/[\d]{15}\.ask/', $_FILES['input_name']['name'])) { // invalid } else { // valid } Not tested but should work no problems. I'm assuming you know how to do the rest? Quote Link to comment https://forums.phpfreaks.com/topic/161261-solved-checking-file-name/#findComment-850940 Share on other sites More sharing options...
Adam Posted June 7, 2009 Share Posted June 7, 2009 After a quick check I realised the regex is wrong, though this seems to work: \b[\d]{15}\b\.ask Quote Link to comment https://forums.phpfreaks.com/topic/161261-solved-checking-file-name/#findComment-850942 Share on other sites More sharing options...
.josh Posted June 7, 2009 Share Posted June 7, 2009 \d does not need to be wrapped in brackets because it is itself a shorthand character class. But you don't really want to use it anyways, as \d accepts more than strictly 0-9. Better to explicitly use 0-9. Also your original regex probably wasn't working due to you not anchoring boundaries to it. You kind of fixed it by adding word boundary shorthands to it but that just stops someone from entering in "blah232343..." not "blah 234324..." or "blah@234324..." etc... also you put the 2nd one in the wrong place. The anchors you really want are the start and end of line anchors ^...$ /^[0-9]{15}\.ask$/ Quote Link to comment https://forums.phpfreaks.com/topic/161261-solved-checking-file-name/#findComment-850997 Share on other sites More sharing options...
ballouta Posted June 7, 2009 Author Share Posted June 7, 2009 thank you all the first reg mixed with the last post is working correctly Quote Link to comment https://forums.phpfreaks.com/topic/161261-solved-checking-file-name/#findComment-851063 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.