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';
}
Related
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'].
I am uploading a file which is an image. I want to get the size of that image every time in bytes only using PHP. I had done this by far
$name=$_FILES['image']['name'];
if($name!=null)
{
$tmpDest=$_FILES['image']['tmp_name'];
$size=$_FILES['image']['size'];
$perDestination="main/$name";
$result=move_uploaded_file($tmpDest,$perDestination);
echo $size;
}
Your code is right, the below line will give you the size in bytes:
size=$_FILES['image']['size'];
You can also get the file size after the file has been uploaded this way:
echo filesize($perDestination) . ' bytes';
This option will also give you the file size in bytes
You can check like this
<?php
if(isset($_FILES['file']) {
if($_FILES['file']['size'] > 10485760) { //10 MB (size is also in bytes)
// File too big
} else {
// File within size restrictions
}
}
check this out
http://www.w3schools.com/php/php_file_upload.asp
I had asked this question and found out that the PHP configuration was limiting my uploads to 2MB, so I fixed it to 3MB. However, I have another problem now: I am also checking for image dimension and it is failing if the image appears to be over 3MB, throwing the below error. So how could I stop the error and check the size by myself instead of the PHP config?
Warning: getimagesize(): Filename cannot be empty
Here is the code:
$size = $_FILES['image']['size'];
list($width, $height) = getimagesize($_FILES['image']['tmp_name']);
$minh = 400;
$minw = 600;
$one_MB = 1024*1024; //1MB
if($size > ($one_MB*3))// this is not checking the size at all, the last echo is what I get if I try to upload over 3mb.
{
echo"File exceeds 3MB";
}
elseif ($width < $minw ) {
echo "Image width shouldn't be less than 600px";
}
elseif ($height < $minh){
echo "Image height shouldn't be less than 400px";
}
else{
if (move_uploaded_file($_FILES['image']['tmp_name'], $new_location)){
//do something
}
else{
echo "Image is too big, try uploading below 3MB";
}
}
Looks like the upload went wrong at some point. Try adding some error handling before getimagesize():
if (!file_exists($_FILES['image']['tmp_name'])) {
echo "File upload failed. ";
if (isset($_FILES['upfile']['error'])) {
echo "Error code: ".$_FILES['upfile']['error'];
}
exit;
}
It's hard to say why it has failed, as there are many parameters involved. The Error code might give you a hint.
Otherwise maybe start reading here: http://de2.php.net/manual/en/features.file-upload.php
I was also having the same error, go to php.ini and go to line 800 "upload_max_filesize".
Choose your max upload file size, i set mine to 4M and in my actual website code I dont allow anything after 3mb.
For some reason having them both the same size doesnt work, PHP.ini needs to be atleast 1mb bigger than what you allow to be uploaded.
I need to upload a video file. Here's my code
if(($_FILES["file"]["type"]=="application/octet-stream") || ($_FILES["file"]["type"]=="video/x-ms-wmv"))
{
if($_FILES["file"]["error"] > 0)
{
echo "Error: ".$_FILES["file"]["error"]."<br />";
}
else if(file_exists("videos/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],"videos/".$_FILES["file"]["name"]);
$filename=$_FILES["file"]["name"];
$type=$_FILES["file"]["type"];
$size=($_FILES["file"]["size"]/1024);
$path="".$_FILES["file"]["name"];
if(($ins=mysql_query("insert into achieva_video values('','".$_REQUEST['vname']."','".$_REQUEST['courid']."','".$filename."','".$path."','".$size."','".$type."','Active')"))==true)
{
header("location:viewcoursevideo.php?ins=1");
}
else
{
echo("<script>alert('Failure Please try again later');</script>");
}
}
}
else
{
echo "<script>alert('Invalid File Type');</script>)";
}
I'm bit confused with this warning message when I try to upload a video file.
"PHP Warning: POST Content-Length of 9311816 bytes exceeds the limit of 8388608 bytes in Unknown on line 0"
I've set the following preferences in the php ini:
memory limit = 150M
upload file size = 120M
post max size = 120M
The file has not been updated. it takes long time and just shows this warning.
the message seems clear: you have an upload limit set to 8M (8388608 bytes) and you are uploading a file of 9M (9311816 bytes). Are you really sure these settings in php.ini are working?
memory limit = 150M
upload file size = 120M
post max size = 120M
Shouldn't it be like this? (underscores, max!)
memory_limit = 150M
upload_max_filesize = 120M
post_max_size = 120M
here is the code that i use to upload files and unzip them in a directory.
But the problem is that it seems to be very slow on files greater than 5MB.
I think it doesnt have to do with the network because it is in localhost computer.
Do I need to edit any parameter in php.ini file or apache or any other workarround?
$target_path = "../somepath/";
$target_path .= JRequest::getVar('uploadedDirectory')."/";
$target_Main_path= JRequest::getVar('uploadedDirectory')."/";
$folderName = $_FILES['uploadedfile']['name'];
$target_path = $target_path . basename($folderName);
//upload file
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path."/")) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
$zip = new ZipArchive;
$res = $zip->open($target_path);
$target_path = str_replace(".zip", "", $target_path);
echo $target_path;
if ($res === TRUE) {
$zip->extractTo($target_path."/");
$zip->close();
echo "ok";
} else {
echo "failed";
}
When handling file uploads ther are a lot of factors to consider. These include PHP settings:
max_input_time
max_execution_time
upload_max_filesize
post_max_size
Execution time can affect the upload if for example, you have a slow upload speed resulting in a timeout.
File size can cause problems if the upload is larger than the upload_max_filesize like wise if your file size + the rest of the post data exceeds post_max_size.
Zip uses a lot of memory in the archive/extraction processes and could easily exceed the memory allocated to PHP -so that would be worth checking as well.
I would start with this page and note some of the comments.