Jump to content
Killersites Community
Sign in to follow this  
saversites

Why My If File Exists Condition Fails ?

Recommended Posts

Php Experts,

For some reason I can't get the "if(file_exists" to work. I don't want the user uploading the same file again. If he tries then should get error alert: "Error: You have already uploaded a video file to verify your ID!"

On the comments, I have written in CAPITALS such as: IS THIS LINE CORRECT ? IS THIS LINE OK ? IS LINE OK ? CORRECT ?

I need your attention most on those particular lines to tell me if I wrote those lines correct or not.

Those are the places where I need your attention the most to tell me if I made any mistakes on those lines or not and if so then what the mistakes are.

If you spot any other errors then kindly let me know. I'd appreciate sample snippets (as your corrections to my mistakes on the mistaken code lines) to what you think I should substitute my lines to.

Thank You!

Here is my attempt:

<?php 
	//Required PHP Files. 
include 'config.php'; 
include 'header.php'; 
include 'account_header.php'; 
	if (!$conn) 
{ 
    $error = mysqli_connect_error(); 
    $errno = mysqli_connect_errno(); 
    print "$errno: $error\n"; 
    exit(); 
} 
	if($_SERVER["REQUEST_METHOD"] == "POST") 
    { 
        //Check whether the file was uploaded or not without any errors. 
        if(!isset($_FILES["id_verification_video_file"]) && $_FILES["id_verification_video_file"]["Error"] == 0) 
        { 
            $Errors = Array(); 
            $Errors[] = "Error: " . $_FILES["id_verification_video_file"] ["ERROR"]; 
            print_r($_FILES); ?><br><?php 
            print_r($_ERRORS); 
            exit(); 
        } 
        else 
        { 
            //Feed Id Verification Video File Upload Directory path. 
            $directory_path = "uploads/videos/id_verifications/"; 
            //Make Directory under $user in 'uploads/videos/id_verifications' Folder. 
            if(!is_dir($directory_path . $user)) //IS THIS LINE CORRECT ?
            { 
                $mode = "0777"; 
                mkdir($directory_path . $user, "$mode", TRUE); //IS THIS LINE CORRECT ?
            } 
            
            //Grab Uploading File details. 
            $Errors = Array(); //SHOULD I KEEP THIS LINE OR NOT ?
            $file_name = $_FILES["id_verification_video_file"]["name"]; 
            $file_tmp = $_FILES["id_verification_video_file"]["tmp_name"]; 
            $file_type = $_FILES["id_verification_video_file"]["type"]; 
            $file_size = $_FILES["id_verification_video_file"]["size"]; 
            $file_error = $_FILES['id_verification_video_file']['error']; 
            
            //Grab Uploading File Extension details. 
            $file_extension = pathinfo($file_name, PATHINFO_EXTENSION);             
            //if(file_exists("$directory_path . $user/ . $file_name")) //IS THIS LINE CORRECT ?
            if(file_exists($directory_path . $user . '/' . $file_name)) //RETYPE
            { 
                $Errors[] = "Error: You have already uploaded a video file to verify your ID!"; 
                exit(); 
            } 
            else 
            { 
                //Feed allowed File Extensions List. 
                $allowed_file_extensions = array("mp4" => "video/mp4"); 
                
                //Feed allowed File Size. 
                $max_file_size_allowed_in_bytes = 1024*1024*100; //Allowed limit: 100MB. 
                $max_file_size_allowed_in_kilobytes = 1024*100; 
                $max_file_size_allowed_in_megabytes = 100; 
                
                $max_file_size_allowed = "$max_file_size_allowed_in_bytes"; 
                
                //Verify File Extension. 
                if(!array_key_exists($file_extension, $allowed_file_extensions)) die("Error: Select a valid video file format. Select an Mp4 file."); 
                //Verify MIME Type of the File. 
                elseif(!in_array($file_type, $allowed_file_extensions)) 
                { 
                    $Errors[] = "Error: There was a problem uploading your file $file_name! Make sure your file is an MP4 video file. You may try again."; //IS THIS LINE CORRECT ?
                } 
                //Verify File Size. Allowed Max Limit: 100MB. 
                elseif($file_size>$max_file_size_allowed) die("Error: Your Video File Size is larger than the allowed limit of: $max_file_size_allowed_in_megabytes."); 
                    //Move uploaded File to newly created directory on the server. 
                    move_uploaded_file("$file_tmp", "$directory_path" . "$user/" . "$file_name"); //IS THIS LINE CORRECT ?
                    //Notify user their Id Verification Video File was uploaded successfully. 
                    echo "Your Video File \"$file_name\" has been uploaded successfully!"; 
                    exit(); 
            } 
        } 
    } 
?> 
	<form METHOD="POST" ACTION="" enctype="multipart/form-data"> 
    <fieldset> 
    <p align="left"><h3><?php $site_name ?> ID Video Verification Form</h3></p> 
    <div class="form-group"> 
        <p align="left"<label>Video File: </label> 
        <input type="file" name="id_verification_video_file" id="id_verification_video_file" value="uploaded 'Id Verification Video File.'"></p> 
    </div> 
    </fieldset> 
    <p align="left"><button type="submit" class="btn btn-default" name="id_verification_video_file_submit">Submit!</button></p> 
</form> 
	</body> 
</html>
	

Share this post


Link to post
Share on other sites
4 hours ago, saversites said:

file_exists("$directory_path . $user/ . $file_name")

Instead of putting this function in the if statement, create a variable to captures the result and then put that variable in the IF statement.

$checkFile = file_exists("$directory_path . $user/ . $file_name");

See what happens.

Stef

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×