I've been pulling hairs on this one. I am creating a quit simple file upload application. On a specific point, the files are not uploaded and I receive the message: "Sorry, your file is too large.". I am uploading a file of ±2MB.
The relevant parameters in the php.ini are:
file_uploads On
max_execution_time 60
max_file_uploads 20
post_max_size 32M
upload_max_filesize 32M
So, there shouldn't be a problem there. Anybody an idea?
Used code:
if (isset($_POST['fileUploadBtn'])) {
echo 'File Size: '.$_FILES['file']['size'];
if(isset($_FILES['file']['error'])) {
echo $error_types[$_FILES['file']['error']].'<br>';
}
if(isset($_FILES["file"])) {
if($_FILES["file"]["name"] != '') {
uploadFile($mysqli, $orderid, $md5, $_FILES["file"], 'weefontwerp');
}
}
}
In your code, did you check the file size?
like this:
if ($_FILES['fileToUpload']['size'] > 500000) {
echo 'file too large.';
}
Or, did you restart php to reload the php.ini?
OK, the problem was in the downloaded upload function. There was the check, I forgot to check. I'm terribly sorry to post this here!
Related
I've used ini_set('post_max_size',"2M") in my php script to limit uploads file size. it didn't worked (do you know why ? )
So i put these rules on my htaccess file :
php_value post_max_size 2M
php_value upload_max_filesize 2M
So when i upload a file larger than 2M , my php script shows this error :
Warning: POST Content-Length of 15903708 bytes exceeds the limit of 2097152 bytes in Unknown on line 0
How can i handle this error in an appropriate way ? (something like try catch).
Note : I wouldn't like to hide this error.
In your upload script, have you tried:
if ($_FILES['upfile']['size'] > 2000000) {
$error_message ="Exceeded filesize limit.";
}
Also there is try/catch in php for example:
try{
//your upload script
}
catch(Exception $e){
$error_message = "File did not upload: ". $e->getMessage();
}
From this you should be able to handle any errors how you want
Another solution is a custom error handler:
set_error_handler("warning_handler", E_WARNING);
//your upload script
function warning_handler($errno, $errstr) {
// do someing with your error here
}
restore_error_handler(); // reinstates original error handling
On my XAMPP server, copy() and move_uploaded_file() fail to work if any file uploaded via http request is over 30kB even thought the php.ini post_max_size and upload_max_size values are well over 30kB.
$tmpfile = $_FILES['fileupload']['tmp_name'];
$fileExtension = 'txt';
$newfilename = '01234';
if (!move_uploaded_file($tmpfile, "files/" . $newfilename . "." . $fileExtension)) {
echo "Failed to upload.";
} else {
echo 'Uploaded successfully.';
}
And the php.ini looks like:
; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize=70M
; Maximum number of files that can be uploaded via a single request
max_file_uploads=20
; Post form size limit
post_max_size=80M
I can't see what I'm doing wrong here, does anyone know how to fix this?
I want to get a text file upto size 20 Mb via browser.
My php code is:
$allowedExts = array("txt");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
$file_name=$_FILES["file"]["name"];
if ((
($_FILES["file"]["type"] == "text/plain"))
&& ($_FILES["file"]["size"] < 5243000)
&& in_array($extension, $allowedExts))
{ echo "Uploaded : " . $_FILES["file"]["name"] . "<br>";}
else {echo "Invalid file";}
Note: my php ini settings are :
ini_set("upload_max_filesize", "20M");
ini_set("memory_limit", "32M");
ini_set("post_max_size","20M");
but My browser throwing : Invalid File. even for 3 Mb file (less than 2 Mb is working fine)
Extension is .txt only,
I am sure extension is not a problem.
What might be the one and solution for it?
UPDATE:
my PHP.ini says: Maximum allowed size for uploaded files. upload_max_filesize = 20M
but my php info(); still says: upload_max_filesize local 2M master 2M
You cannot change the upload_max_filesize variable using ini_set (as you can see in this list: List of php.ini directives). Change the php.ini configuration instead.
Make sure below things with print_r($_FILES)
1) $_FILES["file"]["type"] exact match to "text/plain"
2) $_FILES["file"]["size"] is less than 5243000
3) $extension value is txt ( Caps matter in this case )
4) check all the settings with ini_get after ini_set to make sure that settings has been changed
5) If still is ok then please past your print_r($_FILES) output here
i was making an upload script when i tested an image file wit this extension .JPG, i don't know whats the difference between jpg or jpeg, but it seems that $_FILES don't recognize this file type.
I've read several threads that $_FILES ins't that reliable when it comes to mime type, so i decided to used the php's mime type function mime_content_type(), php's getimagesize(), pathinfo(), though pathinfo returns a file name, and type, but i need the path of the file which is NOT present, all of the functions are being passed with $_FILES['file']['tmp_name'] as parameters.
So this problem came up when i decided to upload an image file e.g sample.JPG, i think most of this files are raw from the camera <-- that's what i think though but nevertheless what is more important is that i can upload them .JPG, .jpg, jpeg, .png. all of them works fine except for .JPG.
Main problem is that field ['tmp_name'] in $_FILES has no values when .JPG is to be uploaded.
Any of you guys who have encountered this problem please do share your workaround or "how did you do it" kind of thing.
If $_FILES[$field]['tmp_name'] is empty then the file hasn't been uploaded. You should look at $_FILES[$field]['error'] to see why.
FWIW, and as far as I understand it, the mime-type in $_FILES[] is provided by the browser.
Update: here is a bit of potted code to handle all file upload errors:
$message = 'Error uploading file';
switch( $_FILES['newfile']['error'] ) {
case UPLOAD_ERR_OK:
$message = false;;
break;
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
$message .= ' - file too large (limit of '.get_max_upload().' bytes).';
break;
case UPLOAD_ERR_PARTIAL:
$message .= ' - file upload was not completed.';
break;
case UPLOAD_ERR_NO_FILE:
$message .= ' - zero-length file uploaded.';
break;
default:
$message .= ' - internal error #'.$_FILES['newfile']['error'];
break;
}
if( !$message ) {
if( !is_uploaded_file($_FILES['newfile']['tmp_name']) ) {
$message = 'Error uploading file - unknown error.';
} else {
// Let's see if we can move the file...
$dest .= '/'.$this_file;
if( !move_uploaded_file($_FILES['newfile']['tmp_name'], $dest) ) { // No error supporession so we can see the underlying error.
$message = 'Error uploading file - could not save upload (this will probably be a permissions problem in '.$dest.')';
} else {
$message = 'File uploaded okay.';
}
}
}
Check your php.ini and in particular this setting
; Maximum allowed size for uploaded files.
; http://www.php.net/manual/en/ini.core.php#ini.upload-max-filesize
upload_max_filesize = 6M
Or do this in your Apache Config:
<Directory "/var/www/vhosts/path/to/your/directory/import/">
php_value post_max_size 6M
php_value upload_max_filesize 6M
</Directory>
I would also say that it is poor that PHP doesn't report an error in the error logs if you upload a file that is larger than your php.ini upload_max_filesize setting. For example, if you upload a 6MB file when you have it set at 2M (which I think is the default).
Posting an answer because my rating is too low.
Remember to restart your server after setting the max file size cap in your php.ini. I spent hours on this issue thinking that it wasn't a file size problem meanwhile I forgot to restart. After restart everything worked.
I hope this can help someone.
I was having the same problem and being familiar with mostly .NET platform makes you forget about stuff that happens in the client side html.
My problem was my form is having a MAX_FILE_SIZE hidden input which has some value lesser than file's equavalent bytes.
Your form should have this;
Other than that, your form tag must include enctype="multipart/form-data"
I was thining that max file size was in kb, but it's in bytes, thanks to some other page in stackoverflow.
The other reason that might cause this problem your php.ini settings that people have mentioned in previous comments. You should can post_max_size = 200M to php.ini file too.
If you are developing on Windows like me, you can see a dump file that showing errors at C:\Windows\Temp called as "php**VERSION**_errors.log". It helps.
Seems as though it's a random problem because while I was making a script to upload CSV files, some CSV files would have no problem uploading but in other cases, $_FILES['file'][tmp_name] would be empty.
I faced the issue with $_FILES field 'tmp_name' having no value for .JPG file extension and successfully fixed it in few steps. These measures may help someone in the future.
First of all, I implemented the Switch Case solution offered by the
user 'staticsan'. Link here -
https://stackoverflow.com/a/14472801/3681985. This solution helped me
trace the potential issues such as parameters in php.ini file and
folder permissions.
The php.ini file was named php.ini.default. Changing the parameters upload_max_filesize and post_max_size didn't yield any result, until I renamed the file to php.ini. Remember to experiment with parameter values.
After fixing the file name issue, I encountered a challenge with the permissions to the folder in which the uploaded temp image has to be moved. I have changed the permissions and was able to see the image uploaded in to the folder.
Just try this and see what happens,
if (($_FILES['file']['type']) == "image/jpg" || ($_FILES['file']['type']) == "image/jpeg") {
//do uploading stuff here
}else{
echo 'File is not a valid JPG,JPEG';
}
I am having strange issues regarding file upload on my windows system. I am using windows 7 with iis7 on the server. I am trying on a client comp with local IP 10.47.47.13 and the server is 10.47.47.1.
I have a very simple form which i couldn't make it work in some cases. The page stays on the wwwroot. (http://10.47.47.1/3.php)
3.php
<?php
$source_file=$_FILES["newsimg"]["tmp_name"];
$destination_file="123.jpg";
$ftp_server="localhost";
$ftp_username="admin";
$ftp_password="apple";
if ($source_file!="") {
$mrph_connect = ftp_connect($ftp_server,21);
$mrph_login= ftp_login($mrph_connect, $ftp_username, $ftp_password);
if (($mrph_connect) && ($mrph_login)) {
$upload = ftp_put($mrph_connect, $destination_file, $source_file, FTP_BINARY);
if ($upload) echo "ok"; else echo "nok";
}
}
?>
<body>
<form enctype="multipart/form-data" action="3.php" method="POST">
<input type=file name=newsimg>
<input type=submit name=mrph>
</form>
</body>
The form calls itself to upload the file. When I select a file of size 1 or 2 KB it works but when I select a file of even 10 15KB the page timeouts after some time. I checked the php.ini settings file upload is on, I set temp folder as c:\uploads just to test. AS I SAID IT WORKS FOR FILES SIZE 1 OR 2KB BUT NOT EVEN WHEN I SELECT A FILE OF 10 OR 20KB. I even removed the PHP code (commented everything) to see even when nothing is done it works but it didn't.
Any help would be appreciated.
To me, the problem seems to be where you are uploading your file, the server; there is nothing wrong with uploading because you are able to upload smaller files but when you upload files of 20 kb size, you fail, check to make sure that right upload settings are specified on the server you want to upload the file to. Using ftp and uploading to a different server/location itself is slow process though. Your code also seems to be right.
My guess is that your ftp_put is timing out, try setting your FTP timeout threshold below PHP's default (30 seconds):
$mrph_connect = ftp_connect($ftp_server,21);
ftp_set_option($mrph_connect, FTP_TIMEOUT_SEC, 20);
$mrph_login= ftp_login($mrph_connect, $ftp_username, $ftp_password);
if (($mrph_connect) && ($mrph_login)) {
$upload = ftp_put($mrph_connect, $destination_file, $source_file, FTP_BINARY);
if ($upload) echo "ok"; else echo "nok";
}
If making that adjustment causes your script to return 'nok' then you'll know the put is taking too long.
If the put is your problem you try a non-blocking put with ftp_nb_put to FTP the file asynchronously:
$mrph_connect = ftp_connect($ftp_server,21);
$mrph_login= ftp_login($mrph_connect, $ftp_username, $ftp_password);
if (($mrph_connect) && ($mrph_login)) {
$ret = ftp_nb_put($mrph_connect, $destination_file, $source_file, FTP_BINARY);
while ($ret == FTP_MOREDATA) {
$ret = ftp_nb_continue($mrph_connect);
}
if ($ret == FTP_FINISHED) echo "ok"; else echo "nok";
}
I think Cryo is onto something, can it be that the php.ini file isn´t correctly configured and the maximum filesize is to low?
This might not be it but for the record your form should have a MAX_FILE_SIZE hidden input with the number of bytes corresponding to the max upload size
You might have a low filesize limit. To check this: create a new php file, called info.php or whatever and just write
<?php
phpinfo();
?>
Open that page in your browser, and search for upload_max_filesize. Check the value for that; if it is only a few kilobytes, that's your problem. If this is the case, you will have to modify your php.ini (under Apache you could use a directive in a .htaccess file as well, but I don't think there's anything like that for IIS). The location of this file can be different depending on your installation, but it's probably C:\Windows\php.ini. Find the upload_max_filesize directive and change it to something bigger. The default is 2 megabytes (2M) but you can make it whatever.