Secure way of uploading php images and GIF I am creating a social network and I want to know if this is a secure way of uploading pics/gifs and videos.
if(isset($_POST['post'])) {

    $uploadOk = 1;
    $imageName = $_FILES['postToUpload']['name'];
    $errorMessage = &quot;&quot;;
    $picdate = date('Y-m-d_H-i-s');

    if($imageName != &quot;&quot;) {
        $targetDir = &quot;assets/images/posts/&quot;;
        $imageName = $targetDir . uniqid() . basename($imageName);
        $imageFileType = pathinfo($imageName, PATHINFO_EXTENSION);
        
        if($_FILES['postToUpload']['size'] &gt; 10971520) {
            $errorMessage = &quot;Your file is to large&quot;;
            $uploadOk = 0;
        }

        if (strtolower($imageFileType) != &quot;jpeg&quot; &amp;&amp; strtolower($imageFileType) != &quot;png&quot; &amp;&amp;
            strtolower($imageFileType) != &quot;jpg&quot; &amp;&amp; strtolower($imageFileType) != &quot;gif&quot;
            &amp;&amp; strtolower($imageFileType) != &quot;mp4&quot;&amp;&amp; strtolower($imageFileType) != &quot;Ogg&quot;
            &amp;&amp; strtolower($imageFileType) != &quot;WebM&quot;){

            $errorMessage = &quot;File type not allowed.&quot;;
            $uploadOk = 0;
        }

        if($uploadOk){
            if(move_uploaded_file($_FILES['postToUpload']['tmp_name'], $imageName)) {
                // image uploaded

            } else{
                // image did not upload
                $uploadOk = 0;
            }
        }
    }

    if($uploadOk) {

    $post = new Post($con, $userLoggedIn);

    $post-&gt;submitPost(trim(strip_tags(filter_var($_POST['post_text'], FILTER_SANITIZE_STRING))), 
        'none', $imageName);

    } else {

        echo &quot;&lt;div class='alert alert-danger'&gt;
                $errorMessage
            &lt;/div&gt;&quot;;
    }
}
Your code is insecure because:

It only relies on the file extension, so attackers can upload any backdoors.
It accesses variable $_FILES['postToUpload']['name'] without checking if it is defined and this may lead to Full Path Disclosure.
It uses function uniqid() without any arguments, so uniqueness is not guaranteed and the files that are uploaded simultaneously and have the same file name will be overwritten.
It stores the filename provided by user without removing potentially dangerous characters and this may lead to unexpected results.

Some recommendations to improve your code:

Get rid of the $uploadOk variable. You already have and handle the $errorMessage variable and this is enough to check if the file was uploaded successfully.
You should define $imageFileType as lowercase, instead of calling strtolower() for each extension.
You should define all accepted file extensions as lowercase, otherwise users won't able to upload some files (in your case WebM and Ogg, because strtolower($imageFileType) != &quot;Ogg&quot; and strtolower($imageFileType) != &quot;WebM&quot; will always be FALSE).