jibo Posted January 27, 2008 Share Posted January 27, 2008 hey i found script for uploading files but wasnt 100% clear and im a completly brain dead wen it comes to php loli understand most but to upload specific file types it says if (!($uploaded_type=="image/gif")) {echo "You may only upload GIF files.<br>";$ok=0;} as an example to only allow the upload of .gif filesif i want to allow other things as an upload ie mp3 or whatever else would i say if (!($uploaded_type=="audio/mp3")) { or would i use a differnet word oppose to "audio" (same with .zip/.rar files and files such as .avi or mpg? is "audio" the correct word to use?) also if i want to allow upload of of mp3 and wma would i do it like this? if (!($uploaded_type=="audio/mp3")) {echo "You may only upload mp3 & wma files.<br>";$ok=0;}if (!($uploaded_type=="audio/wma")) {echo "You may only upload mp3 & wma wma files.<br>";$ok=0;} or would it be done in the same code like if (!($uploaded_type=="audio/mp3,wma")) {echo "You may only upload mp3 & wma files.<br>";$ok=0;} but as i said im rubbish at php so i dont know if commas are used or not its just a guess :)thanks in advance for any help Link to comment Share on other sites More sharing options...
kishou Posted January 27, 2008 Share Posted January 27, 2008 you could use the OR statement. also to see the type just right click and it says like image/gif. Link to comment Share on other sites More sharing options...
killboy Posted January 27, 2008 Share Posted January 27, 2008 I'm not sure about the "audio/mp3" part, but as for the mp3, wma if statement, you just need to put a && if (!($uploaded_type=="audio/mp3") && !($uploaded_type=="audio/wma")) {echo "You may only upload mp3 & wma files.<br>";$ok=0;} Link to comment Share on other sites More sharing options...
jibo Posted January 27, 2008 Author Share Posted January 27, 2008 and for more file types jus do the same thing right?ie if (!($uploaded_type=="audio/mp3") && !($uploaded_type=="audio/wma") && !($uploaded_type=="image/gif")) {echo "You may only upload mp3, wma & gif files.<br>";$ok=0;} to allow someone to upload all file types? instead of doing a seperate if statement for each file type is tht correct? Link to comment Share on other sites More sharing options...
killboy Posted January 27, 2008 Share Posted January 27, 2008 Yep.Oh, just one thing: I'm not quite sure about !($uploaded_type=="[type]") part; I would change it for ($uploaded_type!="[type]") Being the IF statement: if (($uploaded_type!="audio/mp3") && ($uploaded_type!="audio/wma") && ($uploaded_type!="image/gif")) {echo "You may only upload mp3, wma & gif files.<br>";$ok=0;} Although, I've never done it the other way... Link to comment Share on other sites More sharing options...
jibo Posted January 27, 2008 Author Share Posted January 27, 2008 nice 1 mate... all i need no its the correct wording to recognise file types for uploadin as in!($uploaded_type=="image/gif")"image" for an image file"text" for a txt file lik php or w/eso i assume an audio file would b "audio" and perhaps .zip files maybe simply just "file" but im not sure and have no idea what it would be for videos lol so if sum1 can help please Link to comment Share on other sites More sharing options...
killboy Posted January 27, 2008 Share Posted January 27, 2008 I've got it. It's called Multimedia MIME.http://www.w3schools.com/media/media_mimer...sp?output=printThe mp3 stands for "audio/mpeg" Link to comment Share on other sites More sharing options...
jibo Posted January 27, 2008 Author Share Posted January 27, 2008 top lad thnks a lot pal... thers loads ther lol i found all the 1s i need except theres 1 missing..... which is wma so ill just test it as audio/wma if it doesnt work ill kepp looking, again big thanks, and nice work :) Link to comment Share on other sites More sharing options...
killboy Posted January 27, 2008 Share Posted January 27, 2008 No prob, it's always nice to help Link to comment Share on other sites More sharing options...
jibo Posted January 27, 2008 Author Share Posted January 27, 2008 infact ive just had a thought, something a bit better i could use... dont mean to ask too much lol but the upload code ive gt is... <?php$target = "upload/";$target = $target . basename( $_FILES['uploaded']['name']);$ok=1;if (!($uploaded_type=="audio/mpeg") && (!($uploaded_type=="video/quicktime")) {echo "You may only upload mp3 and quicktime videos files.<br>";$ok=0;}if ($ok==0){Echo "Sorry your file was not uploaded";}else{if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)){echo "The file ".basename( $_FILES['uploadedfile']['name']). " has been uploaded";}else{echo "Sorry, there was a problem uploading your file.";}}?> is there a way it can be modded so the uploader can choose a destination that they would like to upload it to... ie a drop down box to decide for example if upping a quiktime vide they can place in a videos folder or an mp3 in a music folder? just to cat up rather than moving the files about once uploaded? but doing this id assume id need to edit the html with an other field is this correct? the html code i currently have is <form enctype="multipart/form-data" action="upload.php" method="POST">Please choose a file: <input name="uploaded" type="file" /><br /><input type="submit" value="Upload" /></form> thanks Link to comment Share on other sites More sharing options...
jibo Posted January 27, 2008 Author Share Posted January 27, 2008 oh yer i found the mimetype for wma and wmv "theyre not on the list u gave me"wma is - audio/x-ms-wmawmv is - video/x-ms-wmvif anybody wanted em Link to comment Share on other sites More sharing options...
jhecht Posted January 27, 2008 Share Posted January 27, 2008 just for future reference, you could take the extension of the file uploaded and then check that against an array of allowed extensions. That would save you time and you wouldn't have to deal with MIME types. Link to comment Share on other sites More sharing options...
killboy Posted January 28, 2008 Share Posted January 28, 2008 just for future reference, you could take the extension of the file uploaded and then check that against an array of allowed extensions. That would save you time and you wouldn't have to deal with MIME types.Amen! Link to comment Share on other sites More sharing options...
justsomeguy Posted January 28, 2008 Share Posted January 28, 2008 I agree with Jhecht, different platforms might send different MIME types but an extension is an extension. You can get the extension like this:$ext = array_pop(explode(".", $filename)); Link to comment Share on other sites More sharing options...
jibo Posted January 31, 2008 Author Share Posted January 31, 2008 as jhect said, "for furture reference" does that mean i cant use his method in that script? I cant use "$ext = array_pop(explode(".", $filename)); "?????? and does anyone have an answer for my question in post 10, thanks Link to comment Share on other sites More sharing options...
jhecht Posted January 31, 2008 Share Posted January 31, 2008 Yes, there is. It would have to be from the same form that the user is uploading the file from, from there you would just append the folder name onto the path and that would do that. And who's method? If you mean "my" method of checking the extension instead of checking the mime type, then yes, feel free. Justsomeguy gave you code to get the extension of the file. All you would be required to do from there is to make an array in a config file somewhere that looks something like this: <?php $allowedExt = array('jpg','jpeg','gif','mp3','insert other extensions here'); // Your script starts now after whatever else may be going on. //$_POST data assumed submited, and justsomeguys' line expected before this; if(in_array($ext,$allowedExt)){ //Upload file }else{ //file specified is not uploadable }?> Link to comment Share on other sites More sharing options...
jibo Posted January 31, 2008 Author Share Posted January 31, 2008 It would have to be from the same form that the user is uploading the file from, from there you would just append the folder name onto the path and that would do thatbut this doesnt give the person an option to choose where to upload the file..... thats what im lookin for like so they can choose to upload the file whether it be a mp3 or avi, as there all going to be in a seperaate folder, like domain.com/upload/music and domain.com/upload/movies so it can be directly uploaded into the correct folder rather than everything going into 1 place like domain.com/upload..... sorry i think i blabbered on a bit there loland im hopeless with php so have no iea how to put it together lol Link to comment Share on other sites More sharing options...
jhecht Posted January 31, 2008 Share Posted January 31, 2008 ... Yeah, it does. <form action="otherpage.php" method="post"><!-- theres one other attribute i can't remember -->Choose a File <input type="file" name="file" /><br />Choose a destination folder<select name="dest"> <option value="music">Music</option> <option value="movie">Movie</option> <option value="picture">Picture</option></select><input type="submit" value="submit" /> Otherpage.php <?php if(sizeof($_POST)>0){ $file = $_POST['file']; $dest = $_POST['dest']; $newFileName = dirname(__FILE__).'/uploads/.'$dest.'/'.$file['name']; //Keep in mind you should check to make sure that $dest is set, and so on and so forth. if(!file_exists($newFileName)){ if(move_uploaded_file($file['tmp_filename'],$newFileName)){ //Your file was uploaded , etc etc }else{ //Something baad happened } }else{ //Cannot upload file because a file with the same name already exists. } }?> Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.