amazon3d Posted September 11, 2007 Share Posted September 11, 2007 Ok so I am working on a file up-loader for a client so they can submit doc and jpeg files to be added to their site. Below is a link to the setup I am using, I wanted to know if anyone knows how I can allow doc and txt files or a list of file types. I tried logical things and well php isn't exactly logical. This is the string to determine file types that can be uploaded. || ($_FILES["file"]["type"] == "image/jpeg") http://www.w3schools.com/php/php_file_upload.asp Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted September 11, 2007 Share Posted September 11, 2007 What exactly seems to be the problem? Quote Link to comment Share on other sites More sharing options...
amazon3d Posted September 12, 2007 Author Share Posted September 12, 2007 I need to know how I can allow doc, docx, txt, and rtf files to be uploaded as well. || ($_FILES["file"]["type"] == "what goes here") Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 12, 2007 Share Posted September 12, 2007 Here is a list of the file types. http://en.wikipedia.org/wiki/Mime_type In this case though why don't you base it off the file extension? Quote Link to comment Share on other sites More sharing options...
amazon3d Posted September 14, 2007 Author Share Posted September 14, 2007 Because I'm not sure how to do that, I just started breaking into php from an editing existing content side, I hope to get into the creation side shortly. Quote Link to comment Share on other sites More sharing options...
amazon3d Posted September 15, 2007 Author Share Posted September 15, 2007 So how would one change it to extension based Quote Link to comment Share on other sites More sharing options...
jitesh Posted September 15, 2007 Share Posted September 15, 2007 <?php $pattern = "/^.+\.(txt|jpg|jpeg|bmp|tif|tiff|gif|png)$/i"; if(preg_match($pattern,$_FILES["file"]["name"])){ echo "Valid File Type."; }else{ echo "Invalid File Type."; } ?> Quote Link to comment Share on other sites More sharing options...
amazon3d Posted September 15, 2007 Author Share Posted September 15, 2007 Thanks, you all have been a great help. Though I have one final question. <?php $pattern = "/^.+\.(txt|doc|docx|rtf|pjpeg|jpg|jpeg|bmp|gif|png)$/i"; { if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } if(preg_match($pattern,$_FILES["file"]["name"])) { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; echo "Invalid file"; } } ?> In this code it is telling it to post both File Exists & Uploaded when I upload a file, rather then saying the file already exists or just uploaded. How would I go about correcting that? Thanks in advance. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 15, 2007 Share Posted September 15, 2007 if the file exists to echo the file name plus "Already exists" Quote Link to comment Share on other sites More sharing options...
amazon3d Posted September 15, 2007 Author Share Posted September 15, 2007 Thats what it is doing now but it is allowing it to overwrite the existing file. Quote Link to comment Share on other sites More sharing options...
sayedsohail Posted September 15, 2007 Share Posted September 15, 2007 I think if u just add return; or exit; inside the if file exists statement, should stop executing the next steps of your script. if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; return; } <?php $pattern = "/^.+\.(txt|doc|docx|rtf|pjpeg|jpg|jpeg|bmp|gif|png)$/i"; { if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } if(preg_match($pattern,$_FILES["file"]["name"])) { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; echo "Invalid file"; } } ?> Quote Link to comment Share on other sites More sharing options...
amazon3d Posted September 15, 2007 Author Share Posted September 15, 2007 Thank you ever so much, works like a charm, blocks non allowed file types, uploads allowed file types, and doesn't overwrite existing files! Thanks again! Quote Link to comment Share on other sites More sharing options...
sayedsohail Posted September 15, 2007 Share Posted September 15, 2007 You are most welcome. can you please check (solved) your thread. Thanks sohail Quote Link to comment 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.