<?php if(isset($_POST['Submit'])) { $allowed = array("image/gif", "video/wmv", "video/mpg", "video/wmv", "video/x-ms-wmv", "image/jpeg", "image/jpg"); if(in_array($_FILES['videofile']['type'], $allowed)) { @copy ($_FILES['videofile']['tmp_name'], "/home/keeptouch/public_html/video/video_up/".$_FILES['videofile']['name']) or die ("Could not copy"); echo "Name: ".$_FILES['videofile']['name']."<br/>"; echo "Size: ".$_FILES['videofile']['size']."<br/>"; echo "Type: ".$_FILES['videofile']['type']."<br/>"; echo "Copy Done...."; } else { echo "Could Not Copy, Wrong Filetype (".$_FILES['videofile']['type'].")<br>"; } } ?> <form name="form1" method="post" action="" enctype="multipart/form-data"> <input type="file" name="videofile"> <br> <input type="submit" name="Submit" value="Submit"> </form> PHP: Anyone can tell me why i cant upload my wmv and mpg and flv files ?
What error do you get? The error it shows if the file type of the submitted file isn't in the array of allowed file types? - How big are the files? - Use move_uploaded_file() instead of copy() - echo $_FILES['videofile']['error'] and see what you get.
1. You should use move_uploaded_file instead of copy. 2. You can't really trust the mime type in $_FILES['videofile']['type']. A far better test is to use the mime_content_type function: if (in_array(mime_content_type($_FILES['videofile']['tmp_name']), $allowed)) { PHP: Try that and see how it goes.
Fatal error: Call to undefined function: mime_content_type() in /home/keeptouch/domains/keeptouch.net/public_html/video/fileup.php on line 9
Argh! that sometimes happens. mime_content_type is deprecated, and not available on all systems. The replacement, FileInfo, is a PECL library and also not available on all systems. But we can try one more thing. If you're on a non-Windows system, and your PHP is set up to allow you to use the exec function, we can use the file command to do the exact same thing: $mime_type = exec("file -i " . $_FILES['videofile']['tmp_name']); if (in_array($mime_type, $allowed)) { PHP: Now...depending how your php is set up, you might need to provide the full path to the file command, so you might need the call to look something like this: $mime_type = exec("/usr/bin/file -i " . $_FILES['videofile']['tmp_name']); PHP: If you have shell access, you can get the full path by running which file. Otherwise, you'll need to ask your host.
ah yeah, it's all coming back to me...wmv and swf (and maybe a couple of others) don't have mime-type magic numbers. The only way to check these is via the file extension, AFTER verifying that it's not something else. Just change the test to this: if (in_array($mime_type, $allowed) || preg_match('/\.wmv$/i',$_FILES['videofile']['tmp_name']) != 0) { PHP: If you need to do the extension-only check for several types, use this regular expression instead (add/remove extensions as desired): '/\.((swf)|(wmv)|(flv))$/i' PHP:
If I'm not mistaken, the temp_name has another temporary extension. Not the original one. And you might as well want to try getimagesize(), I've heard it works other media types than images too. list(,, $mime) = getimagesize($_FILES['videofile']['tmp_name']); PHP: Never fully tested it with other media types though. But it's worth a try I guess.
Yup, you're right. For extension checking it should be $_FILES['userfile']['name'] Code (markup): Also correct about getimagesize(). Better to use that if you're only checking image formats. Unfortinately, wmv isn't covered: IMAGETYPE_GIF IMAGETYPE_JPEG IMAGETYPE_PNG IMAGETYPE_SWF IMAGETYPE_PSD IMAGETYPE_BMP IMAGETYPE_WBMP IMAGETYPE_XBM IMAGETYPE_TIFF_II IMAGETYPE_TIFF_MM IMAGETYPE_IFF IMAGETYPE_JB2 IMAGETYPE_JPC IMAGETYPE_JP2 IMAGETYPE_JPX IMAGETYPE_SWC Code (markup):
It'll work with a minor change as per nico_swd's point regarding tmp_name. Here's the whole block of code: $allowed = array("image/gif", "video/mpg", "image/jpeg", "image/jpg"); $mime_type = exec("file -i " . $_FILES['videofile']['tmp_name']); if (in_array($mime_type, $allowed) || preg_match('/\.wmv$/i',$_FILES['videofile']['name']) != 0) { // you're good to go } else { // invalid file } PHP: Note that nico_swd's recommendation to use getimagesize is a good one (it's a little faster than execing "file"), but the value returned in the field isn't a mime type but an integer constant, and it also doesn't handle the mpg type, so I stayed with "file" here.
but i tried you see my code it wont be able to work even if i put the same <?php error_reporting(E_ALL); if(isset($_POST['Submit'])) { $allowed = array("image/gif", "video/mpg", "image/jpeg", "image/jpg");$mime_type = exec("file -i " . $_FILES['videofile']['tmp_name']); if (in_array($mime_type, $allowed) || preg_match('/\.wmv$/i',$_FILES['videofile']['name']) != 0) { @copy ($_FILES['videofile']['tmp_name'], "/home/keeptouch/public_html/video/video_up/".$_FILES['videofile']['name']) or die ("Could not copy"); echo mime_content_type($_FILES['videofile']['tmp_name']); echo "Name: ".$_FILES['videofile']['name']."<br/>"; echo "Size: ".$_FILES['videofile']['size']."<br/>"; echo "Type: ".$_FILES['videofile']['type']."<br/>"; echo "Copy Done...."; } else { echo "Could Not Copy, Wrong Filetype (".$_FILES['videofile']['type'].")<br>"; } } ?> <form name="form1" method="post" action="" enctype="multipart/form-data"> <input type="file" name="videofile"> <br> <input type="submit" name="Submit" value="Submit"> </form> PHP:
Ah, my fault for one thing; "file -i" should be "file -b -i" Also, you might want to add these types to your mime list (I ran across this just now while testing mpegs): video/x-mpeg video/mpeg Lastly, consider using move_uploaded_file instead of copy. Below is your code with these few modifications. Just tested, and it worked fine for me, uploading all valid types, and rejecting invalid types (I made an alias for your directory; make sure you have 777 permissions on your target copy directory): <?php error_reporting(E_ALL); if(isset($_POST['Submit'])) { $allowed = array("image/gif", "video/mpg","video/mpeg", "video/x-mpeg","image/jpeg", "image/jpg");$mime_type = exec("file -b -i " . $_FILES['videofile']['tmp_name']); if (in_array($mime_type, $allowed) || preg_match('/\.wmv$/i',$_FILES['videofile']['name']) != 0) { move_uploaded_file($_FILES['videofile']['tmp_name'], "/home/keeptouch/public_html/video/video_up/".$_FILES['videofile']['name']) or die ("Could not copy"); echo "Copy Done...."; } else { echo "Could Not Copy, Wrong Filetype (".$_FILES['videofile']['type'].")<br>"; } } ?> <form name="form1" method="post" action="" enctype="multipart/form-data"> <input type="file" name="videofile"> <br> <input type="submit" name="Submit" value="Submit"> </form> PHP:
Check is file type included in the array ? if(isset($_POST['Submit'])){ echo "<pre>"; print_r($_FILES); }
i tried but i just return an array of Array ( [videofile] => Array ( [name] => movies_bouncing_boobs.wmv [type] => [tmp_name] => [error] => 1 [size] => 0 ) )