Why Undefined Index Error And Not Default File Type Error ?

sunny_pro

New member
Joined
Jun 18, 2017
Messages
86
Points
0
Php Folks,

Why do I keep getting this error and how to solve it ?
Notice: Undefined index: video_verification_file in C:\xampp\htdocs\test\upload.php on line 33

Trying to build a web form where you submit your img file. You should get error if the file is not img file. I tried uploading .php file to see if I get the default error alert or not but ain;t getting it.
Default error:

Error: Please select a valid file format.

PHP:
<?php 

// Check if the form was submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
    // Check if file was uploaded without errors
    if(isset($_FILES["video_verification_file"]) && $_FILES["video_verification_file"]["error"] == 0){
        $allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
        $filename = $_FILES["video_verification_file"]["name"];
        $filetype = $_FILES["video_verification_file"]["type"];
        $filesize = $_FILES["video_verification_file"]["size"];
    
        // Verify file extension
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file format.");
    
        // Verify file size - 5MB maximum
        $maxsize = 5 * 1024 * 1024;
        if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");
    
        // Verify MYME type of the file
        if(in_array($filetype, $allowed)){
            // Check whether file exists before uploading it
            if(file_exists("upload/" . $_FILES["video_verification_file"]["name"])){
                echo $_FILES["video_verification_file"]["name"] . " is already exists.";
            } else{
                move_uploaded_file($_FILES["video_verification_file"]["tmp_name"], "upload/" . $_FILES["video_verification_file"]["name"]);
                echo "Your file was uploaded successfully.";
            } 
        } else{
            echo "Error: There was a problem uploading your file. Please try again."; 
        }
    } else{
        echo "Error: " . $_FILES["video_verification_file"]["error"];
    }
}

	
?>
	
<form method="post" action="">	
	<fieldset>	
	<p align="left"><h3><?php $site_name ?> Video Verification Form</h3></p>
	<div class="form-group">
		<p align="left"><label>Video File:</label>
		<input type="file" name="video_verification_file" id="video_verification_file" value ="Upload Image"></p>
	</div>	
	</fieldset>	
	<p align="left"><button type="video_verification_submit" class="btn btn-default" name="video_verification_submit">Submit!</button></p>
</form>
</body>
</html>
 

Ridew

New member
Joined
Nov 6, 2017
Messages
47
Points
0
Fixed. It seems that you are missing the enctype attribute. Anyway, change your HTML to the following codes:

HTML:
<form method="post" action="" enctype="multipart/form-data">     
    <fieldset>     
    <p align="left"><h3><?php $site_name ?> Video Verification Form</h3></p> 
    <div class="form-group"> 
        <p align="left"><label>Video File:</label> 
        <input type="file" name="video_verification_file" id="video_verification_file" value ="Upload Image"></p> 
    </div>     
    </fieldset>     
    <p align="left"><button type="video_verification_submit" class="btn btn-default" name="video_verification_submit">Submit!</button></p> 
</form>
 
  • Like
Reactions: sunny_pro

sunny_pro

New member
Joined
Jun 18, 2017
Messages
86
Points
0
sunny_pro
Yeah, I spotted that and fixed it about half hr ago. But a new problem now ....

Ok, here is my latest update:

PHP:
<?php 

// Check if the form was submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
    // Check if file was uploaded without errors
    if(isset($_FILES["video_verification_file"]) && $_FILES["video_verification_file"]["error"] == 0){
        $allowed = array("mp4" => "upload/mp4");
        $filename = $_FILES["video_verification_file"]["name"];
        $filetype = $_FILES["video_verification_file"]["type"];
        $filesize = $_FILES["video_verification_file"]["size"];
    
        // Verify file extension
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file format.");
    
        // Verify file size - 1000GB maximum
        $maxsize = 1024 * 1024 * 1024 * 1024 * 1024;
        if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");
    
        // Verify MYME type of the file
        if(in_array($filetype, $allowed)){
            // Check whether file exists before uploading it
            if(file_exists("upload/" . $_FILES["video_verification_file"]["name"])){
                echo $_FILES["video_verification_file"]["name"] . " is already exists.";
            } else{
                move_uploaded_file($_FILES["video_verification_file"]["tmp_name"], "upload/" . $_FILES["video_verification_file"]["name"]);
                echo "Your file was uploaded successfully.";
            } 
        } else{
            echo "Error: There was a problem uploading your file. Please try again."; 
        }
    } else{
        echo "Error: " . $_FILES["video_verification_file"]["error"];
    }
}

	
?>
	
<form enctype="multipart/form-data" action="" method="POST">
	<fieldset>	
	<p align="left"><h3><?php $site_name ?> Video Verification Form</h3></p>
	<div class="form-group">
		<p align="left"><label>Video File:</label>
		<input type="file" name="video_verification_file" id="video_verification_file" value ="Uploaded Video Id"></p>
	</div>	
	</fieldset>	
	<p align="left"><button type="video_verification_submit" class="btn btn-default" name="video_verification_submit">Submit!</button></p>
</form>
</body>
</html>
Why do I get these errors ? Note that, my file size for experimenting purpose put it to 1000GB. And so, I should not be getting the first error!

Warning: POST Content-Length of 79117293 bytes exceeds the limit of 8388608 bytes in Unknown on line 0

Notice: Undefined index: video_verification_file in C:\xampp\htdocs\test\upload.php on line 33
Error:


Line 33 looks like this:

PHP:
} else{
        echo "Error: " . $_FILES["video_verification_file"]["error"];
It is related to this IF on line 6:
PHP:
if(isset($_FILES["video_verification_file"]) && $_FILES["video_verification_file"]["error"] == 0){
What do you reckon is wrong ?
 

Ridew

New member
Joined
Nov 6, 2017
Messages
47
Points
0
Well, your MIME type is wrong. Change your array from upload/mp4 to video/mp4 and everything will work out fine.

Here is the complete code to upload MP4 video.

PHP:
<?php 

// Check if the form was submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
    // Check if file was uploaded without errors
    if(isset($_FILES["video_verification_file"]) && $_FILES["video_verification_file"]["error"] == 0){
        $allowed = array("mp4" => "video/mp4");
        $filename = $_FILES["video_verification_file"]["name"];
        $filetype = $_FILES["video_verification_file"]["type"];
        $filesize = $_FILES["video_verification_file"]["size"];
    
        // Verify file extension
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file format.");
    
        // Verify file size - 1000GB maximum
        $maxsize = 1024 * 1024 * 1024 * 1024 * 1024;
        if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");
    
        // Verify MYME type of the file
        if(in_array($filetype, $allowed)){
            // Check whether file exists before uploading it
            if(file_exists("upload/" . $_FILES["video_verification_file"]["name"])){
                echo $_FILES["video_verification_file"]["name"] . " is already exists.";
            } else{
                move_uploaded_file($_FILES["video_verification_file"]["tmp_name"], "upload/" . $_FILES["video_verification_file"]["name"]);
                echo "Your file was uploaded successfully.";
            } 
        } else{
            echo "Error: There was a problem uploading your file. Please try again."; 
        }
    } else{
        echo "Error: " . $_FILES["video_verification_file"]["error"];
    }
}

    
?>
HTML:
<form action="" method="POST" enctype="multipart/form-data">
    <fieldset>    
    <p align="left"><h3><?php $site_name ?> Video Verification Form</h3></p>
    <div class="form-group">
        <p align="left"><label>Video File:</label>
        <input type="file" name="video_verification_file" id="video_verification_file" value ="Uploaded Video Id"></p>
    </div>    
    </fieldset>    
    <p align="left"><button type="video_verification_submit" class="btn btn-default" name="video_verification_submit">Submit!</button></p>
</form>
</body>
</html>
Good luck! :)
 
  • Like
Reactions: sunny_pro

sunny_pro

New member
Joined
Jun 18, 2017
Messages
86
Points
0
sunny_pro
Thanks! Been offline for nearly a week.
Anyway, I copied & pasting your code and get this error:

Warning: POST Content-Length of 28575184 bytes exceeds the limit of 8388608 bytes in Unknown on line 0

Notice: Undefined index: video_verification_file in C:\xampp\htdocs\test\upload.php on line 33

Actually, there was nothing wrong with my following code:

PHP:
<?php 

// Check if the form was submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
    // Check if file was uploaded without errors
    if(isset($_FILES["video_verification_file"]) && $_FILES["video_verification_file"]["error"] == 0){
        $allowed = array("mp4" => "video/mp4");
        $filename = $_FILES["video_verification_file"]["name"];
        $filetype = $_FILES["video_verification_file"]["type"];
        $filesize = $_FILES["video_verification_file"]["size"];
    
        // Verify file extension
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file format.");
    
        // Verify file size - 1000GB maximum
        $maxsize = 1024 * 1024 * 1024 * 1024 * 1024;
        if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");
    
        // Verify MYME type of the file
        if(in_array($filetype, $allowed)){
            // Check whether file exists before uploading it
            if(file_exists("video/" . $_FILES["video_verification_file"]["name"])){
                echo $_FILES["video_verification_file"]["name"] . " is already exists.";
            } else{
                move_uploaded_file($_FILES["video_verification_file"]["tmp_name"], "video/" . $_FILES["video_verification_file"]["name"]);
                echo "Your file was uploaded successfully.";
            } 
        } else{
            echo "Error: There was a problem uploading your file. Please try again."; 
        }
    } else{
        echo "Error: " . $_FILES["video_verification_file"]["error"];
    }
}

	
?>
	
<form enctype="multipart/form-data" action="" method="POST">
	<fieldset>	
	<p align="left"><h3><?php $site_name ?> Video Verification Form</h3></p>
	<div class="form-group">
		<p align="left"><label>Video File:</label>
		<input type="file" name="video_verification_file" id="video_verification_file" value ="Uploaded Video Id"></p>
	</div>	
	</fieldset>	
	<p align="left"><button type="video_verification_submit" class="btn btn-default" name="video_verification_submit">Submit!</button></p>
</form>
</body>
</html>
I kept getting this error:

Warning: POST Content-Length of 28575184 bytes exceeds the limit of 8388608 bytes in Unknown on line 0

Notice: Undefined index: video_verification_file in C:\xampp\htdocs\test\upload.php on line 33


And so, following the instructions on the following link I solved the issue.

I found php.ini file from this path. C:\xampp\php or from xampp folder.

2. Then opened php.ini file and changed the following:

1. post-max-size (changed 8M to 100000M).

2. upload-max-filesize (change d2M to 100000M).

3. Restared Apache server and MySQL.

https://stackoverflow.com/questions...-8978294-bytes-exceeds-the-limit-of-8388608-b

Yes, I will lower the limits.

Issue solved. Thread can now be closed.
 

Mike001

New member
Joined
Apr 27, 2016
Messages
578
Points
0
Keep in mind that the max_file_size you have set will not work on most hosted web servers.

It does great in your development environment, but it comes to shared servers it is highly unlikely that a web host would allow you to dedicate 100 GB to an uploaded file. Do you have any idea how much resources that would require. Believe me you do not want to know, but it would take down all but the most robust servers that are in a shared environment and also in a dedicated enviornment.

When developing applications in a development environment it is a "Best Practice" to mimic the actual environment that they will reside in on a live environment. Normally a live server will allow uploads of about 200MB to 500MB depending upon the number of applications being supported on the shared server and the type of account you have. Business accounts will have larger parameters than standard accounts. Remember the adage "you get what you pay for" , it really holds true here.

Even these numbers are really pushing the window for resources on shared environments.

Modifying your development environment outside the parameters of a live system will only cause you more grief when you upload the code to the live environment.

Contact your hosting provider, find out what their maximum parameters are and make those parameters part of your development setup. Write your application to support those parameters and you will be much better off.

Just food for thought.
 
Older threads
Replies
3
Views
4,295
Replies
7
Views
2,602
Replies
11
Views
3,971
Newer threads
Latest threads
Replies
2
Views
100
Replies
1
Views
184
Replies
6
Views
397
Replies
11
Views
542
Replies
2
Views
234
Recommended threads
Similar threads
Replies
24
Views
1,969
Replies
4
Views
1,210
Replies
4
Views
2,503

Latest postsNew threads

Referral contests

Referral link for :

Sponsors

Popular tags

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top