/    Sign up×
Community /Pin to ProfileBookmark

What Php Functions To Use To Build A Secure File Upload Form ?

Php Folks,

I want to allow my website members to login to their accounts and upload files to my server so other members can view them. Files such as text files, image files, audio files and video files. But not program files or executable files.
Now, what features must my File Upload Form have ?
I need a complete list of Php features it must have. I need you to give me a complete list of Php Functions the File Upload Form must make use of.
Kindly list as many Php features and functions you can think of that my File Upload Form must have in order for it to be a Secure File Upload Form so no one can upload malicious files (virus, programs, etc.).
I am going to get the File Upload Form developed by a paid programmer. I need to give the programmer a list of features the File Upload Form must have. Here is my list of REQUIREMENTS so far:

REQUIREMENT 1:
Php 7 code must be procedural style. No OOP.

REQUIREMENT 2:
The current web form is a single file uploading form. You can upload only 1 “video file” at a time.
You must add multiple upload feature by adding 3 more fields where on one field you can upload “img” file only and on another field you can upload “audio” file only and on another field you can upload “text” file only.
Uploading Form should be in:
Html5, CSS (latest version), Responsive Design (for both Computer, Mobile, Tablets, iPhone, etc. users) with Captcha facility and contain Hidden Field/s to foil bots.
Must be sql injection proof, hacking proof, bot uploading proof and malicious file (virus, etc.) uploading proof.
Current form looks like this:

[code]
<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>
[/code]

You can make the file uploading webform look something like the following. It is only a rough idea about what fields it should contain. And not a rough idea about what the webform design should look like. Frankly, I do not like the webform design and so change it to look good and cool. Show me a few web form templates and I will descide which design I want.

[code]
<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>
<div class=”form-group”>
<p align=”left”<label>Audio File: </label>
<input type=”file” name=”id_verification_audio_file” id=”id_verification_audio_file” value=”uploaded ‘Id Verification Video File.'”></p>
</div>
<div class=”form-group”>
<p align=”left”<label>Text File: </label>
<input type=”file” name=”id_verification_text_file” id=”id_verification_text_file” value=”uploaded ‘Id Verification Video File.'”></p>
</div>
<div class=”form-group”>
<p align=”left”<label>Image File: </label>
<input type=”file” name=”id_verification_image_file” id=”id_verification_image_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>
[/code]

REQUIREMENT 3:
Add filters and sanitizations so malicious files cannot be uploaded. Nor can sql injections can be made.

REQUIREMENT 4:
Only file types from White-List should be uploaded. Any File Types not listed on this White-List should be discarded and not uploaded. Error should be given that this type of file is not allowed to be uploaded.

REQUIREMENT 5:
Uploaded File should not be more than 100MB. Echo error if File Sizes exceed limit & halt script.
MUST check File Size with function: file_size():
https://www.php.net/manual/en/function.filesize.php

REQUIREMENT 6:
Set a maximum name length and maximum file size – Make sure to set a maximum name length and file size in order to prevent a Denial of Service attack.
If you do not know what I am talking about then read number “6” on the following link:
https://www.opswat.com/blog/file-upload-protection-best-practices

REQUIREMENT 7:
MUST make use php of function getimagesize() for security purpose.
https://www.php.net/manual/en/function.getimagesize.php

REQUIREMENT 8:
Write to the file when you store it to include a header that makes it non-executable.
If you do not understand what I am talking about then read the line on the following link that comes just after the CONCLUSION section.
https://www.wordfence.com/learn/how-to-prevent-file-upload-vulnerabilities/

REQUIREMENT 9:
MUST STORE all errors and DISPLAY all errors using traditional:
“Errors[] = “”;.
On my script, fix my error coding mistakes related to the following format as I have no clue how to fix all that to store errors and display them.
“Errors[] = “”;.

REQUIREMENT 10:
To detect File Details, should use php functions:
file_info() & mime_content_type():
https://www.php.net/manual/en/function.finfo-file.php
https://www.php.net/manual/en/function.mime-content-type.php
Script Files (executable files) should not be uploadable. Only text files (.txt, .doc, .pdf, etc.), image files (.giff, .jpeg, etc.), audio files (.mp3, etc.) and video files (.mp4, .wav, etc.).

REQUIREMENT 11:
Script should check whether file upload was successful or not.
MUST check with upload with function: is_uploaded_file().
http://php.net/manual/en/function.is-uploaded-file.php
NOTE: After the check, user must get notified whether file has been uploaded successfully or not.

REQUIREMENT 12:
Uploaded File should be renamed to “$user” before getting saved to permanent destination directory: $directory_path_and_user_dir/$user.
MUST Rename File using function: rename():
https://www.php.net/manual/en/function.rename.php
Eg. If file name is “my_vid.MP4” or “12345.WAV” and User’s username ($user) is “tom” then File Name should be renamed to: “tom_id_verification.MP4″/”tom_id_verification.WAV”.

REQUIREMENT 13:
You may make use of any other php functions, not mentioned in my REQUIREMENTS LIST, should you deem making use of these are necessary to make the php script more safe & secure to upload files. On the quote, you will have to provide me a list of extra functions and features you are thiniking of adding to make the php script secure. If you deem more functions are needed to be used then speak-up. Because, if you do not speakup and others speakup the list of php functions further needed to make the php script safe & secure to upload files then I will assume you have no experience in building these File Uploading scripts and they do. Or, why else you failed to provide me the list and kept quiet ?

END OF REQUIREMENTS

to post a comment

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 29.2019 — > @site-developer#1603170 REQUIREMENT 1:

> Php 7 code must be procedural style. No OOP.


Well, you'd lose me as a potential developer for this right here, if I were actually in the market to do it.

>REQUIREMENT 7:

>MUST make use php of function getimagesize() for security purpose.


You should tell the developer the functional requirement, not how to code it, IMHO. What if they know a better way? (Ditto for Requirement 10.)

Frankly, you'd probably need to use some 3rd-party library -- not necessarily PHP, even -- if you want to check for malicious software, since it's possible to put such code into video and other non-programming files.
Copy linkTweet thisAlerts:
@site-developerauthorApr 29.2019 — Php Folks,

My Current Script is as follows which I am gonna get the programmer to build from there. Carry-on where I left-off. That way, they don't have to write from scratch and charge me an arm and a leg.
<i>
</i>&lt;?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);

$conn = mysqli_connect("localhost","root","","followingbrowser");
if (!$conn)
{
$error = mysqli_connect_error();
$errno = mysqli_connect_errno();
print "$errno: $errorn";
exit();
}

if($_SERVER["REQUEST_METHOD"] == "POST")
{
//var_dump($_FILES); //For debugging.
//Check whether the file was uploaded or not without any errors.
if(!isset($_FILES["id_verification_video_file"]))
{
echo "Upload your video file";
exit;
}
<br/>
<i> </i> elseif (!$_FILES["id_verification_video_file"]["error"] == 0)
<i> </i> {
<i> </i> $Errors = Array(); //Not necssary.
<i> </i> $Errors[] = "Error: " . $_FILES["id_verification_video_file"] ["ERROR"]; //Not necssary.
<i> </i> print_r($_FILES); ?&gt;&lt;br&gt;&lt;?php //For debugging.
<i> </i> print_r($_ERRORS); //For debugging.
<i> </i> exit();
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> $user = 'admin';
<i> </i> $db_user = 'followingbrowser_user'; //Has to be in quotes.
<i> </i> //Feed Id Verification Video File Upload Directory path.
<i> </i> $directory_path = 'uploads/videos/id_verifications/';
<i> </i> //Make Directory under $user in 'uploads/videos/id_verifications' Folder.
<i> </i> $directory_path_and_user_dir = "uploads/videos/id_verifications/$user";
<i> </i>
<i> </i> if(!is_dir($directory_path_and_user_dir))
<i> </i> {
<i> </i> $db_user = 'followingbrowser_user'; //Has to be in quotes.
<i> </i> $mode = 0755;
<i> </i> mkdir($directory_path_and_user_dir,$mode, TRUE); //This line is correct and working even without quoting $mode.
<i> </i> }
<i> </i>
<i> </i> //Grab Uploading File details.
<i> </i> $Errors = Array(); //Printout errors with echoes.
<i> </i> $file_name = $_FILES["id_verification_video_file"]["name"];
<i> </i> $file_tmp = $_FILES["id_verification_video_file"]["tmp_name"];
<i> </i> $file_type = $_FILES["id_verification_video_file"]["type"];
<i> </i> $file_size = $_FILES["id_verification_video_file"]["size"];
<i> </i> $file_error = $_FILES['id_verification_video_file']['error'];
<i> </i>
<i> </i> //Grab Uploading File Extension details.
<i> </i> $file_extension = pathinfo($file_name, PATHINFO_EXTENSION);
<i> </i>
<i> </i> $directory_path_and_user_dir_and_user_file = "$directory_path_and_user_dir/$file_name";
<i> </i>
<i> </i> if(file_exists($directory_path_and_user_dir_and_user_file))
<i> </i> {
<i> </i> echo "Error: You have already uploaded a video file to verify your ID! No need to upload and verify again!";
<i> </i> exit();
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> //Feed allowed File Extensions List.
<i> </i> $allowed_file_extensions = array("mp4" =&gt; "video/mp4","wmv" =&gt; "video/wmv");
<i> </i>
<i> </i> //Feed allowed File Size.
<i> </i> $max_file_size_allowed_in_bytes = 1024*1024*100; //Allowed limit: 100MB.
<i> </i> $max_file_size_allowed_in_kilobytes = 1024*100; //Allowed limit: 100MB.
<i> </i> $max_file_size_allowed_in_megabytes = 100; //Allowed limit: 100MB.
<i> </i>
<i> </i> $max_file_size_allowed = "$max_file_size_allowed_in_bytes";
<i> </i>
<i> </i> //Verify File Extension.
<i> </i> if(!array_key_exists($file_extension, $allowed_file_extensions))
<i> </i> {
<i> </i> echo "Error: Select a valid video file format. Like Mp4, Wav, etc. files.";
<i> </i> exit();
<i> </i> }
<i> </i> //Verify MIME Type of the File.
<i> </i> elseif(!in_array($file_type, $allowed_file_extensions))
<i> </i> {
<i> </i> echo "Error: There was a problem uploading your file $file_name! Make sure your file is a video file. You may try again.";
<i> </i> exit();
<i> </i> }
<i> </i> //Verify File Size. Allowed Max Limit: 100MB.
<i> </i> elseif($file_size&gt;$max_file_size_allowed)
<i> </i> {
<i> </i> echo "Error: Your Video File Size is larger than the allowed limit of: $max_file_size_allowed_in_megabytes.";
<i> </i> exit();
<i> </i> }
<i> </i>
<i> </i> $move_to = $directory_path_and_user_dir."/".$file_name;
<i> </i> //Move uploaded File to newly created directory on the server.
<i> </i> if(!move_uploaded_file($file_tmp, $move_to))
<i> </i> {
<i> </i> $db_user = 'followingbrowser_user'; //Has to be in quotes. But, line not necessary.
<i> </i> $mode = 0755; //Line not necessary.
<i> </i>
<i> </i> //Notify user their Id Verification Video File uploading failed.
<i> </i> echo "Your Video File "$file_name" has failed to be uploaded! You may try some other time.";
<i> </i> exit();
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> //Notify user their Id Verification Video File was uploaded successfully.
<i> </i> die("Your Video File "$file_name" has been uploaded successfully!");
<i> </i> }
<i> </i> }
<i> </i> }
<i> </i>}
?&gt;

&lt;?php //SWITCH FOLLOWING UPLOADING WEB FORM TO HTML5 DESIGN WITH CAPTCHA AND HIDDEN FIELD/S. FOIL BOTS FROM UPLOADING FILES. MUST BE SQL INJECTION AND HACKING PROOF.?&gt;
&lt;form METHOD="POST" ACTION="" enctype="multipart/form-data"&gt;
&lt;fieldset&gt;
&lt;p align="left"&gt;&lt;h3&gt;&lt;?php $site_name ?&gt; ID Video Verification Form&lt;/h3&gt;&lt;/p&gt;
&lt;div class="form-group"&gt;
&lt;p align="left"&lt;label&gt;Video File: &lt;/label&gt;
&lt;input type="file" name="id_verification_video_file" id="id_verification_video_file" value="uploaded 'Id Verification Video File.'"&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/fieldset&gt;
&lt;p align="left"&gt;&lt;button type="submit" class="btn btn-default" name="id_verification_video_file_submit"&gt;Submit!&lt;/button&gt;&lt;/p&gt;
&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;

&lt;?php
include 'footer_account.php'; //Required on all webpages of the Account.
?&gt;


Q2.

Should I get the programmer to add security features from other languages ? Currently, all my REQUIREMENTS revolve around Php as that is the only programming language I am learning. I do not have experience in any other languages. Server-side or Client-side.

Now, do you reckon I should get the programmer to add security features on Client-side ? If so, program in which language and make use of which functions from that language ? Can you give me a complete list ?

Q3. For security purpose, should I even bother getting the programmer to turn the current File Upload Form into an Ajax form ?

Q4. For security purpose, should I even bother getting the programmer to add Json or Jscript ? Are they really necessary ?

If so, which features and functions must be used to make the File Upload Form more secure ?

(I was just youtubing to learn more about Ajax and what is Json or Jquery and so I am very raw in these 3 fields. Hence, need your advice whether of these 3 should be added or not and what features and/or functions must be added from them 3).

Q5. Which language was used to build youtube you reckon and which of the functions were used from the language/s, you reckon ?

Q6. If you were in my position then which languages would you make use of and which functions and features of these languages would you make use of ? I need your complete list.

Thanks
Copy linkTweet thisAlerts:
@site-developerauthorApr 29.2019 — @NogDog#1603171


Mmm. I just been reading articles on how to foil malicious file uploads and been gathering the php functions from these articles and adding them onto my REQUIREMENTS list. I did mention on REQUIREMENT 13 that they are free to add as many other secure features that are not listed on my list. I believe if I do not give them a list of php functions to use then they won't bother too add as many security features as possible just to cut their work short. Hence forcing php function REQUIREMENTS upon them.
×

Success!

Help @site-developer spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...