PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, March 5, 2022

[FIXED] MAMP on Windows gives empty $_FILES on basic file upload

 March 05, 2022     file-upload, mamp, php, windows     No comments   

Issue

I'm using MAMP for Windows. I need just a basic script to upload a video file so that I can put it in a known directory for a native windows application. I started with code I grabbed from this question for a template to go off of, but my preliminary tests have revealed that it's not as easy to just upload a file with MAMP on Windows.

I've modified it a little bit and it looks like this:

video-form.html

<html>
<body>
    <form action="video-up.php" method="post" enctype="multipart/form-data">
        <label for="file">Filename:</label>
        <input type="file" name="file" id="file" />
        <br>
        <input type="submit" name="submit" value="Submit" />
    </form>
</body>
</html> 

video-up.php

<?php
$allowedExts = array("mov");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ( $_FILES["file"]["type"] == "video/quicktime" && in_array($extension, $allowedExts) )
{
    if ($_FILES["file"]["error"] > 0)
    {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
    else
    {
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

        if (file_exists("upload/" . $_FILES["file"]["name"]))
        {
            echo $_FILES["file"]["name"] . " already exists. ";
        }   
        else
        {
            move_uploaded_file($_FILES["file"]["tmp_name"],
            "upload/" . $_FILES["file"]["name"]);
            echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
        }
    }
}
else
{
    echo "Invalid file";
}

?>
<br><br>$_FILES
<pre>
<?php
    var_dump($_FILES);
?>
</pre> 
<br><br>$_POST
<pre>
<?php
    var_dump($_FILES);
?>
</pre> 
<br><br>$_GET
<pre>
<?php
    var_dump($_GET);
?>
</pre>

I put $_FILES $_POST & $_GET there just to see what I was gettin, but the output of video-up.php after being given a video file is a bunch of blank arrays.

Invalid file

$_FILES
array(0) {
}


$_POST
array(0) {
}


$_GET
array(0) {
}

The file uploads section of the php.ini looks like this:

;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;

; Whether to allow HTTP file uploads.
file_uploads = On

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
upload_tmp_dir = C:\MAMP\temp

; Maximum allowed size for uploaded files.
upload_max_filesize = 200M


; Maximum number of files that can be uploaded via a single request
max_file_uploads = 20

Any help as to why php doesn't seem to recognize the upload is welcome. Thanks.


Solution

I tried it with a different .mov file and the upload now recognizes the file. I'm not sure what the difference between those two files are that would cause one to not be recognized/uploaded at all while the other works, but when I find out I will update this answer.

UPDATE:

Turns out there are two settings in php.ini that need to be tweaked that affect the allowed file size. One being the specific one of upload_max_filesize and the other being general to the size of the entire post request itself: post_max_size.

The post_max_size setting doesn't show an error on the page with error_reporting(E_ALL); ini_set('display_errors', 1);, but will put it in the php error log. It will actually be a warning and look something like this:

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

I believe the default size is 8M for that setting.



Answered By - Scott M
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing