Set Max Size in CodeIgniter File Upload - php

I've used this private function to file upload in CodeIgniter Project. I've never set max_size in this function. Here, how to set max_size in this private function?
private function do_upload($value){
$type = explode('.', $_FILES[$value]["name"]);
$type = $type[count($type)-1];
$url = "./assets/uploads/products/".uniqid(rand()).'.'.$type;
if(in_array($type, array("jpg","jpeg","gif","png")))
if(is_uploaded_file($_FILES[$value]["tmp_name"]))
if(move_uploaded_file($_FILES[$value]["tmp_name"], $url))
return $url;
return "";
}

just simply do this.
if($_FILES["my_file_name"]['size'] > 1900000){ //set my_file_name to yours <input type="file" name="my_file_name">
echo "file is too large";
}else{
echo "file meets the minimum size";
}
if you want to set bigger upload size to your system, you need to go to php.ini file and change this.
upload_max_filesize = 2M; //change this to your desired size.
hope that helps.

You can get size of that file by $_FILES['uploaded_file']['size'],
for example:
$maxsize=2097152;
if(($_FILES['uploaded_file']['size'] >= $maxsize)) {
$errors[] = 'File too large. File must be less than 2 megabytes.';
}

you can create a php.ini file at your websites base folder
and add following (2M = 2 megabyte you can add your desired size )
upload_max_filesize = 2M;
on it

AS recommend: https://codeigniter.com/userguide3/libraries/file_uploading.html
2MB (or 2048 KB) So that:
$config['max_size'] = 1024 * 10; // <= 10Mb;

Related

PHP Limit upload size validation

I want to ask about PHP Limit upload size validation
I have made the code to limit size upload, but thats still get error
My limit size is 500 Kb
when I upload file above 500Kb to 2Mb, the validation is working
but when my file size above 2Mb, the validation isnt working
here is my first code
$maxsize = 500000;
if(($_FILES['myfile']['size'] >= $maxsize) || ($_FILES["myfile"]["size"] == 0)) {
$error = true;
array_push($error_cause, "<li>File size is over limit");
}
and this is my second code
if ($myfile->getSize() > 500000) {
$error = true;
array_push($error_cause, "<li>File size is over limit");
}
To make it clearer, i make a GIF about the problem
Here
Arithmetic 101: 5MB ===> 5 * 1024 * 1024 bytes
To keep code clear, I often define units as constants:
<?php
define('KB', 1024);
define('MB', 1048576);
define('GB', 1073741824);
define('TB', 1099511627776);
// Then you can simply do your condition like
ini_set('upload_max_filesize', 5*MB);
if (isset ( $_FILES['uploaded_file'] ) ) {
$file_size = $_FILES['uploaded_file']['size'];
$file_type = $_FILES['uploaded_file']['type'];
if (($file_size > 0.5*MB) && ($file_size < 2*MB)){
$message = 'File too large. File must be more than 500 KB and less than 2 MB.';
echo $message;
}
Simple method:
$min = 500; //KB
$max = 2000; //KB
if($_FILES['myfile']['size'] < $min * 1024 || $_FILES['myfile']['size'] > $max * 1024){
echo 'error';
}
The value of upload_max_filesize in php.ini is 2MB by default. Any file larger than this will be rejected by PHP.
You'll want to change the value in your php.ini file, and make sure to also adjust the post_max_size setting, as this is also considered for uploaded files.
When the uploaded file size is larger than the limit, the $_FILES array will be empty and you won't detect the upload at all (and thus, you won't show any errors).
You can see the actual uploaded file size by looking at the value fo $_SERVER['CONTENT_LENGTH'].

Upload the mp3 file in the folder?

I want to upload the mp3 file but it shows the message of :
"The uploaded file exceeds the maximum allowed size in your PHP configuration file."
my file is only 4.5 mb and this is my controller:
public function add_audio(){
$config['upload_path'] = './musics/';
$config['allowed_types'] = 'mp3|3gp|mpeg';
$config['max_size'] = '999999999999999999999';
$this->load->library('upload',$config);
chmod('musics/', 0777);
$this->upload->do_upload();
$data['audio'] = $_FILES['userfile']['name'];
if ( ! $this->upload->do_upload())
{
$data['error'] = $this->upload->display_errors();
print_r($data['error']);
//line of codes that displays if there are errors
}
else
{
$data['audio'] = $_FILES['userfile']['name'];
$this->load->model('main');
$query = $this->main->insert('audio',$data);
if($query == TRUE){
$this->load->view('admin/success');
}
}
}//end add
need help...
You need to increase upload_max_filesize and post_max_size in your php.ini. After change, restart http server to use new values.
; Maximum allowed size for uploaded files.
upload_max_filesize = 5M
; Must be greater than or equal to upload_max_filesize
post_max_size = 5M

PHP File Upload Size Issue

I am trying to upload PDF file using PHP Script, The below is my code. It works perfectly for any file which is less than 1 MB, but when I upload more than 1 MB, It goes to the else statement and gives this message - "Max File Size Upload Limit Reached, Please Select Any Other File".
I have already seen php.ini configuration, this is set at 16M. Please help
ini_set('upload_max_filesize', '16M');
ini_set('post_max_size', '16M');
//$ImageName=addslashes($_REQUEST['txtimagename']);
//$ImageTitle=addslashes($_REQUEST['txtimagetitle']);
$filepath="files/";
//$filedt=date("dmYhisu")."_.".pathinfo($_FILES['imagefile']['name'], PATHINFO_EXTENSION);
$filedt=$_POST['vehregistration'].".".pathinfo($_FILES['imagefile']['name'], PATHINFO_EXTENSION);
//basename($_FILES['photoimg']['name']);
$destination_img=$_POST['vehregistration'].".".pathinfo($_FILES['imagefile']['name'], PATHINFO_EXTENSION);
$filename=$filepath.$destination_img;
//$filename=$filepath.basename($_FILES['Photo']['name']);
//echo "$filepath".$_FILES[" imagefile "][" name "];
if(move_uploaded_file($_FILES["imagefile"]["tmp_name"], $filename))
{
//echo $filedt;exit;
//rename($_FILES['Photo']['tmp_name'],$filedt);
return $filedt;
}
else
{
echo "Max File Size Upload Limit Reached, Please Select Any Other File";
}
}
Add this in else condition $_FILES['imagefile']['error'] and see what is exact error . For more details http://php.net/manual/en/features.file-upload.errors.php
Instead of using ini_setuse $_FILES["imagefile"]["size"]
$fileSize = $_FILES["imagefile"]["size"]; // File size in bytes
$maxFileSz = 16777216; // set your max file size (in bytes) in this case 16MB
if($fileSize <= $maxFileSz) {
// everything is okay... keep processing
} else {
echo 'Max File Size Upload Limit Exceeded, Please Select Any Other File';
}

File not uploading and moving to folder

$image = $_FILES['picture']['name'];
$id=$_SESSION['id'];
//This function separates the extension from the rest of the file name and returns it
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
$ext = findexts ($_FILES['picture']['name']) ;
//This assigns the subdirectory you want to save into... make sure it exists!
$target = "mainimage/";
//This combines the directory, the random file name, and the extension
$target = $target . $id.".".$ext;
if(move_uploaded_file($_FILES['picture']['tmp_name'], $target))
{
echo ("This is your new profile picture!");
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
for some reason the "else" keeps showing up (there was a problem uploading the file). I put comments in everything that I am trying to do. please help! thanks!
To close the question as per OP's request.
The issue is that you need to increase the maximum size for uploads. PHP's default is usually 2M and you are most probably trying to upload a file larger than what the maximum setting is set to.
This can be achieved in two ways.
By editing your php.ini file with the following settings:
; Maximum allowed size for uploaded files.
upload_max_filesize = 40M
; Must be greater than or equal to upload_max_filesize
post_max_size = 40M
or via an .htaccess file placed in the root of your server:
php_value upload_max_filesize 20M
php_value post_max_size 30M

Is there a predefined upload limit to $_FILES in PHP?

Hi I'm fairly new to HTML, PHP, MySQL etc.. I am wondering if there is a predefined upload limit using $_FILES. I ask because when I try to upload 8 images of around 1.5 megabytes the code does not work but when I upload 10 images of around 60 kilobytes the code works fine.
Here is my code and feel free to make any criticisms/comments about it:
</head>
<body>
<form action="test.php" method="POST" enctype="multipart/form-data" >
<input type="file" name="image[]" multiple="multiple">
<input type="submit" value="upload">
</form>
<?php
include 'connect.php';
if(!empty($_FILES['image']['tmp_name'])){
$allowed = array('jpg', 'gif', 'png', 'jpeg');
$count = 0;
foreach($_FILES['image']['name'] as $key => $name){
$image_name = $name;
$tmp = explode('.', $image_name);
$image_extn = strtolower(end($tmp)); //can only reference file
$image_temp = $_FILES['image']['tmp_name'][$count];
$count = $count +1;
if(in_array($image_extn, $allowed) === true){
$image_path = 'images/' . md5($image_name) . '.' . $image_extn;
move_uploaded_file($image_temp, $image_path);
mysql_query("INSERT INTO store VALUES ('', '$image_name', '$image_path')") or die(mysql_error());
$lastid = mysql_insert_id();
$image_link = mysql_query("SELECT * FROM store WHERE id = $lastid");
$image_link = mysql_fetch_assoc($image_link);
$image_link = $image_link['image'];
$uploaded[] = $image_link;
}
else{
echo 'Incorrect file type. Allowed: ';
echo implode(', ', $allowed);
}
}
}
if(!empty($uploaded)){
foreach($uploaded as $new){
echo "<a href = $new>$new</a><p></p>";
}
}
else{
echo "Please select an image.";
}
?>
</body>
</html>
You are trying to upload an array of files, you will not be able to upload more than 20 files due to max_file_uploads limit in php.ini which is by default set to 20.
So you have to increase this limit to upload more than 20 files.
Note: max_file_uploads can NOT be changed outside php.ini. See PHP "Bug" #50684
Here are the settings you want to change in php.ini:
post_max_size
This setting controls the size of an HTTP post, and it needs to be set larger than the upload_max_filesize setting.
upload_max_filesize
This value sets the maximum size of an upload file.
Remember to restart your web server after making these changes.
Ref:
http://www.php.net/manual/en/ini.core.php#ini.post-max-size
http://www.php.net/manual/en/ini.core.php#ini.upload-max-filesize
A few settings in your php.ini could be causing this. Look into memory_limit, post_max_size, upload_max_filesize. You could also be timing out. The best way to find out specifically is error_reporting(E_ALL);ini_set('display_errors','1');
In php.ini, there will be a post_max_size and upload_max_filesize directive.
You should also define a MAX_FILE_SIZE hidden input within your form, as per
http://www.php.net/manual/en/features.file-upload.post-method.php

Categories