Image Uploading using bootstrap - php

I have created an image uploader using the file upload option in Bootstrap. I want to upload images to a separate folder and keep the path name in the database. When I click the upload button the code does not show any error message but it doesn't works. Could you please help me to solve this problem? I have attached my code here:
image_upload_form_ui.php
<form id="fileupload" class="form-horizontal" method="POST" action="phpscripts/test.php" enctype="multipart/form-data">
<div class="control-group "> <!-- start of image -->
<label class="control-label">Avatar</label>
<div class="controls">
<div class="input-prepend">
<span class="add-on"><i class="icon-camera"></i></span>
<!-- file upload-->
<div class="fileupload fileupload-new" data-provides="fileupload">
<div class="fileupload-preview thumbnail" style="width: 200px; height: 150px;"><img src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&text=no+image"/></div>
<div>
<span class="btn btn-file"><span class="fileupload-new">Select image</span>
<span class="fileupload-exists">Change</span>
<input type="hidden" name="MAX_FILE_SIZE" value="204800" />
<input type="file" name="image" /></span>
Remove
</div>
</div>
<!--end of file upload -->
</div>
</div>
</div><!-- end of image -->
<div class="control-group"> <!-- start of buttons -->
<label class="control-label"></label>
<div class="controls">
<button type="submit" class="btn btn-primary" >Upload</button>
</div>
</div> <!-- end of buttons -->
</form>
here is my test.php
<?php
include "dbConnect.php";
//connect to the database
dbConnect();
$path = "uploads/";
$valid_formats = array("jpg","jpeg", "png", "gif", "bmp");
if(isset($_POST['image']))
{echo "1";
$name = $_FILES['image']['name'];
$size = $_FILES['image']['size'];
if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
if($size<(2048*2048))
{
$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
$tmp = $_FILES['image']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
mysql_query("UPDATE tutor SET avatar='$actual_image_name' WHERE userName='Isuru'");
echo "Hari";
}else
echo die(mysql_error());
}else
echo "exceed the file size";
}else
echo "Not a valid format";
}else
echo "no file is selected";
}
?>

Your code in test.php is checking the $_POST variable to see if a file was submitted via the form, but you probably want to be checking $_FILES. Check out the PHP documentation on file uploads for more information.
Try changing:
if(isset($_POST['image']))
to
if(isset($_FILES['image']))

Related

move_uploaded_file( ) not working on video/audio/pdf

I've done an upload page that should upload the files and set their name in the database. It works just perfect with the pictures , but the sound formats and the other ones doesn't seem to work.
This is how my html part look
<form method="post" enctype="multipart/form-data">
<div class="card card-login">
<?= FH::csrfInput() ?>
<div class="card-header text-center" data-background-color="rose" >
<h3 class="card-title">Upload</h3>
</div>
<div class="card-content">
<div class="input-group">
<span class="input-group-addon">
</span>
<div class="form-group label-floating">
<label class="control-label"><h4>Chose a name for the file</h4></label>
<br>
<input type="textd" name="name" id="name" class="form-control" value="">
</div>
<br><br>
<div class="form-group label-floating">
<label class="control-label"><h4>Choose a file</h4></label>
<br>
<input type="file" id="file" name="file" >
</div>
</div>
</div>
<div class="footer text-center">
<div class="file-upload">
<label for="submit" class="file-upload__label">
<div class="isa_error_class">
<?= FH::displayErrors($this->displayErrors)?>
</div>
<button class="btn btn-wd btn-lg" data-background-color="rose">Submit</button>
</label>
<input type="submit" name="submit" value="Submit" class="file-upload__input">
</div>
</div>
</form>
And there is the php part
if($this->request->isPost())
{
$this->request->csrfCheck();
$upload->assign($this->request->get());
$upload->user_id = Users::currentUser()->id;
$upload->name .= "." . pathinfo($_FILES['file']['name'] , PATHINFO_EXTENSION);
$value = pathinfo($_FILES['file']['name'] , PATHINFO_EXTENSION);
$upload->format = Upload::setFormat($value);
$dir = Users::currentUser()->id;
if(move_uploaded_file($_FILES["file"]["tmp_name"],'files' . DS . $dir . DS . $upload->name ))
{
if($upload->save())
{
Router::redirect('upload');
}
else
{
$upload->addErrorMessage('file','There were a problem saving in the database.');
}
}
else
{
$upload->addErrorMessage('file','There were a problem uploading it.');
}
}
The DS is the separator. The image formats seems to work perfect , but the other formats don't. Any ideas ?
You should check if u have allowed file_uploads = On in your php.ini and also check the maximum file size upload_max_filesize= 20M and to make sure that you are not passing it.

Image cannot upload on server using html5 form

I have html form which i used to upload image from mobile camera to server. code like below :
<form method="post" action="addpreview.php" name="formregister" enctype="multipart/form-data">
<div class="field half first">
<label for="name">Make Your Title</label>
<input type="text" name="title" id="title" />
</div>
<div class="field half">
<label for="email">Promoted By Text</label>
<input type="text" name="promoted" id="promoted" />
</div>
<ul class="actions">
<li>
<input type="file" name="photo" accept="image/*" capture="camera" style="" class="special fit" />
</li>
</ul>
<ul class="actions">
<center>
<li>
<input type="submit" name="Submit" value="Let's Preview" class="special" />
</li>
</center>
</ul>
Then below is code for upload processing in addpreview.php
if(isset($_POST['Submit'])) {
$title = $viral->escape_string($_POST['title']);
$promoted = $viral->escape_string($_POST['promoted']);
// upload image
if (($_FILES['photo']['name']!="")){
// Where the file is going to be stored
$target_dir = "upload/";
$file = $_FILES['photo']['name'];
$path = pathinfo($file);
$filename = $path['filename'];
$ext = $path['extension'];
$temp_name = $_FILES['photo']['tmp_name'];
$path_filename_ext = $target_dir.$filename.".".$ext;
// Check if file already exists
if (file_exists($path_filename_ext)) {
echo "Sorry, file already exists.";
}else{
move_uploaded_file($temp_name,$path_filename_ext);
echo "Congratulations! File Uploaded Successfully.";
}
}
}
It's work when i upload file using image from desktop. but upload failed when i tried upload using mobile camera.
Any missing code on mine ?

How to ulpload multiple images in one time using php form?

Here, my php script for upload image in server. i want user can upload multiple images on one records. right now, in this code at time only one image can post, user's requirement is they can post multiple images at time. I don't idea how can I possible using arrays. please help. Thanks in advance :)
<?php
include ("connect.php");
if(isset($_POST['submit']))
{
$event = $_POST['evnt_name'];
$image_name = $_FILES['evnt_img']['name'];
$image_type = $_FILES['evnt_img']['type'];
$image_size = $_FILES['evnt_img']['size'];
$image_tmp = $_FILES['evnt_img']['tmp_name'];
if($event=='' && $image_name==''){
echo "<script>alert('Any field is empty')</script>";
exit();
}
if($image_type=="image/jpeg" OR $image_type=="image/png" OR $image_type=="image/gif")
{
if($image_size<=50000)
{
move_uploaded_file($image_tmp,"imagess/$image_name");
}
else
{
echo "<script>alert('image is large, only 50kb size allowed')</script>";
exit();
}
}
else{
echo "<script>alert('image type is invalid')</script>";
exit();
}
$query = "insert into event_update (evnt_text,evnt_img) values ('$event','$image_name')";
if(mysqli_query($conn,$query))
{
echo "<script>alert('Post has been published')</script>";
exit();
}
}
?>
And below my simple bootstrap HTML code for form
<div class="col-lg-12">
<form method="POST" action="evntform.php" enctype="multipart/form-data">
<div class="form-group">
<label>Events Name</label>
<input type="text" name="evnt_name" placeholder="Write Events Name" class="form-control">
</div>
<div class="form-group">
<label>File input</label>
<input type="file" name="evnt_img[]">
</div>
<button name="submit" type="submit" class="btn btn-default">Submit Button</button>
</form>
</div>
Here is a quick example:
HTML:
<div class="col-lg-12">
<form method="POST" action="evntform.php" enctype="multipart/form-data">
<div class="form-group">
<label>Events Name</label>
<input type="text" name="evnt_name" placeholder="Write Events Name" class="form-control">
</div>
<div class="form-group">
<label>File input</label>
<input type="file" name="evnt_img[]" multiple>
</div>
<button name="submit" type="submit" class="btn btn-default">Submit Button</button>
</form>
PHP code to select the first image uploaded:
<?php
include ("connect.php");
if(isset($_POST['submit']))
{
$event = $_POST['evnt_name'];
$image_name = $_FILES['evnt_img'][0]['name'];
$image_type = $_FILES['evnt_img'][0]['type'];
$image_size = $_FILES['evnt_img'][0]['size'];
$image_tmp = $_FILES['evnt_img'][0]['tmp_name'];
....
You can use a for loop for every image:
for($i=0;$i<count($_FILES['evnt_img']);$i++){
$image_name = $_FILES['evnt_img'][$i]['name'];
}
J

Why isn't this PHP working? It says my $_FILES variable doesn't exist, when I think I have created it

<body>
<div class="wrapper">
<div class="header">
<h1>Text2<span class="fancy">FairyTale</span></h1>
<h4>Input: text, Output: <span class="fancy">fairy tale</span></h4>
</div>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
echo "<pre>";
print_r($_FILES);
echo "</pre>";
$filename = $_FILES["file-input"]["name"];
$extension = pathinfo($filename, PATHINFO_EXTENSION);
move_uploaded_file($_FILES["file-input"]["tmp_name"], $filename);
}
?>
<div class="upload-form">
<form action="" method="post" encytype="multipart/form-data">
<div class="upload">
<input type="file" name="file-input" class="file-input">
<input type="button" value="Browse">
<span class="filename">No file selected</span>
</div>
<input type="submit" name="submit" value="Upload">
<span class="valid-formats">Valid input: .txt files <= 512 KB</span>
</form>
</div>
</div>
</body>
you only write form Attribute Wrongly is Not encytype is enctype
enctype="multipart/form-data"

Ajax File Upload And insert details and Form File Details to Db

Please I Know that some of the Topics relating to my post is here but it does not actually seems to solve my problem. I have an uploader class and my Ajax upload is working fine without PHP connection. Now after Upload am trying to call in my file class to store the details to Database but not actually working out. I have searched all around both on youtube and none see to solve my problem
error_reporting(0);
if (isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") {
$path = "uploads/"; //set your folder path
//set the valid file extensions
$valid_formats = array("jpg", "png", "gif", "bmp", "jpeg", "GIF", "JPG", "PNG", "doc", "mp3", "txt", "docx", "pdf", "xls", "xlsx", "mp4"); //add the formats you want to upload
$name = $_FILES['myfile']['name']; //get the name of the file
$size = $_FILES['myfile']['size']; //get the size of the file
if (strlen($name)) { //check if the file is selected or cancelled after pressing the browse button.
list($txt, $ext) = explode(".", $name); //extract the name and extension of the file
if (in_array($ext, $valid_formats)) { //if the file is valid go on.
if ($size < 9433098888) { // check if the file size is more than 2 mb
$file_name = $_POST['filename'];
$new = $_POST['music_new']; //get the file name
$tmp = $_FILES['myfile']['tmp_name'];
if (move_uploaded_file($tmp, $path . $file_name.'.'.$ext)) { //
$sam->musicUpload('name','format','so on');
//please am in need of this. if i type echo $file_name works but can actually insert to my db.. i have no problem with my back end
}
else {
echo "failed";
}
} else {
echo "File size max 2 MB";
}
} else {
echo "Invalid file format..";
}
} else {
echo "Please select a file..!";
}
include'../../header.php';
echo $url;
}
<div class='category-tab shop-details-tab'><!--category-tab-->
<div class='col-sm-12'>
<ul class='nav nav-tabs'>
<li><a href='#details' data-toggle='tab'>Details</a></li>
<li><a href='#companyprofile' data-toggle='tab'>Company Profile</a></li>
<li><a href='#tag' data-toggle='tab'>Tag</a></li>
<li class='active'><a href='#reviews' data-toggle='tab'>Reviews (5)</a></li>
</ul>
</div>
<div class='tab-content'>
<div class='tab-pane fade' id='details' >
<div class='col-sm-3'>
<div class='product-image-wrapper'>
<div class='single-products'>
<div class='productinfo text-center'>
<img src='images/home/gallery1.jpg' alt='' />
<h2>$56</h2>
<p>Easy Polo Black Edition</p>
<button type='button' class='btn btn-default add-to-cart'><i class='fa fa-shopping-cart'></i>Add to cart</button>
</div>
</div>
</div>
</div>
</div>
<div class='tab-pane fade' id='companyprofile' >
<div class='col-sm-3'>
<div class='product-image-wrapper'>
<div class='single-products'>
<div class='productinfo text-center'>
<img src='images/home/gallery4.jpg' alt='' />
<h2>$56</h2>
<p>Easy Polo Black Edition</p>
<button type='button' class='btn btn-default add-to-cart'><i class='fa fa-shopping-cart'></i>Add to cart</button>
</div>
</div>
</div>
</div>
</div>
<div class='tab-pane fade' id='tag' >
<div class='col-sm-3'>
<div class='product-image-wrapper'>
<div class='single-products'>
<div class='productinfo text-center'>
<img src='images/home/gallery4.jpg' alt='' />
<h2>$56</h2>
<p>Easy Polo Black Edition</p>
<button type='button' class='btn btn-default add-to-cart'><i class='fa fa-shopping-cart'></i>Add to cart</button>
</div>
</div>
</div>
</div>
</div>
<div class='tab-pane fade active in' id='reviews' >
<div class='col-sm-12'>
<ul>
<li><a href=''><?php if($logged){ echo $username;}else{echo'Not Logged';}?></a></li>
<li><a href=''><i class='fa fa-clock-o'></i>12:41 PM</a></li>
<li><a href=''><i class='fa fa-calendar-o'></i>31 DEC 2014</a></li>
</ul>
<p> Plese make sure to upload Relevant files or else it will be deleted by the Head Admin(s). Thanks for Understanding</p>
<p><b>Write Your Review</b></p>
<div class="row">
<div class='tab-pane fade active in' id='reviews' >
<div class='col-sm-12'>
<h3>jQuery Ajax file upload with percentage progress bar</h3>
<form id="myform" method="post">
<div class="form-group">
<label>Enter the file name: </label>
<input class="form-control" type="text" id="filename" />
</div>
<span>
<input type="text" placeholder="Database Name" id='music_name'/>
<input type="text" placeholder="Producer"" id="music_producer" />
</span>
<br>
<span>
<input type="text" placeholder="Artist Name" id="music_artist"/>
<input type="text" placeholder="Album Name"/>
</span><br>
<textarea name="" placeholder="Music Description" id='music_desc'></textarea>
<div class="form-group">
<label>Select file: </label>
<input class="form-control" type="file" id="myfile" />
</div>
<div class="form-group">
<div class="progress">
<div class="progress-bar progress-bar-success myprogress" role="progressbar" style="width:0%">0%</div>
</div>
<div class="msg"></div>
</div>
<input type="button" id="btn" class="btn-success" value="Upload" />
</form>
<?php include'generalModal.php';
?>
</div>
</div>
</div>
</div>
</div>
</div>
</div><!--/category-tab-->
<?php
include_once'../../music/reCommend.php'; echo'
<script src="'.$url.'panel/uploadMusic/processUpload.js"></script>';
include '../../Blog/Footer.php';

Categories