Simple PHP for File upload form not working? - php

i am new to this one and in a learning phase. i created a form like below,
<html>
<body>
<form enctype="multipart/form-data" action="/cgi-bin/FileUpload.php" method="POST">
<label for="file">Filename:</label>
<input type="file" name="ufile"/>
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
and my php script looks like,
#!/usr/bin/php
<?
header("Content-type:text/html");
echo "\n";
if (isset($HTTP_POST_VARS['submit'])){
echo "HTTP_POST_VARS['submit'] is set" . "<br />";
}
if (empty($_POST)) {
echo "Empty POST !" . "<br />";
}
if(isset($_POST['type'])){
echo "POST['type'] is set to " . $_POST['type'] . "<br />";
}
if (isset($_SERVER['REQUEST_METHOD'])){
echo "_SERVER['REQUEST_METHOD'] is set and REQUEST_METHOD = " . $_SERVER['REQUEST_METHOD'] . "<br />";
}
if (empty($_FILES)){
echo "_FILES is empty ! " . "<br /> <br />";
}
the putput looks like,
Empty POST !
_SERVER['REQUEST_METHOD'] is set and REQUEST_METHOD = POST
_FILES is empty !
the related items in php.ini is like below,
;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;
; Whether to allow HTTP file uploads.
; http://php.net/file-uploads
file_uploads = On
; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
; http://php.net/upload-tmp-dir
upload_tmp_dir = /tmp
; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 2M
; Maximum number of files that can be uploaded via a single request
max_file_uploads = 20
; Maximum size of POST data that PHP will accept.
; http://php.net/post-max-size
post_max_size = 10M
now, why the _POST and _FILES are getting empty ?

A few things:
1.
#!/usr/bin/php
^^^^
The spaces before your shebang will be treated as output by the PHP interpreter. The shebang should appear at the very start of your file
2.
HTTP_POST_VARS is deprecated and should not be used anymore, unless you're on an ancient PHP version, in which case you really should upgrade.
3.
Put a phpinfo(); call into your script. It'll show both the global settings AND the local settings. It's possible that a config file (.htaccess, another .ini later in the loading chain, etc...) is overriding your settings and disabling file uploads. If the local column in the phpinfo output doesn't show the proper settings, you'll have to figure out where the override is occuring.
4.
Instead of doing "blahblah is empty" and the like, try doing var_dump($whatever), which'll show you the actual empty array, or its contents (type+size+data) if there is anything present.

In addition to the comments above, try var_dump($_POST) and var_dump($_FILES); that will dump every file and values posted to the page.
If it's not working, and your php.ini settings are as you wrote earlier, you can try using the ini_set() function on the action page. Use ini_set() to set the file_uploads and the other configuration settings. Ini_set() will not work for all the settings, but it should be enough to get you started.
Don't forget the dependable phpinfo() function also.

Related

PHP Upload Won't Work

I made a php upload script that would be executed after a form with the upload was submitted.
$game_tmp = $game_file['tmp_name'];
$game_name = $game_file['name'];
$game_dest = "/games/" . game_name;
if (move_uploaded_file($game_tmp, $game_dest)) {
echo "<b>SUCCESS:</b><br />";
}
else {
echo "Error.";
}
It always output Error, so I checked my php.ini file and phpinfo(). my php.ini shows upload_tmp_dir with no value and so does the value in phpinfo(). I changed the calue of the directory to /upload, and changed the open_basedir to have a value of upload_tmp_dir. Yet, when I look into phpinfo(), it still shows upload_tmp_dir with a local and master value of no value. I believe this is the problem that is stopping my upload script from working. I created a /upload folder also and gave its permission value 777. Yet, this problem persists. I am not sure what is causing this problem.
Try this, you must use the $_FILE super global:
if($_FILES["game_file"]["error"] == UPLOAD_ERR_OK) {
$game_tmp = $_FILES['game_file']['tmp_name'];
$game_name = $_FILES['game_file']['name'];
$game_dest = "/games/" . $game_name;
if (move_uploaded_file($game_tmp, $game_dest)) {
echo "<b>SUCCESS:</b><br />";
}
else {
echo "Error.";
}
}
if upload_tmp_dir is not specified, php will use system's default tmp directory. You can use sys_get_temp_dir() function to identify temporary directory being used by PHP.
Have you tried printing $game_file var?
If not, try print_r($game_file) or var_dump($game_file) to view if its actually being set. You can also print $_FILE to see error message (http://php.net/manual/en/features.file-upload.errors.php)
move_uploaded_file could fail for many reason. Also, confirm if the destination directory exists and is writable.
Typos:
$game_dest = "/games/" . game_name;
^---you forgot a $ here
That's an undefined constant, so PHP will "politely" treat it as an unquoted string, and you're generating "/games/game_name" instead.
never EVER do devel/debug work in PHP with display_errors and error_reporting turned off. It's the equivalent of going "lalalalala can't hear you" with your fingers stuffed in your ears.
And you're also simply assuming your upload succeeded. There's a ['error'] parameter in $_FILES for a reason. Check it FIRST, before you start fiddling with a file that may not even be there.

How can I upload a file in PHP large than about 2MB?

I am trying to allow users to upload a max of a 2GB jar file with an HTML file input and PHP. The process works fine and it uploads the file, but only if it is small. I have set the php.ini to this:
max_execution_time = 10000
max_input_time = 10000
post_max_size = 2048M
upload_max_filesize = 2048M
After I did this it made all my php pages that has in it:
require_once("../include/config.php");
to load as a plain white page with NOTHING on it OR (SOMETIMES) it gives me this error:
Fatal error: Cannot redeclare encodePassword() (previously declared in /home/duskfall/public_html/authentication/encode.php:2) in /home/duskfall/public_html/authentication/encode.php on line 80
On line 80 where it said it was is:
$coded = "";
Why would it do this when I only put 4 things in the php.ini. They are also the only 4 things when I created the php.ini file. It even does it when I put NOTHING in the php.ini file. Do I need to put some other things in the file?
How would I fix this problem. Or is there a better way to do it with the php.ini and make it effect just ONE php page?
EDIT: Also I am using the host godaddy.com on a linux server if that helps at all.
Before use post_max_size you must set memory_limit.
post_max_size depend of memory_limit, memory_limit is must be higher than post_max_size, not equal.
Also post_max_size must be higher than upload_max_filesize.
Use in php script:
echo ini_get("memory_limit")."\n";
ini_set("memory_limit","30M");
echo ini_get("memory_limit")."\n";
echo ini_get("post_max_size")."\n";
ini_set("post_max_size","20M");
echo ini_get("post_max_size")."\n";
echo ini_get("upload_max_filesize")."\n";
ini_set("upload_max_filesize","19M");
echo ini_get("upload_max_filesize")."\n";
Also:
I think you have a problem with your function encodePassword().
Try use new clear test files like:
test.html
<form enctype="multipart/form-data" action="/path/to/your/phpscript" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
test.php
$uploaddir = '/path/to/your/dir';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "Success.\n";
} else {
echo "Error!\n";
}
echo '<pre>';
echo 'Debug file:';
print_r($_FILES);
echo "</pre>";

How to avoid post_max_size

I have a problem. I am creating my own PHP application and I want to allow users to upload files.
My PHP
function make_upload() {
echo "<pre>";
print_r($_FILES);
echo "</pre>";
}
if (isset($_GET["upload"]) && $_GET["upload"] == "1") {
make_upload();
}
My HTML
<form action="?upload=1" method="post" enctype="multipart/form-data">
<input name="file[]" type="file" multiple/>
<input type="submit" value="Upload"/>
</form>
When I try to browse and select many files, the print_r($_FILES); shows blank Array ( ) instead of the array with my files. That's happens because of post_max_size which is set to 8M. If I change my post_max_size then I can select more images.
I want to share my application to be installed on websites, but I don't want to tell users to increase their post_max_size.
Is there any option to avoid post_max_size? (I see that in wordpress I can select images which are over the post_max_size - how is made?)
you can do it via .htaccess file.
php_value upload_max_filesize 200M
but sometime hosting provider does not allow you to overrite it. you will get 500 Internal Server Error in that case
You need to set the value of upload_max_filesize and post_max_size in your php.ini :
Maximum allowed size for uploaded files.
upload_max_filesize = 40M;
Must be greater than or equal to upload_max_filesize
post_max_size = 40M;
After modifying php.ini file(s), you need to restart your HTTP server to use new configuration.
If you can't change your php.ini, you're out of luck. You cannot change these values at run-time; uploads of file larger than the value specified in php.ini will have failed by the time execution reaches your call to ini_set.
See the Description of core php.ini directives.
you have not passed array to function pass it when you calling
if (isset($_GET["upload"]) && $_GET["upload"] == "1") {
make_upload($_FILES); // pass this $_FILES array to function
}
AND function would be like this
function make_upload($_FILES) {
echo "<pre>";
print_r($_FILES); // will print array
echo "</pre>";
}

PHP Image don't upload

I am dont' uploading image file to my path. I added enctype='multipart/form-data' in html code. That's php code;
$membeUrl = "inc/img/user/".$_SESSION['memberName'];
if(!is_dir($membeUrl)){mkdir($membeUrl);}
$profilePhoto = $membeUrl."/".$_FILES['profilePhoto']['name'];
if($_FILES['profilePhoto']['error'] > 0){
alert("Profil photo error:".$_FILES['profilePhoto']['error']);
}
move_uploaded_file($_FILES['profilePhoto']['tmp_name'],$profilePhoto);
$w = " WHERE email='a#a.com' AND age='18'";
if((!preg_match("/[\-]{2,}|[;]|[']|[\\\*]/",$profilePhoto))){
mysql_query("UPDATE member SET pp='".$profilePhoto."'".$w);
}
I don't see and understand. How we do solve?
Thank you for your insterest.
Good works..
From var_dump() info you specified it is obvious file you are uploading exceeds php.ini upload_max_filesize directive (check the Error=1 code explanation in manual)
Either upload a smaller file or change php.ini value.
Also check that post_max_size ini directive is equal or larger than upload_max_filesize, since even if you put larger size for upload, post value is the absolute limit.
To know more about ini directives read manual: http://www.php.net/manual/en/ini.core.php#ini.upload-max-filesize

PHP Uploading Issue

I've worked with a few scripts to begin uploading files on my development machine. Problem is, despite the expected ease of this operation, Apache seems to time-out whenever I try to upload an image. Uploading is set to On and the tmp directory is set in php.ini.
I tried uploading the main gif from Google, an 8.36KB image. It should be fine and well within the limits to PHPs uploading capabilities.
Here is a copy of the script. There should be an easy fix. As requested, I changed the tilde to an actual directory.
<?php
if (!isset($_GET['upload'])) { ?>
<form method="post" action="index.php?upload=true" enctype="multipart/form-data">
<input type="file" name="file" class="form">
<input name="submit" type="submit">
</form>
<? } else if (isset($_GET['upload']) && $_GET['upload'] == 'true') {
$url = $_FILES['file']['name'];
$move = move_uploaded_file($_FILES['file']['tmp_name'], "/Users/<username>/Sites/file.jpg");
if ($move) {
echo "Success!";
} else {
echo "Err..."
}
} ?>
Thanks,
Dan
EDIT:
I fixed it, with help from a few of the answers, to one of which I will mark.
A few things here were causing this behavior.
Permissions on the images directory were not set to allow the _www user to access it. A chmod -R 777 images seemed to fix it, as well as a sudo chown _www images.
The form output may have been corrupting the PHP script itself. As suggested, an ECHO <<< ...END helped, I think.
What is it that leads you to believe that Apache is timing out rather than, say, outright failing in some way? Because what leaps out at me is that you're trying to move the file to ~/file.jpg, which I'm nearly certain will not work; ~ is a construct that only normally has meaning inside shells, unless one of PHP's freakish obscure features is processing it in contexts like this. Anyway, try putting the actual directory.
This is more than likely an issue with the size of the file and/or a permission issue between the Apache user and the directory specified. For instance make sure the Apache instance is not running under user (nobody).
Comment to chaos:
He is right the tilde (~) can cause issues, but would probably not cause a timeout; it would display a warning. Even if it does work on your system it would probably deposit the file into an unexpected directory or run into some issues if the Apache user (ie www) does not have a valid home directory set.
If the issue is filesize, add the following lines to your php.ini file and it should work:
upload_max_filesize = 500M ;
post_max_size = 500M ;
PHP by default has a 30 second timeout on the page. So if your upload takes longer than 30 seconds it will fail. Set the timeout either in your php.ini or put the following code at the top of the file.
ini_set(max_execution_time, 90);
The second argument represents the time in seconds before the page will timeout. Set it to whatever time you feel is appropriate. Also, chaos is correct in that '~' is a construct that commonly has meaning only inside shells.
Re: http://ca2.php.net/manual/en/ini.list.php
EDIT:
The problem is that you reopened the tag in the middle of a conditional. Trying your code I get a syntax error. It's strange that you were able to see any web form. This is the fixed code (that works for me).
<?php
if (!isset($_GET['upload'])) {
ECHO <<<END
<form method="post" action="index.php?upload=true" enctype="multipart/form-data">
<input type="file" name="file" class="form">
<input name="submit" type="submit">
</form>
END;
} else if (isset($_GET['upload']) && $_GET['upload'] == 'true') {
$url = $_FILES['file']['name'];
$move = move_uploaded_file($_FILES['file']['tmp_name'], "/Users/<username>/Sites/file.jpg");
if ($move) {
echo "Success!";
} else {
echo "Err...";
}
} ?>

Categories