Jump to content
phreak3r

POST error upon uploading a file

Recommended Posts

I am working on a video-hosting site, something akin to YouTube. I converted whatever MySQLi I had to PDO. This piece of particular code is responsible for checking if the fields are filled in; then proceeds to upload the files and inserts data into the database. The code jumps straight to the error I created which is "empty fields". The var_dump prints out as null all the way. I cannot seem to figure out where the problem lies. I would say it could be that the file is not set? I am not quite sure. Here is what the log gives me:

[Sat Feb 16 00:19:35.575770 2019] [php7:warn] [pid 16239] [client 127.0.0.1:42504] PHP Warning:  POST Content-Length of 12263648 bytes exceeds the limit of 8388608 bytes in Unknown on line 0, referer: http://localhost/soapbox/upload.php
[Sat Feb 16 00:19:35.576769 2019] [php7:notice] [pid 16239] [client 127.0.0.1:42504] PHP Notice:  Undefined index: video_title in /var/www/html/soapbox/upload.php on line 15, referer: http://localhost/soapbox/upload.php
[Sat Feb 16 00:19:35.576805 2019] [php7:notice] [pid 16239] [client 127.0.0.1:42504] PHP Notice:  Undefined index: textarea-videoDesc in /var/www/html/soapbox/upload.php on line 16, referer: http://localhost/soapbox/upload.php
[Sat Feb 16 00:19:35.576811 2019] [php7:notice] [pid 16239] [client 127.0.0.1:42504] PHP Notice:  Undefined index: videoFile in /var/www/html/soapbox/upload.php on line 22, referer: http://localhost/soapbox/upload.php
[Sat Feb 16 00:19:35.576829 2019] [php7:notice] [pid 16239] [client 127.0.0.1:42504] PHP Notice:  Undefined index: thumbnailImage in /var/www/html/soapbox/upload.php on line 51, referer: http://localhost/soapbox/upload.php
[Sat Feb 16 00:19:35.576845 2019] [php7:notice] [pid 16239] [client 127.0.0.1:42504] PHP Notice:  Undefined index: thumbnailImage in /var/www/html/soapbox/upload.php on line 52, referer: http://localhost/soapbox/upload.php
[Sat Feb 16 00:19:35.576849 2019] [php7:notice] [pid 16239] [client 127.0.0.1:42504] PHP Notice:  Undefined index: thumbnailImage in /var/www/html/soapbox/upload.php on line 53, referer: http://localhost/soapbox/upload.php
[Sat Feb 16 00:19:35.576854 2019] [php7:notice] [pid 16239] [client 127.0.0.1:42504] PHP Notice:  Undefined index: thumbnailImage in /var/www/html/soapbox/upload.php on line 54, referer: http://localhost/soapbox/upload.php
[Sat Feb 16 00:19:35.576858 2019] [php7:notice] [pid 16239] [client 127.0.0.1:42504] PHP Notice:  Undefined index: thumbnailImage in /var/www/html/soapbox/upload.php on line 55, referer: http://localhost/soapbox/upload.php
[Sat Feb 16 00:19:35.576862 2019] [php7:notice] [pid 16239] [client 127.0.0.1:42504] PHP Notice:  Undefined index: thumbnailImage in /var/www/html/soapbox/upload.php on line 56, referer: http://localhost/soapbox/upload.php

I have changed the allotted sizes in the php.ini file, so that rules out the POST Content-Length problem, I think.

Here are the "undefined indexes":

$videoTitle = $_POST['video_title'];
$videoDesc = $_POST['textarea-videoDesc'];

$file = $_FILES['videoFile'];

$thumbnailImageName = $_FILES['thumbnailImage']['name'];
$thumbnailImageTmpName = $_FILES['thumbnailImage']['tmp_name'];
$thumbnailImageSize = $_FILES['thumbnailImage']['size'];
$thumbnailImageError = $_FILES['thumbnailImage']['error'];
$thumbnailImageType = $_FILES['thumbnailImage']['type'];
$thumbnailImageExt = explode('.', $thumbnailImageName);

And the corresponding form names to go with them:

	<form action="upload.php" method="POST" enctype="multipart/form-data" multiple><br>
		<p>Video File:</p><input type="file" name="videoFile" id="fileToUpload"><br>
		<p>Thumbnail Image File: </p><input type="file" name="thumbnailImage"><br>
		<p>Video Title: </p><input type="text" name="video_title" id="videoTitle" placeholder="Video title"><br>
		<p>Video Description</p><textarea name="textarea-videoDesc" placeholder="Video description..." rows="7" style="resize: none;"></textarea><br>
		<br><input type="submit" name="uploadBtn" value="Upload">
	</form>

 

Share this post


Link to post
Share on other sites

Did you restart Apache/nginx/PHP so the change takes effect?

Share this post


Link to post
Share on other sites

You showed us two separate blocks of code.  What is there physical relationship in this script?  Does the first block get executed when the second has not been run yet?  Do you actually confirm that you have a post array at some point?

Share this post


Link to post
Share on other sites
8 hours ago, requinix said:

Did you restart Apache/nginx/PHP so the change takes effect?

I restarted the Apache2 service.

 

4 hours ago, ginerjm said:

You showed us two separate blocks of code.  What is there physical relationship in this script?  Does the first block get executed when the second has not been run yet?  Do you actually confirm that you have a post array at some point?

Oh! I am so sorry, I forgot to add in the script, here you go.

		if (isset($file) && $fileSize != 0 /*&& $thumbnailImageSize != 0*/ && !empty($videoTitle)) {
			$sql = $pdo->prepare("INSERT into videos001 (uploader, video, thumbnail, video_title, video_desc) VALUES (:username, :fileDestination, :thumbnailImageDestination, :videoTitle, :videoDesc)");
                        $sql->bindValue(':username', $username);
                        $sql->bindValue(':fileDestination', $fileDestination);
                        $sql->bindValue(':thumbnailImageDestination', $thumbnailImageDestination);
                        $sql->bindValue(':videoTitle', $videoTitle);
                        $sql->bindValue(':videoDesc', $videoDesc);
                        $sql->execute();

			header('Location: /soapbox/upload.php?success');
		} else {
			echo $error[3];
                        var_dump($file, $videoTitle, $videoDesc);
		}

 

Share this post


Link to post
Share on other sites

Ok - you've added a third block of code and again - What is the realationship between all of these?   Which one comes first?? I have to ask this before considering the possible reasons for your error messages. 

At first glance your error is because you haven't yet received the submit from the form, hence there is no post with those elements in it.   IMHO the way to write this script would be to first check if the post has been received by  checking the $_SERVER['REQUEST_METHOD'] for a value of 'POST'.  If you don't have it yet then don't go processing anything from that form.  Keep moving on down your script until you get to the part that displays the html and then exit and wait.

Share this post


Link to post
Share on other sites

If you really want to see the whole code, here it goes. I could do with some better erm...organization/structure? It is such a big script, so I tried to refrain from including it.

<?php

include('header.php');
require('dbcon/dbcon.php');
include('functions.php');

isLoggedIn();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    // Error declaration
    $error = ["Your file is too big!", "There was an error uploading your file!", "Cannot upload file of this type!", "Empty fields!"];

    // Process POST variables
    $videoTitle = $_POST['video_title'];
    $videoDesc = $_POST['textarea-videoDesc'];

    // Process session variable
    $username = $_SESSION['username'];
		
    // file upload stuff...
    $file = $_FILES['videoFile'];
    $fileName = $file['name'];
    $fileTmpName = $file['tmp_name'];
    $fileSize = $file['size'];
    $fileError = $file['error'];
    $fileType = $file['type'];
    $fileExt = explode('.', $fileName);
    $fileActualExt = strtolower(end($fileExt));

    $allowed = array('mp4', 'mov', 'mkv');

    if (in_array($fileActualExt, $allowed)) {
        if ($fileError === 0) {
            if ($fileSize < 2000000) {
                $fileNameNew = $username . "." . $fileActualExt;
                $fileDestination = "channel/" . $username . "/videos/" . $fileNameNew;
                move_uploaded_file($fileTmpName, $fileDestination);
            } else {
                echo $error[0];
            }
        } else {
            echo $error[1];
        }
    } else if (!$allowed) {
        echo $error[2];
    }
////////////////////////////////////////////////////////////////////

			$thumbnailImageFile = $_FILES['thumbnailImage'];
			$thumbnailImageName = $_FILES['thumbnailImage']['name'];
			$thumbnailImageTmpName = $_FILES['thumbnailImage']['tmp_name'];
			$thumbnailImageSize = $_FILES['thumbnailImage']['size'];
			$thumbnailImageError = $_FILES['thumbnailImage']['error'];
			$thumbnailImageType = $_FILES['thumbnailImage']['type'];
			$thumbnailImageExt = explode('.', $thumbnailImageName);
			$thumbnailImageActualExt = strtolower(end($thumbnailImageExt));

			$allowedThumbnailFileExts = array('png', 'jpg', 'jpeg');

			if (in_array($thumbnailImageActualExt, $allowedThumbnailFileExts)) {
				if ($thumbnailImageError === 0) {
					if ($thumbnailImageSize < 200000000) {
						$thumbnailImageNameNew = $username . "thumbnailImage" . uniqid('', true). "." . $thumbnailImageActualExt;
						$thumbnailImageDestination = 'uploads/thumbnails/' . $thumbnailImageNameNew;
						move_uploaded_file($thumbnailImageTmpName, $thumbnailImageDestination);
					} else {
						echo $error[0];
					}
				} else {
					echo $error[1];
				}
			} else if (!$allowed) {
				echo $error[2];
			}

		if (isset($file) && $fileSize != 0 /*&& $thumbnailImageSize != 0*/ && !empty($videoTitle)) {
			$sql = $pdo->prepare("INSERT into videos001 (uploader, video, thumbnail, video_title, video_desc) VALUES (:username, :fileDestination, :thumbnailImageDestination, :videoTitle, :videoDesc)");
                        $sql->bindValue(':username', $username);
                        $sql->bindValue(':fileDestination', $fileDestination);
                        $sql->bindValue(':thumbnailImageDestination', $thumbnailImageDestination);
                        $sql->bindValue(':videoTitle', $videoTitle);
                        $sql->bindValue(':videoDesc', $videoDesc);
                        $sql->execute();

			header('Location: /soapbox/upload.php?success');
		} else {
			echo $error[3];
                        var_dump($file, $videoTitle, $videoDesc);
		}
		
} // end of if server method...

// TODO: if there's no thumbnail, do not upload video, let user know to put in a thumbnail
?>

<!DOCTYPE html>
<html>
<head>
	<title>soapbox - upload</title>
</head>
<body>
	<form action="upload.php" method="POST" enctype="multipart/form-data" multiple><br>
		<p>Video File:</p><input type="file" name="videoFile" id="fileToUpload"><br>
		<p>Thumbnail Image File: </p><input type="file" name="thumbnailImage"><br>
		<p>Video Title: </p><input type="text" name="video_title" id="videoTitle" placeholder="Video title"><br>
		<p>Video Description</p><textarea name="textarea-videoDesc" placeholder="Video description..." rows="7" style="resize: none;"></textarea><br>
		<br><input type="submit" name="uploadBtn" value="Upload">
	</form>
</body>
</html>

 

Share this post


Link to post
Share on other sites

Assuming that this:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    // Error declaration
    $error = ["Your file is too big!", "There was an error uploading your file!", "Cannot upload file of this type!", "Empty fields!"];

    // Process POST variables
    $videoTitle = $_POST['video_title'];
    $videoDesc = $_POST['textarea-videoDesc'];

is where your error is occurring, add the following:

echo "<pre>",print_r($_POST,true),"</pre>";

right after the "Process POST variables" comment line.  Let's see what your POST array has in it!

Share this post


Link to post
Share on other sites

Apparently there is nothing in the array.

Array
(
)

And why is a comma used instead of a period in reference to the echo "<pre>",print_r($_POST, true),"</pre>"; line?

Share this post


Link to post
Share on other sites
6 minutes ago, phreak3r said:

And why is a comma used instead of a period in reference to the echo "<pre>",print_r($_POST, true),"</pre>"; line?

Unlike print() (which allows only a single argument and you would have to use "." to concatenate with that function) echo() will accept several arguments separated by commas. Either will do.

Share this post


Link to post
Share on other sites
2 minutes ago, Barand said:

Unlike print() (which allows only a single argument and you would have to use "." to concatenate with that function) echo() will accept several arguments separated by commas. Either will do.

Okay, well I tried using "." but that did not really seem to work. Thank you for your contribution. I know many of my previous posts may display the behavior "IT'S BROKEN! HELP!", but I am genuinely trying to understand this all.

Share this post


Link to post
Share on other sites

Should work

<?php
    $a = 'foo';
    $b = 'bar';
    
    echo $a, $b, '<br>';                   //--> foobar
    echo $a . $b . '<br>';                 //--> foobar
?>

 

Share this post


Link to post
Share on other sites

Back to the topic at hand....

So - I want you to look at the source code of your displayed web page and make sure you have a valid "form" entity in it.  To get the output that you say you received tells me that you did not get a proper submit from this form of yours so I'd have to question what got sent to your page. 

Share this post


Link to post
Share on other sites
1 hour ago, Barand said:

Should work


<?php
    $a = 'foo';
    $b = 'bar';
    
    echo $a, $b, '<br>';                   //--> foobar
    echo $a . $b . '<br>';                 //--> foobar
?>

 

Yes, it does. I made a simple mistake and added a semicolon after the variable. For example : echo $a; "<br>";

Share this post


Link to post
Share on other sites

ginerjm, I think the form is valid. I will scour the Internet for some similar problems.

Share this post


Link to post
Share on other sites

Obviously the form is no valid since you are not getting anything back.  That's why I asked that you LOOK at the web page content using the 'view source' option of your browser to see what you have out there. 

You can certainly "go scour" but the short answer is to go see what is (NOT) getting sent.

PS - When I took JUST your form's code and put it into a little test script I managed to get it sent. I then made entries for the two problem input elements and clicked on the Update button and got back data.  So - the form code is valid (as you say) and works when clicked.  Therefore - I'd say your script is NOT sending it in the first place.

Edited by ginerjm

Share this post


Link to post
Share on other sites

if you are still getting the first error you listed - ... POST Content-Length of 12263648 bytes exceeds the limit of 8388608 bytes..., and you tried to set the post_max_size setting to be more than 8M Bytes, something prevented the change from working. either the php.ini that you changed isn't the one that php is using, there's a setting that's overriding the one you change (a local php.ini), there's a syntax error in the php.ini above the point you changed that's preventing the file from being fully parsed, the line you changed is in some commented out code or is using a value that isn't valid.

add a phpinfo(); statement to the top of your .php file and check what the - Loaded Configuration File value is and what both the local and master post_max_size setting is. also, check if the file_uploads setting is ON.

for reference, here are the things that can cause the $_FILES array to be empty - 

1. One of several possible problems with the html form - not a post method form, missing the enctype attribute, missing a type='file' field, the form field isn't inside the form, a misnaming between the form field and the php code, broken html markup, ...
2. Uploads are not enabled on the server.
3. The total size of the submitted post data exceeds the post_max_size setting. In this case, both the $_POST and $_FILES arrays will be empty.
4. The $_FILES array is being referenced without first detecting that a post method form was submitted.

 

Edited by mac_gyver
  • Like 1

Share this post


Link to post
Share on other sites

The Loaded Configuration File is /etc/php/7.2/apache2/php.ini. Both master and local are 8M. I modified the drb@z10n:/etc/php/7.2/cli path. 😕

Share this post


Link to post
Share on other sites

mac_gyver, that worked, I am still getting an error, but at least there isn't any of the null business.

Share this post


Link to post
Share on other sites
1 minute ago, phreak3r said:

Both master and local are 8M

yes, but you are trying to upload 12M bytes.

your form processing code, after detecting that a post method form was submitted, first needs to detect and handle the condition in item #3 in my list above. you should detect that the $_FILES array is empty and set up an appropriate user error message, than skip trying to process any $_FILES or $_POST data since there isn't any.

you should use an array to hold the validation errors (this is not the same as your array holding the defined error messages.) as you test and validate the submitted data, store the errors into an array variable. this variable then also serves as an error flag. if the array is empty, there are no errors and you can use the submitted data. if the array is not empty, there are errors. to display the errors, you would output the content of this array at the appropriate point in your html document.

Share this post


Link to post
Share on other sites
8 hours ago, mac_gyver said:

your form processing code, after detecting that a post method form was submitted, first needs to detect and handle the condition in item #3 in my list above. you should detect that the $_FILES array is empty and set up an appropriate user error message, than skip trying to process any $_FILES or $_POST data since there isn't any.

you should use an array to hold the validation errors (this is not the same as your array holding the defined error messages.) as you test and validate the submitted data, store the errors into an array variable. this variable then also serves as an error flag. if the array is empty, there are no errors and you can use the submitted data. if the array is not empty, there are errors. to display the errors, you would output the content of this array at the appropriate point in your html document. 

So it is not the same as my array holding defined error messages? I might have an idea of what you mean, but for the most part I do not. How would this help? In my mind, PHP is a language where you are simply manipulating arrays, that's all. At least that is how I interpret it.

Share this post


Link to post
Share on other sites

PHP ... simply manipulating arrays?  Wow - I would have never thought of that.  There is so much much more that one can do with PHP.  It is incredible how many extensions/add-ons have been written to complement native PHP that for someone to boil it all down to "manipulating arrays" is hard to fathom.

Glad to see that mac_gyver figured it out for you.

As for how to handle error messages - you tried one way but it's kind of backwards.  Instead of storing your set of canned messages use an array to save generated messages.  You can do it with just a numeric (default) key that is meaningless or with a named index to help you decide how to output them, but the values will be whatever message you want to eventually output.  So - as you go thru your processing and accumulate errors you build/manipulate this array and at the end of your logic test if the array is empty or not and then output it or do whatever you intended to do with the user's input.

Share this post


Link to post
Share on other sites
13 hours ago, ginerjm said:

PHP ... simply manipulating arrays?  Wow - I would have never thought of that.  There is so much much more that one can do with PHP.  It is incredible how many extensions/add-ons have been written to complement native PHP that for someone to boil it all down to "manipulating arrays" is hard to fathom.

Glad to see that mac_gyver figured it out for you.

As for how to handle error messages - you tried one way but it's kind of backwards.  Instead of storing your set of canned messages use an array to save generated messages.  You can do it with just a numeric (default) key that is meaningless or with a named index to help you decide how to output them, but the values will be whatever message you want to eventually output.  So - as you go thru your processing and accumulate errors you build/manipulate this array and at the end of your logic test if the array is empty or not and then output it or do whatever you intended to do with the user's input.

Sarcastic and cynical. Mmmm...my favorite flavor!

What is the difference between my canned messages and using an array to 'save' generated messages? I am the kind of person that needs things further simplified sometimes.

Share this post


Link to post
Share on other sites

Sarcasm? I suggest you re-read my post....

The difference is you only create an array that has immediate usage as in when you have an error to output only then do you bother to store it.  Plus - from a maintenance POV - you can keep track of exactly where an error message is derived from instead of trying to relate a set of them stored off to the side and where the code is that triggers one of them to show up.  You also can easily check if you HAVE any errors by doing what I said instead of having to keep track of some kind of switch that tells you if you want to output any errors.

Basically, it's just simpler.

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

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.