Hi, My apologies for such a rudimentary request but I am a stone cold newbie at how best to check strings for certain conditions. I have a file upload form with a file element so the user can select the file they wish to upload. I need to isolate the file extension of whatever file they selected and determine if it is ".PDF" regardless of how long the path is to the file. How do I do that? An example path might be: C:\Program Files\xampp\htdocs\wordpress\wp-content\test_file.pdf Many thanks!
Thanks! Where can I find explanations of what these commands are doing so I can learn? Also, I realized I need to validate that the field isn't empty as well. How would I accomplish that?
strtolower() - Converts the string to lowercase substr() - Pops the last character from the string (in this case) strrchr() - Find the last occurrence of a character in a string. Click on the functions in my script. It's all explained there as well. My script checks if the extension is "pdf", so it returns false when it's not, or when the filename is empty. It should work the way it is.
Hi, It doesn't seem to be working. All I get is the error message regardless of the file extension. $pdfu_file = $_REQUEST['pdfu_file']; if (strtolower(substr(strrchr($pdfu_file, '.'), 1)) == 'pdf') { pdfupload_process_upload(); }else{ echo("The file must be a .pdf file."); } } Code (markup):
Is this file supposed to be a user uploaded file? If so you'll need the $_FILES variable. And post your HTML form.
Here's the form <form name="pdfu_form" action="" method="POST" enctype="multipart/form-data"> <div><input type="hidden" name="formtrigger" value="uploaded" /></div> <div><strong>Select a PDF file for uploading:</strong></div> <div><input name="pdfu_file" type="file" size="45" tabindex="1" /> </div> <div>After you successfully upload your file, the notification form will become available. You can test your upload by click the link in the success message.</div> <div><input type="submit" name="pdfu_btn" value="Upload" tabindex="2" /></div> </form> Code (markup): The user is selecting a file from his local computer to upload to a folder on the webserver. HTH.
Try $_FILES['pdfu_file']['name'] instead. Seems like you're using a premade script though... maybe you can set the allowed extensions there at some place.
Got it! You were correct. The $FILES variable was needed. Thanks so much for the assist. I'm learning...I'm learning
if($_FILES['fieldname']['type'] == 'application/pdf){ echo 'File is PDF'; } PHP: * Not sure of the "application/pdf". Remember that uploaded files can have fake extensions. Peace,
right azizny But yours one is the best solution i think because windows can have file name like this too abc.pqr.xyz.pdf so we should consider last index of ".". But yours one is the best one