1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Uploading Of Php error file type

Discussion in 'PHP' started by baddot, Apr 4, 2007.

  1. #1
    <?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 ?
     
    baddot, Apr 4, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    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.
     
    nico_swd, Apr 4, 2007 IP
  3. technoguy

    technoguy Notable Member

    Messages:
    4,332
    Likes Received:
    299
    Best Answers:
    0
    Trophy Points:
    205
    #3
    whats an error? Can you please give us more details or screenshot please?
     
    technoguy, Apr 4, 2007 IP
  4. baddot

    baddot Active Member

    Messages:
    309
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #4
    it just say Could Not Copy, Wrong Filetype and i upload 5 megs wmv or mpg or avi
     
    baddot, Apr 4, 2007 IP
  5. baddot

    baddot Active Member

    Messages:
    309
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #5
    1Could Not Copy, Wrong Filetype () it just show this
     
    baddot, Apr 4, 2007 IP
  6. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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.
     
    sea otter, Apr 4, 2007 IP
  7. baddot

    baddot Active Member

    Messages:
    309
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #7
    Fatal error: Call to undefined function: mime_content_type() in /home/keeptouch/domains/keeptouch.net/public_html/video/fileup.php on line 9
     
    baddot, Apr 4, 2007 IP
  8. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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.
     
    sea otter, Apr 4, 2007 IP
  9. baddot

    baddot Active Member

    Messages:
    309
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #9
    but when i do a file -i i got this

    boobs.wmv: application/octet-stream
     
    baddot, Apr 5, 2007 IP
  10. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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:
     
    sea otter, Apr 5, 2007 IP
  11. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #11
    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.
     
    nico_swd, Apr 5, 2007 IP
  12. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #12
    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):
     
    sea otter, Apr 5, 2007 IP
  13. baddot

    baddot Active Member

    Messages:
    309
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #13
    so meaning it wont work ?
     
    baddot, Apr 8, 2007 IP
  14. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #14
    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.
     
    sea otter, Apr 8, 2007 IP
  15. baddot

    baddot Active Member

    Messages:
    309
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #15
    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:
     
    baddot, Apr 8, 2007 IP
  16. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #16
    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:
     
    sea otter, Apr 9, 2007 IP
  17. baddot

    baddot Active Member

    Messages:
    309
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #17
    btw can i know what is application/x-empty ?
     
    baddot, Apr 9, 2007 IP
  18. baddot

    baddot Active Member

    Messages:
    309
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #18
    baddot, Apr 9, 2007 IP
  19. jitesh

    jitesh Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #19
    Check is file type included in the array ?

    if(isset($_POST['Submit'])){
    echo "<pre>";
    print_r($_FILES);

    }
     
    jitesh, Apr 9, 2007 IP
  20. baddot

    baddot Active Member

    Messages:
    309
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #20
    i tried but i just return an array of

    Array
    (
    [videofile] => Array
    (
    [name] => movies_bouncing_boobs.wmv
    [type] =>
    [tmp_name] =>
    [error] => 1
    [size] => 0
    )

    )
     
    baddot, Apr 9, 2007 IP