Thread: Upload File Php

Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    764
    Rep Power
    0

    Question Upload File Php


    Php Folks,


    Here are 2 php scripts related to uploading a file.
    The 2 scripts got the same code but if you check the first ELSE you will notice both of them differ slightly in the code rearrangements.

    Q1. Which script's rearrangement would you go for ? Which one should I stick too ?
    The first one was based around a skeleton from a tutorial. It didn't seem to orderly and so I built the second one. It might be faster than the first one when a lot of traffic uses the upload.php. What do you say ?

    Q2. Do you see any mistakes or bad coding in any of them ?

    Q3. I done my best and created 2 samples. How-about you show us a sample how you'd code it using procedural style ? You don't have to start from scratch. You may build from my ones' skeletons. I haven't gotten into pdo yet. Sill on mysqli. SO, do bear that in mind when showing your sample. Though, I don't think this is really important in this topic.


    Thanks!

    SAMPLE 1
    PHP Code:
    <?php 

    //Required PHP Files.
    include 'config.php';
    include 
    'header.php';

    //Check if User is already logged-in or not. Get the login_check() FUNCTION to check. 
    if (login_check() === FALSE)
    {
        
    //Redirect User to Log-in Page immediately.
        //header("refresh:0; url=login.php");
        
    header("location:login.php");
        exit();
    }
    else
    {
        
    $user $_SESSION["user"];
        
        
    $id $_SESSION["id"]; 
        
    $account_activation_status $_SESSION["account_activation_status"]; 
        
    $id_video_verification_status $_SESSION["id_video_verification_status"]; 
        
    $id_video_verification_url $_SESSION["id_video_verification_url"]; 
        
    $sponsor_username $_SESSION["sponsor_username"]; 
        
    $recruits_number $_SESSION["recruits_number"]; 
        
    $on_day_number_on_7_days_wish_list $_SESSION["on_day_number_on_7_days_wish_list"]; 
        
    $primary_website_domain $_SESSION["primary_website_domain"]; 
        
    $primary_website_email $_SESSION["primary_website_email"]; 
        
    $username $_SESSION["username"]; 
        
    $first_name $_SESSION["first_name"]; 
        
    $middle_name $_SESSION["middle_name"]; 
        
    $surname $_SESSION["surname"]; 
        
    $gender $_SESSION["gender"]; 
        
    $date_of_birth $_SESSION["date_of_birth"]; 
        
    $age_range $_SESSION["age_range"]; 
        
    $religion $_SESSION["religion"]; 
        
    $education $_SESSION["education"]; 
        
    $profession $_SESSION["profession"]; 
        
    $marital_status $_SESSION["marital_status"]; 
        
    $working_status $_SESSION["working_status"]; 
        
    $home_town $_SESSION["home_town"]; 
        
    $home_borough $_SESSION["home_borough"]; 
        
    $home_city $_SESSION["home_city"]; 
        
    $home_county $_SESSION["home_county"]; 
        
    $home_region $_SESSION["home_region"]; 
        
    $home_state $_SESSION["home_state"]; 
        
    $home_country $_SESSION["home_country"]; 

        
        
    //Check whether the form was submitted or not. 
        
    if($_SERVER["REQUEST_METHOD"] == "POST"
        { 
            
    //Check whether the file was uploaded without any errors or not. 
            
    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 
            { 
                
    $user $_SESSION["user"]; 
                
                
    //Feed Id Video Verification 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"))
                {
                    
    $mode "0777";
                    
    mkdir("$directory_path"$user"$modeTRUE);
                }
                
                
    //Grab File details. 
                
    $Errors = Array(); 
                
    $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"]; 
                
                
    //Grab File Extension details. 
                
    $file_extension pathinfo($file_namePATHINFO_EXTENSION); 
                        
                if(
    file_exists("$directory_path"$user/" "$file_name")) 
                {
                    
    $Errors[] = "Error: File $file_name! already exists! It has been uploaded before!";                 
                    exit();
                }
                else 
                {
                    
    //Feed allowed File Extension(s). 
                    
    $allowed_file_extensions = array("mp4" => "video/mp4");
                    
                    
    //Feed allowed File Size. 
                    
    $max_file_size_allowed_in_Bytes 1024 1024 1024100//Allowed limit is: 100MB. 
                    
    $max_file_size_allowed_in_KBs $max_file_size_allowed_in_Bytes/1000
                    
    $max_file_size_allowed_in_MBs $max_file_size_allowed_in_KBs/1000
                    
    $max_file_size_allowed_in_GBs $max_file_size_allowed_in_MBs/1000
                    
    $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."
                    } 
                    
    //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_MBs."); 
                    
                        
    //Move uploaded File to newly created directory on server. 
                        
    move_uploaded_file("$file_tmp""$directory_path"$user/" "$file_name"); 
                        
    //Notify user their File was uploaded successfully."; 
                        
    echo "Your Video File \"$file_name\" has been uploaded successfully!"
                        exit(); 
                } 
            }
        } 
    }

    ?>

    <form enctype="multipart/form-data" ACTION="" METHOD="POST">
        <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>
    SAMPLE 2
    PHP Code:
    <?php 

    //Required PHP Files.
    include 'config.php';
    include 
    'header.php';

    //Check if User is already logged-in or not. Get the login_check() FUNCTION to check. 
    if (login_check() === FALSE)
    {
        
    //Redirect User to Log-in Page immediately.
        //header("refresh:0; url=login.php");
        
    header("location:login.php");
        exit();
    }
    else
    {
        
    $user $_SESSION["user"];
        
        
    $id $_SESSION["id"]; 
        
    $account_activation_status $_SESSION["account_activation_status"]; 
        
    $id_video_verification_status $_SESSION["id_video_verification_status"]; 
        
    $id_video_verification_url $_SESSION["id_video_verification_url"]; 
        
    $sponsor_username $_SESSION["sponsor_username"]; 
        
    $recruits_number $_SESSION["recruits_number"]; 
        
    $on_day_number_on_7_days_wish_list $_SESSION["on_day_number_on_7_days_wish_list"]; 
        
    $primary_website_domain $_SESSION["primary_website_domain"]; 
        
    $primary_website_email $_SESSION["primary_website_email"]; 
        
    $username $_SESSION["username"]; 
        
    $first_name $_SESSION["first_name"]; 
        
    $middle_name $_SESSION["middle_name"]; 
        
    $surname $_SESSION["surname"]; 
        
    $gender $_SESSION["gender"]; 
        
    $date_of_birth $_SESSION["date_of_birth"]; 
        
    $age_range $_SESSION["age_range"]; 
        
    $religion $_SESSION["religion"]; 
        
    $education $_SESSION["education"]; 
        
    $profession $_SESSION["profession"]; 
        
    $marital_status $_SESSION["marital_status"]; 
        
    $working_status $_SESSION["working_status"]; 
        
    $home_town $_SESSION["home_town"]; 
        
    $home_borough $_SESSION["home_borough"]; 
        
    $home_city $_SESSION["home_city"]; 
        
    $home_county $_SESSION["home_county"]; 
        
    $home_region $_SESSION["home_region"]; 
        
    $home_state $_SESSION["home_state"]; 
        
    $home_country $_SESSION["home_country"]; 

        
        
    //Check whether the form was submitted or not. 
        
    if($_SERVER["REQUEST_METHOD"] == "POST"
        { 
            
    //Check whether the file was uploaded without any errors or not. 
            
    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 
            { 
                
    $user $_SESSION["user"]; 
                
                
    //Grab File details. 
                
    $Errors = Array(); 
                
    $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"]; 
                
                
    //Grab File Extension details. 
                
    $file_extension pathinfo($file_namePATHINFO_EXTENSION); 
                
                
    //Feed Id Video Verification 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"))
                {
                    
    $mode "0777";
                    
    mkdir("$directory_path"$user"$modeTRUE);
                }
                        
                if(
    file_exists("$directory_path"$user/" "$file_name")) 
                {
                    
    $Errors[] = "Error: File $file_name! already exists! It has been uploaded before!"
                    exit(); 
                }
                else 
                { 
                    
    //Feed allowed File Extension(s). 
                    
    $allowed_file_extensions = array("mp4" => "video/mp4");
                    
                    
    //Feed allowed File Size. 
                    
    $max_file_size_allowed_in_Bytes 1024 1024 1024100//Allowed limit is: 100MB. 
                    
    $max_file_size_allowed_in_KBs $max_file_size_allowed_in_Bytes/1000
                    
    $max_file_size_allowed_in_MBs $max_file_size_allowed_in_KBs/1000
                    
    $max_file_size_allowed_in_GBs $max_file_size_allowed_in_MBs/1000
                    
    $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."
                    } 
                    
    //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_MBs."); 
                    
                        
    //Move uploaded File to newly created directory on server. 
                        
    move_uploaded_file("$file_tmp""$directory_path"$user/" "$file_name"); 
                        
    //Notify user their File was uploaded successfully."; 
                        
    echo "Your Video File \"$file_name\" has been uploaded successfully!"
                        exit(); 
                } 
            }
        } 
    }

    ?>

    <form enctype="multipart/form-data" ACTION="" METHOD="POST">
        <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>
    Last edited by UniqueIdeaMan; May 11th, 2018 at 07:15 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,438
    Rep Power
    652
    Which did you try and which didn't work? What errors did you get or what behavior was other than expected?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    764
    Rep Power
    0

    Question


    Originally Posted by gw1500se
    Which did you try and which didn't work? What errors did you get or what behavior was other than expected?
    Hello my new friend,

    Both worked when I tested them a few wks back. I just want your opinion which one looks more neater and logical. And which one would be rendered faster should tonnes of people request the page at once.
    Like I say, the only difference between the 2 is the code in the first ELSE. Code has been rearranged on the 2nd sample. Should I keep it rearranged like the 2nd sample or revert it back like it was in the 1st sample ? That was the main question. I'm talking about the codes within the { and } in the first ELSE.
    And, do you reckon I have coded it aright ? Sometimes things work co-incidentally even though the code is buggy or done in bad practice. So, let me know if my code was done in good manner (accurate way) or not.
    Better to show me your own sample.

    Thanks
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,438
    Rep Power
    652
    Six of one, half dozen of the other.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,933
    Rep Power
    4554
    You moved one block of code 6 lines or so down. Who ****ing cares? Why do you think this is going to make a difference? It's all the same code, unless I missed a change somewhere.

    Code:
                //Make Directory under $user in 'uploads/videos/id_verifications' Folder. 
                if(!is_dir("$directory_path"  . "$user")) 
                { 
                    $mode = "0777"; 
                    mkdir("$directory_path" . "$user", $mode, TRUE); 
                }
    -- Cigars, whiskey and wild, wild women. --
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    764
    Rep Power
    0
    Originally Posted by Sepodati
    You moved one block of code 6 lines or so down. Who ****ing cares? Why do you think this is going to make a difference? It's all the same code, unless I missed a change somewhere.

    Code:
                //Make Directory under $user in 'uploads/videos/id_verifications' Folder. 
                if(!is_dir("$directory_path"  . "$user")) 
                { 
                    $mode = "0777"; 
                    mkdir("$directory_path" . "$user", $mode, TRUE); 
                }
    Yes, but which version out of the 2 is best practice ?
    Which one's way would you use, my pal ?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    764
    Rep Power
    0
    Originally Posted by gw1500se
    Six of one, half dozen of the other.
    What do you mean ?
    Anyway, do you see any bad coding or error in my code ? I don't get any errors but best to trust you over the error reportings.
  14. #8
  15. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,252
    Rep Power
    4150
    The difference is far too minor to be covered by any best practice rules. Which one do you like best? Use that.

    Comments on this post

    • UniqueIdeaMan agrees : Ok.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,438
    Rep Power
    652
    Originally Posted by UniqueIdeaMan
    What do you mean ?
    Anyway, do you see any bad coding or error in my code ? I don't get any errors but best to trust you over the error reportings.
    I mean for you to think about the difference between six and a half dozen.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  18. #10
  19. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,933
    Rep Power
    4554
    When you draw a circle, is it best to draw clockwise or counterclockwise?
    -- Cigars, whiskey and wild, wild women. --
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,438
    Rep Power
    652
    Wouldn't that depend in if you were right or left handed?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  22. #12
  23. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,933
    Rep Power
    4554
    No
    -- Cigars, whiskey and wild, wild women. --
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2016
    Location
    Cheshire, UK
    Posts
    86
    Rep Power
    72
    It does depend on the software used. With the GD lib, arcs are drawn with the angles increasing in a clockwise direction whereas if you draw a circle as series of many line segments (with each node being at $r*cos($theta), $r*sin($theta) ) then $theta increases in a counterclockwise direction.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,438
    Rep Power
    652
    In geometry, zero degrees starts at the positive x-axis and increases counterclockwise. However in aviation, zero degrees starts at the positive y-axis and increase clockwise. This could be a very difficult problem for this particular OP to comprehend.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  28. #15
  29. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,933
    Rep Power
    4554
    You ****ers are making a simple point complicated. I said draw.
    -- Cigars, whiskey and wild, wild women. --
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo