Php code
$target = "upload/";
$nameF = "";
$targetImage = "upload/";
$nameI = "";
if (!empty($_FILES['fileUP']['name'])) {
print_r("ce il file");
$target = $target . basename($_FILES['fileUP']['name']);
$nameF = $_FILES['fileUP']['name'];
if (!move_uploaded_file($_FILES['fileUP']['tmp_name'], $target)) {
echo -1;
}
}
if (!empty($_FILES['imageUP']['name'])) {
$targetImage = $targetImage . basename($_FILES['imageUP']['name']);
$nameI = $_FILES['imageUP']['name'];
if (!move_uploaded_file($_FILES['imageUP']['tmp_name'], $targetImage)) {
echo -1;
}
}
$title = $_POST['title'];
$admin = $_POST['admin'];
$content = $_POST['content'];
$sql = "INSERT INTO news (title,admin,content,img,file) values('$title','$admin','$content','$nameI','$nameF')";
$result = $conn->query($sql) or die(mysql_error());
if ($result === TRUE) {
echo 1;
} else {
echo -1;
}
Form
<form enctype="multipart/form-data" id="insert" class="bs-example bs-example-form" method="POST">
<div class="input-group">
<span class="input-group-addon">Titolo</span>
<input id="title" name="title" type="text" class="form-control" placeholder="Titolo">
</div>
<br>
<div class="input-group">
<span class="input-group-addon">Admin</span>
<input id="admin" name="admin" type="text" class="form-control" value='{{$utente|lower}}'
placeholder='{{$utente}}'>
</div>
<br><br> <br> <br>
<div class="input-group">
<span class="input-group-addon">Immagine</span>
<input id="image" name="imageUP" accept="image/*" type="file" class="form-control"
placeholder="Immagine">
</div>
<br>
<div class="input-group">
<span class="input-group-addon">File</span>
<input id="image" name="fileUP" id="fileToUpload" type="file" class="form-control"
placeholder="FIle">
</div>
<br>
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-font"></span></span>
<input id="content" name="content" type="text" class="form-control"
placeholder="Contenuto">
</div>
<br>
<button id="crea" type="submit" class="btn btn-warning">Crea</button>
</form>
Ajax request
$('#insert').submit(function (e) {
e.preventDefault();
var data = new FormData($(this)[0]);
$.ajax
({
url: 'uploads.php',
data: data,
type: 'post',
processData: false,
contentType: false,
success: function (response) {
response = parseInt(response);
switch (response) {
case -1: //errore generico
alert("errore");
break;
case 1:
alert("la creazione della news è andata a buon fine");
break;
}
close ajax call..
My problem is:
the script work but not well, i've notice that query don't insert data if i put text in the 'content' input and upload image.
In the console I've this error:
not allowed to load local resource: file:///C:/fakepath/xx.jpg
when i work in localhost i haven't this error and query ALWAYS insert data. Now i've problem and i am in real server.
Anyone know to fix it?ù
I need your help
Try this. And, let me know. Use whole code as it is. I will explain in few minutes. First try.
$target = "upload/";
$nameF = "";
$targetImage = "upload/";
$nameI = "";
$flag = 1;
if (!empty( $_FILES['fileUP']['name'])) {
print_r("ce il file");
$target = $target . basename( $_FILES['fileUP']['name']);
$nameF =$_FILES['fileUP']['name'];
if (!move_uploaded_file($_FILES['fileUP']['tmp_name'], $target)) {
$flag = -1;
}
}
if (!empty( $_FILES['imageUP']['name'])) {
$targetImage = $targetImage . basename( $_FILES['imageUP']['name']);
$nameI =$_FILES['imageUP']['name'];
if (!move_uploaded_file($_FILES['imageUP']['tmp_name'], $targetImage)) {
$flag = -1;
}
}
$title = $_POST['title'];
$admin = $_POST['admin'];
$content = $_POST['content'];
$sql = "INSERT INTO news (title,admin,content,img,file) values('$title','$admin','$content','$nameI','$nameF')";
$result = $conn->query($sql) or die(mysql_error());
if ($result === TRUE) {
$flag = 1;
}
else {
$flag = -1;
}
if($flag == -1){
echo -1;
} else {
echo 1;
}
Related
I created a new function to change the name of the file to be uploaded. Now I have successfully changed the file name. but in my code, before it loops through the files normally it gives an error.
I have 5 files inputs where the input is in 1 form.
There was no problems before adding my function, but after adding the rename function, my loop became messy, but my old code was working correctly.
here is my old code:
public function proses_upload()
{
$gambar = array();
$jumlah = count($_FILES['userfile']['name']);
for ($i=0; $i < $jumlah; $i++)
{
$file_name = $_FILES['userfile']['name'][$i];
$tmp_name = $_FILES['userfile']['tmp_name'][$i];
move_uploaded_file($tmp_name, "file/".$file_name);
$gambar[$i] = $file_name;
}
$nama_file1 = $gambar[0];
$format_p2ptm =1;
$tanggal = date("Y-m-d H:i:s");
$jenis ='p2ptm';
$nama_file2 = $gambar[1];
$format_p2ptm2 =2;
$jenis ='p2ptm';
$nama_file3 = $gambar[2];
$format_p2ptm3 =3;
$jenis ='p2ptm';
$nama_file4 = $gambar[3];
$format_p2ptm4 =4;
$jenis ='p2ptm';
$nama_file5 = $gambar[4];
$format_p2ptm5 =5;
$jenis ='p2ptm';
if (!$nama_file1 == "") {
$this->app_model->insert_data('penyakit', $format_p2ptm, $tanggal,$nama_file1,$jenis, $this->session->userdata('id_puskesmas'));
}
if(!$nama_file2 == ""){
$this->app_model->insert_data('penyakit', $format_p2ptm2, $tanggal,$nama_file2,$jenis,$this->session->userdata('id_puskesmas'));
}
if(!$nama_file3 == ""){
$this->app_model->insert_data('penyakit', $format_p2ptm3, $tanggal,$nama_file3,$jenis,$this->session->userdata('id_puskesmas'));
}
if(!$nama_file4 == ""){
$this->app_model->insert_data('penyakit', $format_p2ptm4, $tanggal,$nama_file4,$jenis,$this->session->userdata('id_puskesmas'));
}
if(!$nama_file5 == ""){
$this->app_model->insert_data('penyakit', $format_p2ptm5, $tanggal,$nama_file5,$jenis,$this->session->userdata('id_puskesmas'));
}
redirect('puskesmas/view_puskesmas');
}
Here is my new code with errors:
public function proses_upload()
{
$gambar = array();
$jumlah = count($_FILES['userfile']['name']);
for ($i=0; $i < $jumlah; $i++)
{
$file_name = $_FILES['userfile']['name'][$i];
$explode = explode('.',$file_name);
// die(print_r($explode));
$ekstensi = pathinfo($file_name, PATHINFO_EXTENSION);
$bulan = date('M');
$tahun = date('y') ;
$kode = $this->session->userdata('kode_puskesmas');
$new_file_name = $explode[1].'_'.$kode.'_'.$bulan.$tahun.'.'.$ekstensi;
$tmp_name = $_FILES['userfile']['tmp_name'][$i];
move_uploaded_file($tmp_name, "file/".$new_file_name);
$gambar[$i] = $new_file_name;
}
$nama_file1 = $gambar[0];
$format_p2ptm =1;
$tanggal = date("Y-m-d H:i:s");
$jenis ='p2ptm';
$nama_file2 = $gambar[1];
$format_p2ptm2 =2;
$jenis ='p2ptm';
$nama_file3 = $gambar[2];
$format_p2ptm3 =3;
$jenis ='p2ptm';
$nama_file4 = $gambar[3];
$format_p2ptm4 =4;
$jenis ='p2ptm';
$nama_file5 = $gambar[4];
$format_p2ptm5 =5;
$jenis ='p2ptm';
if (!$nama_file1 == "") {
$this->app_model->insert_data('penyakit', $format_p2ptm, $tanggal,$nama_file1,$jenis, $this->session->userdata('id_puskesmas'));
}
if(!$nama_file2 == ""){
$this->app_model->insert_data('penyakit', $format_p2ptm2, $tanggal,$nama_file2,$jenis,$this->session->userdata('id_puskesmas'));
}
if(!$nama_file3 == ""){
$this->app_model->insert_data('penyakit', $format_p2ptm3, $tanggal,$nama_file3,$jenis,$this->session->userdata('id_puskesmas'));
}
if(!$nama_file4 == ""){
$this->app_model->insert_data('penyakit', $format_p2ptm4, $tanggal,$nama_file4,$jenis,$this->session->userdata('id_puskesmas'));
}
if(!$nama_file5 == ""){
$this->app_model->insert_data('penyakit', $format_p2ptm5, $tanggal,$nama_file5,$jenis,$this->session->userdata('id_puskesmas'));
}
redirect('puskesmas/view_puskesmas');
}
Here is my view form:
<?php echo form_open_multipart('puskesmas/proses_upload');?>
<br><br><br>
<p>
<h4>1. Form Lap PTM</h4>
</p>
<p>
<label>FORMAT : </label>
<input type="text" name="format" style="width:230px; height:25px; border:2; "
placeholder="Form Lap PTM" readonly>
</p>
<label >Input file : </label>
<input type="file" name="userfile[]">
<br><br><br>
<p>
<h4>2. Form Lap Posbindu</h4>
</p>
<p>
<label>FORMAT : </label>
<input type="text" name="format2" style="width:230px; height:25px; border:2; "
placeholder="Form Lap Posbindu " readonly =>
</p>
<label>Input file : </label>
<input type="file" name="userfile[]">
<br><br><br>
<p>
<h4>3. Form Lap IVA</h4>
</p>
<p>
<label>FORMAT : </label>
<input type="text" name="format3" style="width:230px; height:25px; border:2; "
placeholder="Form Lap IVA " readonly>
</p>
<label>Input file : </label>
<input type="file" name="userfile[]">
<br><br><br>
<p>
<h4>4. Form Lap Jiwa</h4>
</p>
<p>
<label>FORMAT : </label>
<input type="text" name="format4" style="width:230px; height:25px; border:2; "
placeholder="Form Lap Jiwa" readonly>
</p>
<label>Input file : </label>
<input type="file" name="userfile[]">
<br><br><br>
<p>
<h5>5. Form Lap Indera dan Gimul</h5>
</p>
<p>
<label>FORMAT : </label>
<input type="text" name="format5" style="width:230px; height:25px; border:2; "
placeholder="Form Lap Indera_dan Gimul" readonly>
</p>
<label>Input file : </label>
<input type="file" name="userfile[]">
<br><br><br>
<!-- <button class="" type="submit">Upload</button> -->
<input class="btn btn-primary btn-lg" type="submit" value="Upload">
<?php echo form_close() ?>
if I upload files one of the 5 inputs that for the column I fill in the input is true, but why do the remaining 4 other inputs also enter data? even though I only input 1 input
its my phpmyadmin foto input
There will always be 5 files sent due to 5 inputs being sent, you need to verify whether or not they are empty or filled.
$jumlah = count(array_filter($_FILES['userfile']['name']));
Will give you the true count value of how many files have been uploaded, removing the blanks. You would ther need to update the rest of your code to accommodate for there only being x amount of array values.
To keep your current code with minimal changes you can add an if statement to check if the name is blank
$file_name = $_FILES['userfile']['name'][$i];
$gambar[$i] = ''; //set it to blank by default for your checks further down
if($file_name != ''){
$explode = explode('.',$file_name);
// die(print_r($explode));
$ekstensi = pathinfo($file_name, PATHINFO_EXTENSION);
$bulan = date('M');
$tahun = date('y') ;
$kode = $this->session->userdata('kode_puskesmas');
$new_file_name = $explode[1].'_'.$kode.'_'.$bulan.$tahun.'.'.$ekstensi;
$tmp_name = $_FILES['userfile']['tmp_name'][$i];
move_uploaded_file($tmp_name, "file/".$new_file_name);
$gambar[$i] = $new_file_name;
}
code:
<?php
if(isset($_POST['submit']))
{
if(isset($_FILES['img_url']['name']))
{
for($i=0; $i<count($_FILES['img_url']['name']); $i++)
{
$tmpFilePath = $_FILES['img_url']['tmp_name'][$i];
if ($tmpFilePath != "")
{
$path = "../images/hotel_images/";
$name = $_FILES['img_url']['name'][$i];
$size = $_FILES['img_url']['size'][$i];
list($txt, $ext) = explode(".", $name);
$file= time().substr(str_replace(" ", "_", $txt), 0);
$info = pathinfo($file);
$filename = $file.".".$ext;
$filepath_upload = $path.$filename;
$imageFileType = strtolower(pathinfo($filename,PATHINFO_EXTENSION));
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" )
{
$j2 = $i+1;
$msg = "<div class='alert alert-success'>Sorry, ".$j2." Image files Extension .".$ext." are not Uploaded & Other files Uploaded Successfully <br/>.</div>" ;
}
else if(move_uploaded_file($_FILES['img_url']['tmp_name'][$i], $filepath_upload))
{
$banner_image_source_file = $filepath_upload;
$banner_image_save_file = $filepath_upload;
list($width_orig, $height_orig) = getimagesize($banner_image_source_file);
$img_url_name.=$filename."|";
}
}
$filepath = rtrim($img_url_name, '|');
}
if(!empty($filepath))
{
$query = mysqli_query($con, "INSERT INTO `hotel`(`img_url`, `status`) VALUES ('".$filepath."')");
if($query)
{
$msg .= "<div class='alert alert-success'>Hotel Record Added Successfully</div>";
}
else
{
$msg = "<div class='alert alert-success'>Unbale to Add Hotel Record Please Try Again !!!</div>";
}
}
else
{
$msg = "<div class='alert alert-success'>Unable to Add Please Try Again !! </div>";
}
}
else
{
$msg = "<div class='alert alert-success'>Unable to Add Please Try Again !! </div>";
}
}
?>
<form action="" method="POST" enctype="multipart/form-data" id="add-hotel-form">
<div class="form-group col-md-12">
<label for="hotel">Upload Hotel Images</label>
<input type="file" class="form-control-file" id="upload_hotel_img1" name="img_url[]" multiple>
</div>
<div class="form-group col-md-12">
<label for="room">Upload Room Images</label>
<input type="file" class="form-control-file" id="upload_room_img1" name="img_url2[]" multiple>
</div>
<input type="submit" class="btn btn-primary" name="submit" value="Submit">
</form>
In the following code I have create a simple form where I have two input field type='file' where I am able to upload only first one i.e. <label for="hotel">Upload Hotel Images</label> and move into the folder successfully but I also want to upload and move second input field i.e. <label for="room">Upload Room Images</label>. I don't have any idea about this. So, How can I do this? Please help me.
Thank You
Your Room Image input is called img_url2, which you never interact with in code.
For best practice, move the bulk of your PHP code into a file upload function, then call it once with img_url $_FILE input, and once with img_url2.
Example:
<?php
error_reporting(E_ALL);
ini_set("display_errors", true);
ini_set("display_startup_errors", true);
function uploadFile($file, $path)
{
$img_url_name = "";
if(is_array($file) && isset($file['name']) && isset($file['tmp_name']) && isset($file['size']))
{
$tmpFilePath = isset($file['tmp_name'][0]) ? $file['tmp_name'][0] : "";
$name = isset($file['name'][0]) ? $file['name'][0] : "";
list($txt, $ext) = explode(".", $name);
$file = time().substr(str_replace(" ", "_", $txt), 0);
$filename = $file.".".$ext;
$filepath_upload = $path.$filename;
$imageFileType = strtolower(pathinfo($filename,PATHINFO_EXTENSION));
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" )
{
return "<div class='alert alert-success'>Sorry, ".$name." Image files Extension .".$ext." are not Uploaded & Other files Uploaded Successfully <br/>.</div>" ;
}
else if(move_uploaded_file($tmpFilePath, $filepath_upload))
{
$img_url_name .= $filename."|";
}
$filepath = rtrim($img_url_name, '|');
if(!empty($filepath))
{
if(!isset($con))
{
global $con;
}
$query = mysqli_query($con, "INSERT INTO `hotel`(`img_url`, `room_url`) VALUES ('".$filename."','".$filepath."')");
if($query)
{
return "<div class='alert alert-success'>Hotel Record Added Successfully</div>";
}
}
}
return "<div class='alert alert-success'>Unable to Add Please Try Again !! </div>";
}
if(isset($_POST['submit']))
{
if(isset($_FILES['img_url']['name']))
{
echo uploadFile($_FILES['img_url'], "../images/hotel_images/");
}
if(isset($_FILES['img_url2']['name']))
{
echo uploadFile($_FILES['img_url2'], "../images/room_img/");
}
}
?>
<form action="" method="POST" enctype="multipart/form-data" id="add-hotel-form">
<div class="form-group col-md-12">
<label for="hotel">Upload Hotel Images</label>
<input type="file" class="form-control-file" id="upload_hotel_img1" name="img_url" multiple>
</div>
<div class="form-group col-md-12">
<label for="room">Upload Room Images</label>
<input type="file" class="form-control-file" id="upload_room_img1" name="img_url2" multiple>
</div>
<input type="submit" class="btn btn-primary" name="submit" value="Submit">
</form>
I use FormData() to upload multiple images from multiple fields. Please take a look at this code:
<head>
<title>Example</title>
</head>
<body>
<form enctype="multipart/form-data" id="add-hotel-form">
<div class="form-group col-md-12">
<label for="hotel">Upload Hotel Images</label>
<input type="file" class="form-control-file" id="upload_hotel_img1" name="img_url" multiple>
</div>
<div class="form-group col-md-12">
<label for="room">Upload Room Images</label>
<input type="file" class="form-control-file" id="upload_room_img2" name="img_url2" multiple>
</div>
<input type="button" class="btn btn-primary" name="submit" value="Submit" onclick="myfunction();">
</form>
</body>
<script>
function myfunction(){
var form_data = new FormData();
var image = document.getElementById('upload_hotel_img1').files.length;
var images = document.getElementById('upload_room_img2').files.length;
for (var x = 0; x < image; x++) {
form_data.append("image", document.getElementById('upload_hotel_img1').files[x]);
}
for (var x = 0; x < images; x++) {
form_data.append("images[]", document.getElementById('upload_room_img2').files[x]);
}
}
</script>
We can retrieve these images in the backend using the follwing : $_FILES["image"]['name'] and $_FILES["images"]['name'] respectively.
I am on Ubuntu. I am trying to take user file upload of small images. I checked the $_FILES it's filled with data. I tried to debug the move command but it doesnot echo anything.
if ($_SERVER['REQUEST_METHOD'] == 'POST' ){
//Now handle everything
if(isset($_POST["submit"])) {
print_r($_FILES);
//Store the image
if(!empty($_FILES['uploaded_file']))
{
$path = "/neel/public/img/";
$path = $path.basename($_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
echo 'Its working';
} else{
echo 'I am done!!!';
die();
}
}
createnewEvent($conn);
header('Location:/neel/index.php');
}
}
You can check if the file exists by checking its name.
if(!empty($_FILES['file']['name']))
Where file is the name of input field for file.
P. G above here is correct.
Instead of checking, if $_POST['submit']
You should check this:
if(isset($_FILES['uploaded_file']['name']))
Try this code it's a complete sign up form with PHP , Bootstrap
HTML
<div class="container">
<div class="row">
<br>
<h1 class="text-center">Create new account</h1>
<br>
<div class="col-lg-4 col-md-6 col-sm-12">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<div class="form-group">
<input type="text" class="form-control form-control-lg" name="name" placeholder="Your Name">
</div>
<div class="form-group">
<input type="text" class="form-control form-control-lg" name="username" placeholder="Username">
</div>
<div class="form-group">
<input type="password" class="form-control form-control-lg" name="password" placeholder="Password">
</div>
<div class="form-group text-center">
<div class='file-input'>
<input type='file' name="profile-img">
<span class='button label' data-js-label>Choose your profile image</span>
</div>
</div>
<div class="form-group">
<input type="submit" class="btn btn-success form-control form-control-lg" name="signup" value="Signup">
</div>
</form>
</div>
</div>
</div>
PHP
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$errors;
if (empty($_POST['name'])) {
$errors[] = "Name field is empty";
}
if (empty($_POST['username'])) {
$errors[] = "Username field is empty";
}
if (empty($_POST['password'])) {
$errors[] = "Password field is empty";
}
if (empty($_FILES['profile-img'])) {
$errors[] = "You should upload profile image";
} else{
$img = $_FILES['profile-img'];
$ext = end(explode('.', $img['name']));
$allowed_extensions = array('jpg', 'jpeg', 'png', 'gif');
$max_size = 4; //MegaBytes
if (! in_array($ext, $allowed_extensions)) {
$errors[] = "Please , Choose a valid image";
}
$img_size = $img['size'] / 1000000; // By Megabyte
if ($img_size > $max_size) {
$errors[] = "This picture is so large";
}
}
if (!empty($errors)) {
echo '<div class="container">';
foreach ($errors as $error) {
echo '
<div class="alert alert-danger" role="alert">
' . $error . '
</div>
';
}
echo "</div>";
} else {
$username = filter_var(htmlentities(trim($_POST['username'])), FILTER_SANITIZE_STRING);
$name = filter_var(htmlentities(trim($_POST['name'])), FILTER_SANITIZE_STRING);
$password = sha1(filter_var(htmlentities(trim($_POST['password'])), FILTER_SANITIZE_STRING));
// Profile Picture :-
$imgname = uniqid(uniqid()) . #date("Y-m-d") . "." . $ext;
$target_bath = "uploads/imgs/";
$target_bath = $target_bath . basename($imgname);
$orginal_img = $img['tmp_name'];
if (move_uploaded_file($orginal_img, $target_bath)) {
$sql = "INSERT INTO users(name, username, password,profile_picture, r_d) VALUES ('$name', '$username', '$password', '$target_bath', NOW())";
$result = mysqli_query($conn, $sql);
header('location: ./login.php');
}
}
}
The script you've shown shown will only "not echo anything" if $_SERVER['REQUEST_METHOD'] is not "POST". Assuming your description of events is accurate, then the problem is in the form #halojoy has asked that you show here.
I do hope that you are not redirecting the script back to itself. Also you shouldn't attempt to do a redirect after an echo.
I have created an add file upload function and multiple file upload function.
Add function is working fine, but Edit function is not working.
After submitting the EDIT form, the OLD VALUES are not saved in the database.
The file is saved in some other folder.
Kindly help.
this is my code:controller file:
if(isset($_POST['additionalimage_exists']) && $_POST['additionalimage_exists'] != ''){
$temp_image = $_POST['additionalimage_exists'];
//echo $temp_image;die;
if(isset($_FILES['additional_images']['name']) && $_FILES['additional_images']['name'] != ""){
$errors= array();
$additional_eventimages = $_FILES['additional_images']['name'];
foreach($_FILES['additional_images']['name'] as $key => $tmpName) {
$additionalimages = $_FILES['additional_images']['name'][$key];
$file_type = $_FILES['additional_images']['type'][$key];
$file_size = $_FILES['additional_images']['size'][$key];
$file_tmp = $_FILES['additional_images']['tmp_name'][$key];
move_uploaded_file($file_tmp,"images/events/".$additionalimages);
}
}
else
{
$additional_eventimages = $temp_image;
}
}
view File:
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="additional-image">Additional Images</label>
<input type="file" class="form-control" name="additional_images[]" value="<?php echo $additional_images;?>" multiple="multiple">
<input type="hidden" name="additionalimage_exists" value="<?php echo $additional_images;?>" class="form-control-file" id="exampleInputFile" placeholder="Enter Image Text" aria-describedby="fileHelp">
<div>
<?php echo $additional_images;?>
</div>
</div>
</div>
</div>
Try with this code.
if(isset($_FILES['additional_images']['name']) && $_FILES['additional_images']['name'] != ""){
$errors= array();
$additional_eventimages = $_FILES['additional_images']['name'];
foreach($_FILES['additional_images']['name'] as $key => $tmpName) {
$additionalimages = $_FILES['additional_images']['name'][$key];
$file_type = $_FILES['additional_images']['type'][$key];
$file_size = $_FILES['additional_images']['size'][$key];
$file_tmp = $_FILES['additional_images']['tmp_name'][$key];
move_uploaded_file($file_tmp,"images/events/".$additionalimages);
} else if(isset($_POST['additionalimage_exists']) && $_POST['additionalimage_exists'] != ''){
$additional_eventimages = $_POST['additionalimage_exists'];
} else {
$additional_eventimages = "";
}
I am uploading images in codeigniter. my file is not getting uploaded. i dont know where i am doing wrong. please find in below code
Html:
<form method="post" enctype="multipart/form-data" action="<?php echo base_url(); ?>assign_user/addStartPopup">
<div class="col-lg-6">
<div class="form-group">
<label for="image">
<input type="radio" id="image" name="start_img" onclick="rightfilefunction(1);" checked> <label for="image"> <span style="top: -2px;position: relative;">Image upload</span></label>
<label for="video" class="green">
<input type="radio" id="video" name="start_img" onclick="rightfilefunction(2);">
<span style="top: -2px;position: relative;">Video</span>
</label>
</div>
<div class="form-group" id="imageId">
<input type="file" name="imageupload" class="form-control">
</div>
<div class="form-group" id="videoId" style="display:none">
<input type="text" name="video_upload" class="form-control">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary"> Save </button>
</div>
</div>
</form>
Model:
public function startPopup(){
$createdOn = date('Y-m-d H:i:s');
if($_FILES['imageupload']['size'] != 0)
{
$files = $_FILES;
$config = array();
$config['upload_path'] = "startpage/";
$config['allowed_types'] = "png|jpg|gif";
$config['max_size'] = "5000";
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('imageupload')){
$imgdata = $this->upload->data();
$image_config=array();
$image_config["image_library"] = "gd2";
$image_config['overwrite'] = TRUE;
$image_config["source_image"] = $imgdata["full_path"];
$image_config['create_thumb'] = FALSE;
$image_config['maintain_ratio'] = TRUE;
$image_config['new_image'] = $imgdata["file_path"].$imgdata["file_name"];
$image_config['quality'] = "95%";
$image_config['width'] = 170;
$image_config['height'] = 170;
$this->load->library('image_lib',$image_config);
$this->image_lib->initialize($image_config);
$this->image_lib->resize();
$logo = 'startpage/'.$imgdata["file_name"];
$query = $this->db->query("INSERT INTO `startpop` (content_view, created_on) VALUES ('$logo', '$createdOn')");
if($query){
return 1;
}
}else
{
echo "not uploaded";
}
}else{
$prv = $this->input->post('video_upload');
$query = $this->db->query("INSERT INTO `startpop` (content_view, created_on) VALUES ('$prv', '$createdOn')");
if($query){
return 1;
}
}
}
Controller:
public function addStartPopup(){
$result = $this->model->startPopup();
if($result > 0){
$data['mgs'] = "Added Successfully ..!";
$this->load->view('admin/startPage',$data);
}
}
From this above code, i am getting the error "not uploaded". can any one tell me where i am doing wrong