File upload not working properly in PHP - php

I have created a file upload in PHP. the max file size is 5MB. I'm trying to upload a file which is 39 MB.
The file upload is working fine for all the files which are of 5MB or less than 5MB, it is even working fine with 30 MB file and returning ERROR: MAX file size limit exceeds. But only in this case 39MB it is returning the below error.
I'm getting below error.
**action.php**
<form class="w3-container " action='file_upload.php' method='post' enctype="multipart/form-data" name="form" id="form-e" role="form">
<DIV id="file_name_div">
<label for="file_name">Enter file name: </label><br>
<input data-validation="filename" autocomplete="off" style="width:30%" class="w3-input w3-border w3-light-grey " type="text" name='file_name' id='file_name' autofocus />
<label for="Role">Enter Role: </label><br>
<input data-validation="role" style="width:30%" class="w3-input w3-border w3-light-grey " type="text" name='role' id='role' autofocus />
</DIV>
<br>
<button type="reset" class="btn btn-info btn-md w3-blue"><span class="glyphicon glyphicon-refresh"></span> Reset</button>
</form>
****file_upload.php****
<?php
session_start();
include_once 'config.php';
$message = '';
$fileName = $_POST['file_name']; //LINE NO 6
$role = $_POST['role']; #role code //LINE NO 7
$dir= "files/$role/"; #uploading file to selected role
foreach ($_FILES as $filename => $filearray) {
if (is_uploaded_file($filearray['tmp_name'])) {
$fileLogicalName = $filearray['name'];
$fileSize = (int) ($filearray['size'] / 1024);
$filePhysicalName = pathinfo("$fileLogicalName"); //Return complete file name with extension
$filePhysicalName['filename']; //The arrray of pathinfo return only file name without extension
if ($fileSize <= 5120) // 5mb/1024
{
//file upload code
}
}
}
?>

try add more size to the file limit
ini_set('memory_limit','1028M');
ini_set('upload_max_filesize','1028M');
ini_set('post_max_siz','1028M');

Related

Create a PHP contact form with multiple image upload

I need to create an html contact form which collects some data and uploads 8 images through a PHP script
This is my HTML:
<form action="formmail.php" onsubmit="return controlloform()" id="form" name="form" method="POST" enctype="multipart/form-data">
<input name="email" id="email" placeholder="Email address" type="text" value="" maxlength="40">
<input name="tel" id="tel" type="text" placeholder="Order number" value="" maxlength="20">
<textarea name="msg" placeholder="Message" value="" maxlength="300"></textarea>
Upload a photo:
<p>
<p>
<input name="file" id="file" class="button" type="file" value="">
<p>
<p>
<td colspan="2">
<input type="submit" class="button" value="Submit" />
</td>
</tr>
</table>
</form>
I don't know how to create the upload script, what I need to do is uploading 2 blocks of 4 images, check the extension of the file (only jpg and png allowed) and the size (max 500 kb for image).
I've already found some upload scripts (not 100% suitable for what I need) but I don't know how to include the upload part into the rest of the code
If anyone could help me would be great
Thanks a lot
This code make multiple upload
$total = count($_FILES['photo_file']['name']);
// Loop through each file
for($i=0; $i<$total; $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['photo_file']['tmp_name'][$i];
//Make sure we have a filepath
if ($tmpFilePath != ""){
//Setup our new file path
$newFilePath = "./uploadFiles/" . $_FILES['photo_file']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
//Handle other code here
}
}
}
if you want to check file size insert this code
if ($_FILES["photo_file"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
this code make validation
$allowed = array('gif','png' ,'jpg');
$filename = $_FILES['photo_file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed) ) {
$uploadOk = 0;
}

tmp_name not holding my uploaded file

I am trying to run a php script to upload an image to a location on my server while storing its the location details in my database.
All seems fine except my tmp_name doesn't seem to be getting the file so all I get is the string location in the database pointing to a file not existing in my server. I couldn't figure out this one.
First this is the html markup:
$imgsize=$_POST['MAX_FILE_SIZE'];
$name = $_FILES["file"]["name"];
$location = 'db/farmers_pPix/';
$load_dataFarmerPix="form/".$location.$FNAME.'_'.$LNAME.'_'.$FNO.'-'.$name;
$passport=$load_dataFarmerPix;
...
$tmp_name = $_FILES['file']['tmp_name'];
$error = $_FILES['file']['error'];
if (!empty($name)) {
move_uploaded_file($tmp_name, $load_dataFarmerPix);
if (move_uploaded_file($tmp_name, $load_dataFarmerPix)){
echo '<p style="width:100%; color:#777; font-family:eurof35-webfont;"><h4 class="text-center">Your profle has been created and photo Uploaded</h4></p>';
}
} else {
echo '<p style="width:100%; color:#777; font-family:eurof35-webfont;text-align:center;" class="text-center"><h5>please choose a file</h5></p>';
}
echo '<p style="width:100%; color:#777; font-family:eurof55-webfont;text-align:center;"><h3 class="text-center">Registration Successfull</h3></p>';
}
}
<form enctype="multipart/form-data" id="myform" class="fs-form fs-form-full" autocomplete="off" style="color:#ccc;" action="farmreg2.php" method="post">
...
<li>
<label class="fs-field-label fs-anim-upper" for="q5">Upload a Current Photo? </label>
<input class="fs-anim-lower" style="color:#ccc; padding:1%;" id="id" name="file" type="file" placeholder="Supported formats (jpg,png,bmp,gif)"/>
<input class="fs-anim-lower" id="id" value="30000" name="MAX_FILE_SIZE" type="hidden" placeholder="" required/>
</li>
...
</form>
this file itself is farmreg2.php

Issues with Php Music File Uploader

So I'm trying to upload music files with php but the files aren't going to the folder, here's my code what are my mistakes and what am i missing
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="text" placeholder="File Name" name="file_name">
<input type="text" placeholder="Artist Name" name="artist_name">
<textarea rows="5" placeholder="Description" name="description"></textarea>
<input type="submit" class="btn btn-warning bgcolor" name="submit" value="Upload"/>
</form>
<?php
if(isset($_POST['submit'])){
function GetMusicExtension($musictype)
{
if(empty($musictype)) return false;
switch($musictype){
case 'audio/mp3': return '.mp3';
default: return false;
}
}
if (!empty($_FILES["file"]["name"])) {
$file_name=$_FILES["file"]["name"];
$temp_name=$_FILES["file"]["tmp_name"];
$music_type=$_FILES["file"]["type"];
$size = $_FILES['file']['size'];
$ext= GetMusicExtension($music_type);
$musicname = $_POST['file_name'].$ext;
$target_path = "music/".$musicname;
if(move_uploaded_file($temp_name, $target_path)) {
$query = "insert into music(name,artist,description)
values('".$musicname."','".$_POST['artist_name']."','".$_POST['description']."')";
mysqli_query($link,$query) or die("error in $query == ----> ".mysqli_error($link));
}
}else{
exit('<script>alert("Error Saving Data... try again");</script>');
}
}
?>
PS: If i change the mime to an image mime and try uploading it uploads
UPDATE:
If i change post_max_size and upload_max_size it works. Thanks all.

I can't upload pdf files in php

Now i am trying to upload pdf files by the form in php but when i upload it, it doesn't work because function is_uploaded_file($_FILES['file']['tmp_name']) return false
How can i solve it please ??
I am using localhost and I verify php.ini
; 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 = "E:/wamp/tmp"
; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 50M
This is my code :
Class
<?php
require '../includes/master.inc.php';
// Kick-out users who aren't logged in as an admin
$auth->requireAdmin('../login_admin_cp.php');
if (isset($_POST['btnsubmit'])) {
$error->blank($_POST['field_name'], "field_name");
if ($error->ok()) {
$field_name = $_POST['field_name'];
if (is_uploaded_file($_FILES['file']['tmp_name'])) {
// exit;
$mypath = "upload/";
$file_name = $_FILES['file']['name'];
$tmp = $_FILES['file']['tmp_name'];
$n = $mypath . $file_name;
$t = $_FILES['file']['size'];
$filesize = round($t / 1024) . " KB";
$extintion = strchr($n, ".");
$extintion = strtolower($extintion);
$file_extintion_allow = array(".pdf");
if ($n == "") {
echo "Error in upload";
} elseif (!in_array($extintion, $file_extintion_allow)) {
echo "من فضلك ادخل المادة بطريقة صحيحة";
exit;
} else {
//rename the file when upload by query name
$quaryname = rand(11111, 99999);
$rename = "../" . $mypath . $quaryname . $extintion;
$URL = substr($rename, 10, strlen($rename));
move_uploaded_file($tmp, $rename);
if (!empty($_POST['hide'])) {
$hide = $_POST['hide'];
$db->query("insert into field (field_name,url,hide)values('$field_name','$URL','$hide')");
$sess->set_flashdata('success_msg', 'تم حفظ المادة بنجاح');
redirect("field_new.php");
} else {
$db->query("insert into field (field_name,url,hide)values('$field_name','$URL','0')");
$sess->set_flashdata('success_msg', 'تم حفظ المادة بنجاح');
redirect("field_new.php");
}
}
} else {
exit;
}
} else {
$sess->set_flashdata('error_msg', 'حقل المادة فارغ');
}
}
// View
include('views/header.php');
include('views/field_form_new.php');
include('views/footer.php');
?>
Form
<!-- Form elements -->
<div class="grid_9">
<div class="module">
<h2><span> رفع المادة</span></h2>
<div class="module-body">
<?php echo $error;?>
<form action="field_new.php" method="post" name="myform" enctype="multipart/form-data">
<p>
<label> اسم المادة</label>
<input type="text" class="input-medium" name="field_name" value="" />
</p>
<p style="font-size:14px"> رفع المادة </p>
<p>
<label> الرابط</label>
<input type="text" class="input-medium" name="link" value="<?php $link; ?>" />
</p>
أو<br/> <br/>
<p>
<input type="hidden" class="input-medium" name="MAX_FILE_SIZE" value="1000000">
<label>رفع الملف </label>
<input type="file" class="input-medium" name="file" />
</p>
<p>
<input type="checkbox" name="hide" value="1" />تفعيل المادة
</p>
<fieldset>
<input class="submit-green" type="submit" name="btnsubmit" value="اضافة" />
</fieldset>
</form>
</div> <!-- End .module-body -->
</div> <!-- End .module -->
<div style="clear:both;"></div>
</div> <!-- End .grid_6 -->
Maybe the file is to big or the destination directory is not writeable for php/apache.
Check the folder permission "E:/wamp/tmp" and try to upload a smaller file.
If a smaller file works, check these values in the php.ini file:
post_max_size
max_input_time
memory_limit

PHP: Image not uploading to folder

Hey guys I think i need a fresh set of eyes on this. I have an insert script that inserts data to the database along with the URL of an image that gets uploaded to a folder called uploads. The problem is that while all the information goes into the database the image never gets uploaded to the folder. Does anyone know why?
<?php
$date=$_POST['date'];
$title=$_POST['title'];
$body=$_POST['body'];
$month=$_POST['month'];
$file = $_FILES['file'];
$name = $file['name'];
$path = "uploads/" . basename($name);
$sql="INSERT INTO content (date, title, body, month, pic_id) VALUES ('$date','$title', '$body', '$month', '" . mysql_real_escape_string($path) . "')";
$result=mysql_query($sql);
if($result && move_uploaded_file($file['tmp_name'], $path) ){
echo 'Query has been inputted into the database';
}
else{
echo 'An error occured';
}
?>
My html looks like this:
<h1>Post A Blog </h1></br>
<form name="personal_information" method="post" enctype="multipart/form-data" action="insert.php">
<label>
<span>Date:<br></span><input id="date" type="date" name="date" />
</label><br><br>
<label>
<span>Title:<br></span><input id="title" type="text" name="title" />
</label><br><br>
<label>
<span>Body:<br></span><textarea id="body" type="text" name="body"/></textarea>
</label><br><br>
<label>
<span>Month:<br></span><input id="month" type="text" name="month" />
</label><br><br>
<br>
Upload file:</br>
<input type="file" name="file" id="fileupload"></br></br>
<input type="submit" name="submit" value="Submit" id="submit"/>
I'm on my phone so can't give you an example but I'm pretty sure that you have to use the fullpath as destination.
try __DIR__ . '/'. $path or use document_root
move_uploaded_file() - Moves an uploaded file to a new location.
is_uploaded_file() - Tells whether the file was uploaded via HTTP POST
This will work for you:
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
echo "File ". $_FILES['userfile']['name'] ." uploaded successfully.\n";
}
More detail and example

Categories