select value not getting passed - php

can some one please tell me why its not getting passsed. all the other value gets passed except for the select option value. the files gets updates perfectly as i want. but the album ID doesnt get assigned as i want :( .
HTML Code from the page:
<form action="" method="post" enctype="multipart/form-data" >
<label>Select Image:</label>
<input type="file" aria-required="true" size="30" id="file" name="files[]" multiple="multiple" />
<?php if(isset($_GET['al'])){ ?>
<label>Album(required):</label>
<select name='album' id='album' aria-required="true">
<?php
$query_album = "SELECT * FROM `albums` ORDER BY `id` DESC ";
$result_m = $conn->query($query_album);
if ($result_m->num_rows > 0) {
// output data of each row
while($item_a = $result_m->fetch_assoc()) {
$value= $item_a['id'];?>
<option value='<?php echo $value; ?>'>
<?php echo $item_a['title']; ?>
</option>
<?php
}
} ?>
</select>
<?php } ?>
<label>Image Title :</label>
<input type="text" aria-required="true" size="30" value="" name='title' id='title'>
<label> Image Description :</label>
<input type="text" aria-required="true" size="30" value="" name='desc' id='desc'>
<button type="submit" value="Upload!" class="soc-icon soc-icon-small soc-icon-alt fa fa-floppy-o">
</button>
<button type="reset" name="reset" class="soc-icon soc-icon-small soc-icon-alt fa fa-refresh">
</button>
<input type="hidden" name="album" id="album" value="<?php echo $album; ?>">
</form>
PHP Code from the UPLOAD PAGE where the values are passed to... all other values gets passed except for the album id. can some one please help me to figure out what i'm doin wrong.
<?php
include 'DBConfig.php';
require 'imageconfig.php';
require 'img_functions.php';
if(isset($_FILES['files'])){
if(!empty($_POST['album'])){
$album_id = $_POST['album'];
$image_title=$_POST['title'];
$image_desc=$_POST['desc'];
$errors= array();
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
$file_name = $key.$_FILES['files']['name'][$key];
$file_size =$_FILES['files']['size'][$key];
$file_tmp =$_FILES['files']['tmp_name'][$key];
$file_type=$_FILES['files']['type'][$key];
if($file_size > 2097152){
$errors[]='File size must be less than 2 MB';
}
$query="INSERT into images(`ID`,`ALBUM_ID`,`CAPTION`,`FILE_NAME`,`FILE_SIZE`,`FILE_TYPE`,`DESC`) VALUES(DEFAULT,'{$album_id}','$image_title','$file_name','$file_size','$file_type','$image_desc'); ";
$desired_dir="uploads/fullsized/";
if(empty($errors)==true){
if(is_dir($desired_dir)==false){
mkdir("$desired_dir", 0700); // Create directory if it does not exist
}
if(is_dir("$desired_dir/".$file_name)==false){
move_uploaded_file($file_tmp,"uploads/fullsized/".$file_name);
create_square_image("uploads/fullsized/".$file_name,"uploads/thumbs/".$file_name,200);
}
else{ //rename the file if another one exist
$new_dir="uploads/fullsized/".$file_name.time();
rename($file_tmp,$new_dir) ;
}
$result = mysqli_query($conn, $query);
}else{
print_r($errors);
}
}
if(empty($error)){
header('Location:gallery.php');
}
}
else {
echo"album id not set";
}
} ?>

You are overwriting your input with another one with the same name:
<select name='album' id='album' aria-required="true">
...
<input type="hidden" name="album" id="album" value="<?php echo $album; ?>">

Related

How to remove image from folder at update phpMysql?

Update query from the database:
<?php
include 'dpconnection.php';
$uid=$_REQUEST['upid'];
if (isset($_POST['update'])) {
$category= $_POST['category'];
$pname= $_POST['pname'];
$parea= $_POST['parea'];
$pPrices= $_POST['pPrices'];
$pAddress= $_POST['pAddress'];
$filename = $_FILES['new_image']['name'] ;
$tempname = $_FILES['new_image']['tmp_name'] ;
$filesize = $_FILES['new_image']['size'] ;
$fileextension = explode('.', $filename) ;
$fileextension = strtolower(end($fileextension));
$newfilename = uniqid().'images'.'.'.$fileextension ;
$path = "media/".$newfilename ;
$sql = mysqli_query($conn,"UPDATE product SET c_id='$category', p_name='$pname', p_area='$parea', p_prices='$pPrices',p_address='$pAddress', p_thumb_image='$path' WHERE p_id='$uid'");
if (move_uploaded_file($tempname, $path) && $sql) {
echo "<script>alert('Record Updated successfully');</script>";
echo "<script>location.href = 'adminViewProduct.php';</script>";
} else {
echo "Error updating record: " . $conn->error;
}
}
?>
Image not deleting from folder, i have two if statement but just one else statement so it looks like i am missing something but i really don't know what i am missing.
This is form:
<div class="form-group">
<label>Address</label>
<input class="form-control" type="text" name="pAddress" value="<?php echo $row1['p_address'];?>">
</div>
<div class="form-group">
<label>Thumb Image</label>
<input type="file" name="new_image" class="form-control">
<input class="form-control" type="hidden" name="pimage" value="<?php echo $row1['p_thumb_image'];?>">
<img class="float-left" src="<?php echo $row1['p_thumb_image'];?>" width="100px;"><br><br>
</div>
<div class="form-group text-center">
<input class="btn btn-primary" type="submit" name="update" value="Update">
</div>
</form>
You don't have a code that delete the files in php there's a function called unlink where you can delete a files but first you need fetch the path or filename and delete it before updating your data.
Here is the link of unlink.
https://www.php.net/manual/en/function.unlink.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.

How to add multiple images using browse button

I have a following code where I can upload a single image. This image gets stored in both database and folder. Now what I want is to add multiple images. How can I do that. Help me to come out of this.
<?php
$uploadDir ="C:/wamp/www/dragongym/customers/";
if(isset($_POST['submit']))
{
$intime =DATE("H:i", STRTOTIME($_POST['intime']));
$outtime =DATE("H:i", STRTOTIME($_POST['outtime']));
date_default_timezone_set('Asia/Calcutta');
$today = date("Y-m-d");
$msg="";
$res = "SELECT customer_id FROM customer ORDER by customer_id DESC LIMIT 1";
$qur = mysql_query($res);
while($row = mysql_fetch_array($qur, MYSQL_BOTH))
{
$last_id = $row['customer_id'];
$plus_id = 1;
}
if( $last_id !="")
{
$cust_id = $last_id + $plus_id;
}
$filePath="";
if($_FILES['cimage']['size'] > 0)
{
// echo $cust_id;
// Temporary file name stored on the server for pdf
$filename = basename($_FILES['cimage']['name']);
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$new = $cust_id.'.'.$extension;
$tmpName1 = $_FILES['cimage']['tmp_name'];
$fileSize = $_FILES['cimage']['size'];
$fileType = $_FILES['cimage']['type'];
$filePath = $uploadDir . $new;
$resultes = move_uploaded_file($tmpName1, $filePath);
if (!$resultes)
{
echo "Error uploading file";
exit;
}
if(!get_magic_quotes_gpc())
{
$new = addslashes($new);
$filePath = addslashes($filePath);
}
}
$sql = 'INSERT INTO customer(customer_name,roll_no,customer_number,customer_address,tariff_id,intime,outtime,customer_image,active,joining_date) VALUES("'.$_POST['name'].'","'.$_POST['roll'].'","'.$_POST['number'].'","'.$_POST['address'].'","'.$_POST['tariff'].'","'.$intime.'","'.$outtime.'","'.$filePath.'","1","'.$today.'")';
$msg="<p style=\"color:#99CC00; font-size:13px;\"> Successfully!</p>";
if (!mysql_query($sql, $link))
{
die('Error: ' . mysql_error());
}
}
?>
<form action="#" method="post" enctype="multipart/form-data">
<h2>Registration Form</h2><?php echo $msg; ?>
<label>Name</label>
<input type="text" value="" name="name" id="name" required class="txtfield">
<label>Roll Number</label>
<input type="text" value="" name="roll" id="roll" required class="txtfield">
<label>Mobile Number</label>
<input type="text" value="" name="number" required class="txtfield" id="mobnum">
<label>Address</label>
<textarea name="address" class="txtfield"></textarea>
<label>Upload Photo</label>
<input type="file" value="" name="cimage" class="txtfield">
<!-- <label style="display: block">Timing</label>
<input type="text" value="" name="intime" placeholder="Intime" required class="timefield timepicker">
<input type="text" value="" name="outtime" placeholder="Outtime" required class="timefield timepicker">-->
<input type="submit" value="Save" name="submit" class="btn buttonside1">
</form>
You can use jquery plugin for that..
there is lots of plugin available on google..try this one http://blueimp.github.io/jQuery-File-Upload/
to do multiple file upload you should first have multiple="true" in your tab like so
<input type="file" name='files[]' multiple='true'/>
then use foreach loop to upload files.

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

How to save image in folder using CodeIgniter?

I am new to CodeIgniter. I want to know how to save an image in a folder. But I wrote the code like image name was stored in a table. But I want to store the image in a folder and retrieve image from the folder. Here I am using the code to store image name in table:
In Controller:
public function product()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('productname','Product Code','trim|required');
$this->form_validation->set_rules('productcode','Product Code','trim|required');
$this->form_validation->set_rules('productprice','Product Price','trim|required');
$this->form_validation->set_rules('quantity','Quantity','trim|required');
$this->form_validation->set_rules('uploadimage','Upload Image','trim_rquired');
if($this->form_validation->run()==FALSE)
{
$this->index();
}else
{
$data['query']=$this->main_model->product_db();
$this->load->view('query_view',$data);
}
}
In Model:
public function product_db()
{
$data=array(
'productname'=>$this->input->post('productname'),
'productcode'=>$this->input->post('productcode'),
'productprice'=>$this->input->post('productprice'),
'quantity'=>$this->input->post('quantity'),
'image'=>$this->input->post('uploadimage')
);
$query=$this->db->get("product");
if($query->num_rows())
{
$this->db->insert('product',$data);
$query=$this->db->get("product");
$this->session->set_userdata($data);
return $query->result();
}
return false;
}
In View:(form page)
<?php echo validation_errors('<p class="error">'); ?>
<?php echo form_open("main/product"); ?>
<p>
<label for="product_name">Product Name:</label>
<input type="text" id="productname" name="productname" value="<?php echo set_value('product_name'); ?>" />
</p>
<p>
<label for="ProductCode">Product Code</label>
<input type="text" id="productcode" name="productcode" value="<?php echo set_value('productcode'); ?>" />
</p>
<p>
<label for="productprice">Product Price:</label>
<input type="text" id="productprice" name="productprice" value="<?php echo set_value('productprice'); ?>" />
</p>
<p>
<label for="Quantity">Quantity:</label>
<select name="quantity" id="quantity" value="<?php echo set_value('quantity'); ?>" /><option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</p>
<p>
<label for="Uploadimage">Upload Image:</label>
<input type="file" name="uploadimage" id="uploadimage" value="<?php echo set_value('quantity'); ?>" />
</p>
<p>
<input type="submit" class="greenButton" value="submit" />
</p>
<?php echo form_close();
?>
In View(query_view Page):
<?php
echo "<table border='2'>
<tr>
<th>productid</th><th>productname</th><th>productcode</th><th>productprice</th>
<th>quantity</th><th>image</th>
</tr>";
foreach($query as $r)
{
echo "<tr>";
echo "<td>".$r->productid."</td>"."<td>".$r->productname."</td>".
<td>".$r>productcode."</td>"."<td>".$r->productprice."</td>"."<td>"
.$r->quantity."</td>"." <td>".$r->image."</td>";
echo "</tr>";
}
echo "</table>";
echo "<br>";
?>
This is just a sample code of uploading an image:
<?php
$configUpload['upload_path'] = './uploads/'; #the folder placed in the root of project
$configUpload['allowed_types'] = 'gif|jpg|png|bmp|jpeg'; #allowed types description
$configUpload['max_size'] = '0'; #max size
$configUpload['max_width'] = '0'; #max width
$configUpload['max_height'] = '0'; #max height
$configUpload['encrypt_name'] = true; #encrypt name of the uploaded file
$this->load->library('upload', $configUpload); #init the upload class
if(!$this->upload->do_upload('uploadimage')){
$uploadedDetails = $this->upload->display_errors();
}else{
$uploadedDetails = $this->upload->data();
}
print_r($uploadedDetails);die;
?>
you are seeking this kind of code
$config['upload_path'] = './files'; //core folder (if you like upload to application folder use APPPATH)
$config['allowed_types'] = 'gif|jpg|png'; //allowed MIME types
$config['encrypt_name'] = TRUE; //creates uniuque filename this is mainly for security reason
$this->load->library('upload', $config);
if (!$this->upload->do_upload('picture_upload')) { //picture_upload is upload field name set in HTML eg. name="upload_field"
$error = array('error' => $this->upload->display_errors());
}else{
//print_r($this->upload->data()); // this is array of uploaded file consist of filename, filepath etc. print it out
$this->upload->data()['file_name']; // this is how you get for example "file name" of file
}
Please follow this guide http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html it has everything in it. If you need help with logic its pretty simple
form -> upload field -> button -> form sent -> check rules if file is OK -> upload file -> save data (filename, filepath...) in table.

Categories