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>";
}
Related
I have a single PHP page, with the php (v7.4.7) script at the top of the page. The page functions as a file up-loader, allows up to 10 files per upload, and the max file size is set in the script.
The site works perfectly, generating a table of the result of each file to the user.
That is, unless someone uploads a file greater than the upload_max_filesize directive in the php.ini file. At which point, the script stops dead and cannot therefore, continue to provide the necessary results back to the user. No results are returned, the results table is therefore empty, and the user might think, wrongly, all went well.
I have tried adding try/catch blocks, but the script still fails to complete. Is this by design, or is there a way to coerce the script to run/complete, if this directive is exceeded? The HTML/PHP code is all pretty standard, as demonstrated by various tutorials etc.
Many thanks.
Violating upload_max_filesize doesn't abort the script execution. It'll just cause the error key in $_FILES to become UPLOAD_ERR_INI_SIZE.
Most likely, you're hitting an entirely different limit. For example, Apache has
LimitRequestBody and PHP itself has post_max_size or even max_file_uploads. In general, those directives don't abort the script either but simply wipe out the data excess.
My advice is to locate all the directives that may affect file uploads, set them one by one to an artificially low limit and verify what effect they have in your data. A check I typically do is to verify if $_SERVER['CONTENT_LENGTH'] is greater than zero for a request where $_SERVER['REQUEST_METHOD'] is POST but $_POST is empty. Aside that, checking error in $_FILES should have you fairly covered.
; Allow enough time for the upload to complete
max_input_time = 1200
; Max file size
upload_max_filesize = 50M
; Greater than upload_max_filesize to ease diagnose slightly large files
post_max_size = 100M
if (
empty($_FILES) &&
empty($_POST) &&
isset($_SERVER['REQUEST_METHOD']) &&
strtolower($_SERVER['REQUEST_METHOD'])=='post'
) {
// Error: post body was too large
// Bytes sent (and discarded): filter_input(INPUT_SERVER, 'CONTENT_LENGTH')
} elseif (
isset($_FILES['foo']) &&
$_FILES['foo']['error'] != UPLOAD_ERR_NO_FILE
){
// Upload
if ($_FILES['foo']['error'] === UPLOAD_ERR_OK) {
// Successful upload
}else{
// Failed upload
}
} else {
// No upload
}
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>";
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
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.
I have a little problem with uploading multiple files in PHP ,
i have this html form:
<form method="post" action="upload.php" enctype="multipart/form-data">
<input type="file" name="myfile[]" />
<input type="submit" />
</form>
and this is the upload.php :
<?php print_r( $_FILES ); ?>
when i'm sending a file it show me this:
Array
(
[myfile] => Array
(
[name] => Array
(
[0] => Krw_Qe4QKmI.mp3
)
[type] => Array
(
[0] =>
)
[tmp_name] => Array
(
[0] =>
)
[error] => Array
(
[0] => 1
)
[size] => Array
(
[0] => 0
)
)
)
so far so good.
the problem starts when i upgrade my form to this one :
<form method="post" action="upload.php" enctype="multipart/form-data">
<input type="file" name="myfile[]" />
<input type="file" name="myfile[]" />
<input type="submit" />
</form>
now , when i send 2 files , it show me this :
Array
(
)
so , what's the problem here?
thank you , Mor.
I would bet that you exceeded post_max_size and PHP just ignored the uploaded files.
It's 8MB by default. If you try to upload one 5MB file everything will work. If you try to upload 2 5MB files, it exceeeds 8MB and PHP ignores posted data.
Try increasing the value of post_max_size in your php.ini.
A lot of suggestions here. I'll give it a go. This is based on #Pekka 's comment.
I see you're testing with mp3s, which probably exceed PHP upload limit. This is because in your first example, you actually have an upload error code 1: The uploaded file exceeds the upload_max_filesize directive in php.ini.. So even your fist upload didn't work. A successful upload always has 0 as the error code.
Modify you php.ini with upload_max_filesize = 10M (or 20M, or 300M; careful about that M - which means megabytes - as omitted, brings alot of headache.
I suggest testing with smaller files, as I see you have a limit of 2M for uploading.
Further reading.
Check your max_file_uploads setting -- is it more than 1?
echo ini_get('max_file_uploads');
The php.ini file must have something 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 =
; Maximum allowed size for uploaded files.
upload_max_filesize = 50M
; Maximum number of files that can be uploaded via a single request
max_file_uploads = 20
Change the values of upload_max_filesize and then restart the server
To do multiple files at once, try giving an index like this:
<? For ( $count = 0; count < SOME_MAXIMUM; ++$count; ): ?>
<input type="file" name="myfile[<? Echo $count; ?>]" />
<? endfor; ?>
I had the same problem.. All my efforts were in vain, but finally I found a pretty good note at the PHP Manual. It's simple but suited me perfectly...
"Multiple upload might not work if you use a table for displaying your
form inputs when <form> element is inside the <table> element. In this
case only the first file will be uploaded.
Put the <form> element outside the element to get it to work."
Follow this link for the full note, there is a function to rearrange the multiple upload file array in a easy-to-use manner.
http://php.net/manual/en/features.file-upload.multiple.php
The Problem is your name="myfile[]" attribute on your input-Element.
You cant referer later in PHP to your file, if you haven't an identifier for it. The PHP-Documentation gives you the same hint:
http://www.php.net/manual/en/features.file-upload.multiple.php
Multiple files can be uploaded using different name for input.
So change the name to "myfile1" and "myfile2" (or some better name ;)) should solve your problem.