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 ?
Related
Have been trying to upload an image and it is not working as expected. The error in the title is being displayed on the form. The image is being inserted via a file in the 'View' and checked in the 'Controller', however, when it comes to the final upload it fails, due to permissions I assume...
I have given both files in the view and controller the correct permissions for uploading files - in FileZilla.
The code is:
FORM - View
<form class="article" id="article-form" name="article" method="post" enctype="multipart/form-data">
<ol>
<li>
<label for="heading">Heading</label> <span id="headingMessage"></span>
<input name="heading" id="heading" class="form-control" type="text" required>
</li>
<li>
<label for="topic">Topic</label> <span id="topicMessage" required></span>
<input name="topic" id="topic" class="form-control" type="text" list="football">
<datalist id="football">
<option value="Scotland"></option>
<option value="England"></option>
<option value="Spain"></option>
</datalist>
</li>
<li>
<label for="summary">Summary</label> <span id="summaryMessage"></span>
<input name="summary" id="summary" class="form-control" type="text">
</li>
<li>
<label for="thumbnail">Thumbnail Link</label> <span id="thumbnailMessage"></span>
<!-- <input name="thumbnail" id="thumbnail" class="form-control" type="text" required> -->
<input type="file" name="file" id="file">
</li>
<li>
<label for="video">Video</label> <span id="videoMessage"></span>
<input name="video" id="video" class="form-control" type="text">
</li>
<li>
<label for="articleText">Text</label>
<textarea name="articleText" id="articleText" class="md-textarea form-control" required></textarea>
</li>
<!-- <li>
<div class="g-recaptcha" data-sitekey="6LcUAnQUAAAAAPeF1u6Hcnf0Y5TfS4-0xitZ7ZeZ"></div>
</li> -->
</ol>
<input class="btn btn-success" id="formButton" type="submit" name="submit"value="Submit" name="submit">
<input class="btn btn-danger" id="formButton" type="reset" value="Reset">
</form>
The controller -
//check image
$target_dir = "../view/pictures";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 0;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["file"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 2;
//$thumbnail = '<img class="img-responsive" id="articleImage" src="'.$_POST["file"].'">';
echo $target_file;
} else {
echo "File is not an image. <hr>";
$uploadOk = 0;
}
}
$thumbnail=$target_file;
include("../model/api-article.php") ;
if($uploadOk>1)
{
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
$articleTxt = insertArticle($headline, $topic, $summary, $text, $thumbnail,$video,$date,$userId);
} else {
echo "Sorry, there was an error uploading your file.";
}
}else{
echo "Article not inserted - only upload images (PNG, JPEG)";
}
}
Please excuse the indenting
It's may not enough to give permission to the upload file - you need to permit the whole folder where you are uploading your images.
Hello everyone,
I have a table consist of 10 columns which in 4 of them i want to insert the image paths.
can any one guide me how to insert the image paths into database along with other 6 more columns data in a single query and upload the images into server(inside a folder).
here my html code:
<div class="panel-body">
<div class="form-group">
<label style="align-content:center" for="inputdefault">Product full name</label>
<input class="form-control" id="inputdefault" name="name2" type="text">
</div>
<div class="panel-body">
<div class="form-group">
<label style="align-content:center" for="inputdefault">Product category</label>
<input class="form-control" id="inputdefault2" name="name1" type="text">
</div>
<div class="panel-body">
<div class="form-group">
<label style="align-content:center" for="inputdefault">Product qty</label>
<input class="form-control" id="inputdefault3" name="name3" type="text">
</div>
<div class="panel-body">
<div class="form-group">
<label style="align-content:center" for="inputdefault">Picture 1.</label>
<input type="file" id="file3" name="files[]" multiple="multiple" accept="image/*" />
</div>
<div class="form-group">
<label style="align-content:center" for="inputdefault">Picture 2.</label>
<input type="file" id="file3" name="files[]" multiple="multiple" accept="image/*" />
</div>
<div class="form-group">
<label style="align-content:center" for="inputdefault">Picture 3.</label>
<input type="file" id="file3" name="files[]" multiple="multiple" accept="image/*" />
</div>
<div class="form-group">
<label style="align-content:center" for="inputdefault">Picture 4.</label>
<input type="file" id="file3" name="files[]" multiple="multiple" accept="image/*" />
</div>
Here php code :
Note: I did not get image file names in any variable to added into query, everything is working fine here just need to work with images upload and store the path in to database
<?php
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$pname= stripslashes($_REQUEST['name1']);
$pcat= stripslashes($_REQUEST['name2']);
$pprice= stripslashes($_REQUEST['name3']);
$pqty= stripslashes($_REQUEST['qty']);
$pdesc=stripslashes($_REQUEST['description']);
//$spassword=stripslashes($_REQUEST['img']);
$sqlinsert ="INSERT INTO `noorizone`.`products` VALUES ('$pname', '$pcat', '$pprice')";
if($con-> query($sqlinsert)=== true)
{
echo "<center><b style='color:green;'> added successfully... </b>";
echo"</center>";
}
else{
echo "<b style='color:red;'> cant register". $con->error."</b>";
}
}
?>
THANKS IN ADVANCE !!!
This is example for me, you can learn my codes :
$fileImageStatus = ""; // Create your variable for files
if(isset($_FILES['files_status']['name'])){ // Checking value for all input
$fileImageName = array(); // Make an array
for($i = 0; $i < count($_FILES['files_status']['name']); $i++){ // Looping
$imageName = $_FILES['files_status']['name'][$i]; // Give a variable with index from array looped
$uploadPath = '././sistem/users/members/'.$idPenerima.'/unggahan/';
move_uploaded_file($imageTmp, $uploadPath.$imageName); // Path upload
$fileImageName[] = $imageName; // Setting a values from array
}
$fileImageStatus = implode(",", $fileImageName); // Make that values an string
}
// in here you can insert to database with value from $fileImageStatus
I think if you change the name of the file to say file1 instead of file[], you will be fine.
See an example from W3Schools.com
<!DOCTYPE html>
<html>
<body>
<!--
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
-->
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
Here is a Good tutorial on how to upload multiple images into PHP and Mysql
http://www.wdb24.com/how-to-upload-multiple-images-in-php-and-store-in-mysql/
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
I'm trying to upload a picture to my server and save the image name to my database. But if I fill in my form it just refreshes and does nothing. Does someone know why it doesn't save to the folder?
If my submit button is clicked I run this PHP code:
<?php
if(isset($_POST['btn_save_updates'])) {
$id = $_GET['id'];
$email = $_POST['email'];
$signature = $_POST['signature'];
//if they DID upload a file...
if($_FILES['avatar']['name'])
{
//if no errors...
if(!$_FILES['avatar']['error'])
{
//now is the time to modify the future file name and validate the file
$new_file_name = strtolower($_FILES['avatar']['tmp_name']); //rename file
if($_FILES['avatar']['size'] > (2048000)) //can't be larger than 1 MB
{
$valid_file = false;
$error = 'Oops! Your file\'s size is to large.';
}
//if the file has passed the test
if($valid_file)
{
//move it to where we want it to be
move_uploaded_file($_FILES['avatar']['tmp_name'], 'avatar/'.$new_file_name);
$filename = $_FILES['avatar']['name'];
// run update statement here ...
$error = 'Settings have been updated.';
}
}
//if there is an error...
else
{
//set that to be the returned message
$error = 'Oops! Error: '.$_FILES['avatar']['error'];
}
}
}
?>
HTML Form:
<form method="post" role="form" autocomplete="off">
<div class="form-group">
<label for="email">Email</label>
<input type="text" name="email" id="email" tabindex="1" class="form-control" value="<?php userProfile("email"); ?>" autocomplete="off">
</div>
<div class="form-group">
<label for="signature">Signature <i id="sign_tooltip" class="fa fa-exclamation-circle" aria-hidden="true" ata-toggle="tooltip" data-placement="right" title="You can add images to your signature!"></i></label>
<script>
$("#sign_tooltip").hover(function(){
$('#sign_tooltip').tooltip('show')
});
</script>
<textarea rows="4" name="signature" id="signature" tabindex="3" class="form-control" autocomplete="off"><?php userProfile("signature"); ?></textarea>
</div>
<br /><br />
<div class="form-group">
<label for="email">Avatar</label><br /><br />
<img src="avatars/<?php userInfo('avatar'); ?>" alt="Default Avatar" class="img-circle" width="140" height="140"><br /><br />
<input name="avatar" id="avatar" tabindex="4" class="input-group" type="file" accept="image/*" />
</div>
<div class="form-group">
<div class="row">
<div class="col-xs-7">
</div>
<div class="col-xs-5 pull-right">
<button type="submit" name="btn_save_updates" id="btn_save_updates" tabindex="5" class="form-control btn btn-success"><i class="fa fa-floppy-o" aria-hidden="true"></i> Save </button>
</div>
</div>
</div>
</form>
You forgot to add enctype="multipart/form-data" to your form.
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']))