Php Masters,
Below is my code to upload a video file to my mysql tbl.
If user tries uploading wrong type of file then he should get error.
And if he tries uploading a file beyond the allowed limit then too he should get error.
Do you think my code is ok ?
One thing though, I am trying to get the script to upload the user’s video file on this path:
uploads/videos/id_verifications/$user
And so, if the directory does not exist then the script should create it. Else just upload the file to the directory without creating/recreating it.
My code looks like this:
[code]
//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”))
{
$mode = “0777”;
mkdir(“$directory_path” . “$user”, $mode, TRUE);
}
Problem is, if the directory already exists then I get error which I should not because if the directory exists then the script should just upload it to that directory without creating/recreating it (based on the condition).
Error:
Warning: mkdir(): File exists in C:xampphtdocstextupload_v1.php on line 35
Line 35 is this:
[code]
mkdir(“$directory_path” . “$user”, $mode, TRUE);
What is wrong ? Why I get this error ? Not supposed to as the condition states that if the directory does not exist then create it. Else just upload the file to it without creating/recreating it.
Full Code:
[code]
<?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: $errorn”;
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”))
{
$mode = “0777”;
mkdir(“$directory_path” . “$user”, $mode, TRUE);
}
else
{
}
//Grab uploading 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 Uploading File Extension details.
$file_extension = pathinfo($file_name, PATHINFO_EXTENSION);
if(file_exists(“$directory_path” . “$user/” . “$file_name”))
{
$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.”;
}
//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”);
//Notify user their Id Verification Video 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>
Error reporting is in one of the included files and it goes like this:
[code]
<?php
//ERROR REPORTING CODES.
declare(strict_types=1);
ini_set(‘display_errors’, ‘1’);
ini_set(‘display_startup_errors’, ‘1’);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
?>
Beats me! Can you figure this thing out ? Any ideas ?