PHP Upload - Allow only jpg files

后端 未结 2 1893
再見小時候
再見小時候 2021-01-15 21:27

This is what I currently have:

$file_name = $HTTP_POST_FILES[\'uid\'][\'name\'];
$user= \'FILENAME\';
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
$new_f         


        
相关标签:
2条回答
  • 2021-01-15 21:50
    <?php
    
    $allowedTypes = array('image/jpeg');
    
    $fileType = $HTTP_POST_FILES['uid']['type'];
    
    if(!in_array($fileType, $allowedTypes)) {
        // do whatever you need to say that
        // it is an invalid type eg:
        die('You may only upload jpeg images');
    }
    
    ?> 
    

    hope this helps. Also why are you using HTTP_POST_FILES instead of $_FILES? Are you working with an older version of PHP?

    0 讨论(0)
  • 2021-01-15 22:07

    Never ever ever trust that happening. It's unsafe and could potentially lead to people screwing up with your server. Try this instead http://ar.php.net/imagecreatefromjpeg

    <?php
    function LoadJpeg($imgname){
        /* Attempt to open */
        $im = @imagecreatefromjpeg($imgname);
    
        if(!$im){ 
           throw new InvalidArgumentException("$imgname is not a JPEG image");
        }  
    
        return $im;
    }
    ?>
    

    Using it like so:

    $uploadDir = "/path/to/uploads/directory";
    $handle = LoadJpeg($_FILES['uid']['tmp_name']);
    imagejpeg($handle, $uploadDir.DIRECTORY_SEPARATOR.$_FILES['uid']['name']);
    
    0 讨论(0)
提交回复
热议问题