PHP $_FILES multiple file uploading problem - php

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.

Related

PHP file upload gives successful error code, but no image appears

For the sake of clarity I'll break this up into easy to navigate and readable sections.
The Issue:
The $_FILES super-global doesn't appear to hold any information after I upload a file. Despite this, I get error code 0 (successful upload) which seems contradictory.
Background:
I've been working on a personal website for a while now, and am working on a post management system. It's been going great so far, but I'm stuck fast trying to upload an image using $_FILES.
I've done this kind of thing many times at school and my job, but there appears to be some sort of issue with my computer, or I'm having a massive stroke of idiocy.
To fix this issue I've already:
Enabled file uploads in php.ini-production for the version I am using (php 7.1.7)
Changed values such as upload_max_filesize and post_max_size to 100m
Inspected the error code PHP is returning, returns error code 0, or UPLOAD_ERR_OK
Checked my own code for any simple mistakes I'm making. I can't see any, but that's usually the case when you have an error.
I'll include a small test page I wrote, and is demonstrating the issue:
text.php:
<?php
if(isset($_POST['submit'])){
echo $_FILES['a']['error'];
if(isset($_FILE['a']['tmp_name'])){
echo "hello";
}
}
?>
<form action = "text.php" method = "POST" enctype="multipart/form-data">
<label for = "a"> Send Image </label>
<input type = "file" name = "a" id = "a">
<input type = "submit" name = "submit" value = "Submit">
</form>
when the form is submitted, I get error code 0 but the hello test statement does not trigger.
Global array is named $_FILES you are checking for isset($_FILE['a']['tmp_name']
And if you want to check for uploaded file try using
if (is_uploaded_file($_FILES['a']['tmp_name'])) {
//file is uploaded
}

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 File Copy() Not working on IIS 6

I have the following code that attempts to take a users form input of a file, and upload it to the webserver.
This code does work on a Apache server, however I'm now trying to get the same code working on my Windows IIS 6 web server, which has PHP (Version 5.2.3) installed and working. I have set the PHP.INI file so that
file_uploads = On
upload_tmp_dir = "C:\Temp"
My form is
<form method="POST" action="do_upload.php" enctype="multipart/form-data">
<input type="file" name="img1" size="30">
<input type="submit" name="BtnUpload" value="Click To Upload Now">
</form>
My PHP code to do the upload is
$abpath = "C:\MyWebs\Website1\httdocs\images";
#copy($img1, "$abpath/$img1_name") or $log .= "Couldn't copy image 1 to server";
if (file_exists("$abpath/$img1_name"))
{
$log .= "File 1 was uploaded";
}
else
{
$log .= "File 1 is not an image";
}
For some reason when I check the value of $img1 e.g echo $img1; it is empty. Therefore I tried to get the file using $_FILES['img1']['name']. This worked fine, but still I couldn't upload any files
Any ideas why this is happening.
Your code should be:
move_uploaded_file($_FILES['img1']['tmp_name'], "$abpath/$img1_name");
Don't copy() uploaded files. There are a few edge cases where an uploaded file can be tampered with, which is why move_uploaded_file() exists - it checks for those particular types of tampering.
As well, be VERY careful with how you create your filenames when processing the upload. If you directly use ANYTHING provided in $_FILES as part of the destination path/name for the file, you are opening bad security holes on your server, and a malicious user can exploit that to scribble a file anywhere they want on your server.

Simple PHP for File upload form not working?

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.

Problem uploading file in php

I have a simple but annoying problem and I don't understand the reason. I need to upload a simple csv file with php. Here's my code:
index.php:
<form method="post" action="upload.php" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input type="submit" value="submit" />
</form>
upload.php:
$upfile = "csv/".$_FILES["file"]["name"];
move_uploaded_file($_FILES["file"]["tmp_name"], $upfile);
No errors whatsoever, but the file is still not uploaded on the server.
UPDATE: print_r($_FILES) output:
Array ( [fileUpload] => Array ( [name] => file1.csv [type] => text/comma-separated-values [tmp_name] => /var/tmp/php6YZ4Bt [error] => 0 [size] => 45 ) )
Things to check
1) make sure your directory has permissions set to 755
2) check your path to see if it is correct.
3) make sure your post_max_size is the proper size.
4) make sure all your errors are turned on.
This can be done in the php script by using this code:
error_reporting(E_ALL); // or E_STRICT
ini_set("display_errors",1);
5) Increase your memory limit to see if the script is using more memory than previously allocated..
ini_set("memory_limit","1024M");

Categories