I have a php file named abstract_submission and to process the form in that file I created submitted_abstract php file. but move_uploaded file function does not working. for files I created a folder named submissions. Does anyone have a solution to this?
I tried shifting insert query up and down in if statement but it doesn't work.
<?php
$con = mysqli_connect("localhost",MYSQL_USER,MYSQL_PASSWORD,"svavpo_phicon");
ini_set('display_errors', 1);
ini_set('display_startup-errors', 1);
error_reporting(E_ALL);
$edit_state = false;
if(isset($_POST['sybmitabs']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$category = $_POST['category'];
$subm_category = $_POST['subm_category'];
$main_category = $_POST['main_category'];
$title = $_POST['title'];
$author = $_POST['author'];
$body = $_POST['body'];
$reference = $_POST['reference'];
$file = $_FILES['file']['name'];
$target = "submissions/".basename($_FILES['file']['name']);
$sql = "INSERT INTO `sub_abstract`(`name`, `email`, `category`, `subm_category`, `main_category`, `title`, `author`, `body`, `reference`, `file`) VALUES ('$name','$email','$category','$subm_category','$main_category','$title','$author','$body','$reference','$file')";
$result=mysqli_query($con,$sql);
if(move_uploaded_file($_FILES['file']['tmp_name'], $target))
{
echo "errro in updating db";
}
else
{
echo ("<script LANGUAGE='JavaScript'>
window.alert('Abstract Submitted Succesfully !');
window.location.href='../abstract_submission.php';
</script>");
}
}
?>
Related
I create a html page which have two file that i want to store these 2 different file i.e., Image and Signature in two different folder i.e., profile and signature folder. but Signature file stored in profile folder and Image file cant stored any of them. Please help me out.
here is my code
<?php
require 'config.php';
if(isset($_POST['submit'])){
$imagename = $_FILES["Image"]["name"];
$imagetemp = $_FILES["Image"]["tmp_name"];
$imagefolder = "Upload/profile/".$imagename;
move_uploaded_file($imagetemp, $imagefolder);
echo "<img src='$imagefolder' height='100px' width='100px'";
$signname = $_FILES["Signature"]["name"];
$signtemp = $_FILES["Signature"]["tmp_name"];
$signfolder = "signature/".$signname;
move_uploaded_file($signtemp, $signfolder);
echo "<img src='$signfolder' height='100px' width='100px'";
$Name = $_POST['Name'];
$gender = $_POST['gender'];
$phone = $_POST['phone'];
$location = $_POST['location'];
$Qualification = $_POST['Qualification'];
$Speciality = $_POST['Speciality'];
$Experience = $_POST['Experience'];
$License_No = $_POST['License_No'];
$Email = $_POST['Email'];
$Password = $_POST['Password'];
//$Image = $_POST['Image'];
//$Signature = $_POST['Signature'];
$sql = ("INSERT INTO doctor (Doctor_Name, gender, Phone, Location, Qualification, Speciality, Experience, License_No, Email, Password) value('$Name', '$gender', '$phone', '$location', '$Qualification', '$Speciality', '$Experience', '$License_No', '$Email', '$Password')");
$insertquery= mysqli_query($con, $sql);
if($insertquery){
echo "data inserted";
}
else{
echo "ERROR: $sql <br> $con->error";
}
$con->close();
}
The $imagedoes not get assigned any thing. The alert displays an empty string. The update query is working perfectly.
<?php
if(isset($_POST['EditProfilebu']))
{
$id = $_GET['id'];
$username = $_POST['Name'];
$Email = $_POST['Email'];
$PostalCode = $_POST['PostalCode'];
$Address = $_POST['Address'];
$text = $_POST['text'];
$image = $_FILES['image']['name'];
$image_tmp = $_FILES['image']['tmp_name'];
move_uploaded_file($image_tmp,"images/$image");
echo "<script>alert('$image')</script>";
move_uploaded_file($image_tmp,"images/$image");
$query = "update users set UserName='$username',UserEmail='$Email',Picture='$image',PostalCode='$PostalCode',Address='$Address',Text='$text' where UserId='$id'";
$run = mysqli_query($mysqli,$query);
if($run)
{
echo "<script>alert('Updated')</script>";
}
}
?>
If user uploads wrong file then if statement resetting $name to blank. What is the problem in following code?
if(isset($_POST['submit'])){
$conn= new mysqli('localhost','root','','dilip');
if(!$conn){
die("Not connect".mysqli_error);
}
$stm = $conn->prepare("Insert into comment (Name, email, Message, Image) values (?, ?, ?, ?)");
$stm->bind_param('ssss',$name,$email,$message,$image);
$name = $_POST['name'];
$message = $_POST['readerInput'];
$email = $_POST['email'];
$image='upload/comment/default.jpg';
$time = date("s");
//if(isset($_FILES['image'])){
if(is_uploaded_file($_FILES['image']['tmp_name'])){
$tmp_image = $_FILES['image']['size'];
$finfo = new finfo(FILEINFO_MIME_TYPE);
$file = $finfo->file($_FILES['image']['tmp_name']); //This line checks MIME Type of uploaded image
if($file!=='image/jpeg' && $file !=='image/gif' || $tmp_image > 1024*1024*2){ //1024*1024*2 = 2MB
echo "Upload your Profile Image in jpg/gif format and lower than 2mb. Otherwise continue without Image.";
}
else{
$photo = move_uploaded_file($_FILES['image']['tmp_name'],'upload/comment/'.$name.$time.'.jpg');
$image = 'upload/comment/'.$name.$time.'.jpg';
echo '<script> alert("Your file is accepted.")</script>';
$stm->execute();
$message = 'Your Comment';
$name = 'Your Name';
$email = 'Your eMail';
}
}else{
$stm->execute();
$message = 'Your Comment';
$name = 'Your Name';
$email = 'Your eMail';
}
}
Please try with below snippet. You are preparing insert statement before variable initialization and image upload code. I have moved code below file upload code.
if(isset($_POST['submit'])){
$flag = 0;
$conn= new mysqli('localhost','root','','dilip');
if(!$conn){
die("Not connect".mysqli_error);
}
$name = $_POST['name'];
$message = $_POST['readerInput'];
$email = $_POST['email'];
$image='upload/comment/default.jpg';
$time = date("s");
//if(isset($_FILES['image'])){
if(is_uploaded_file($_FILES['image']['tmp_name'])){
$tmp_image = $_FILES['image']['size'];
$finfo = new finfo(FILEINFO_MIME_TYPE);
$file = $finfo->file($_FILES['image']['tmp_name']); //This line checks MIME Type of uploaded image
if($file!=='image/jpeg' && $file !=='image/gif' || $tmp_image > 1024*1024*2){ //1024*1024*2 = 2MB
echo "Upload your Profile Image in jpg/gif format and lower than 2mb. Otherwise continue without Image.";
$flag = 1;
}else{
$photo = move_uploaded_file($_FILES['image']['tmp_name'],'upload/comment/'.$name.$time.'.jpg');
$image = 'upload/comment/'.$name.$time.'.jpg';
echo '<script> alert("Your file is accepted.")</script>';
}
}
$stm = $conn->prepare("Insert into comment (Name, email, Message, Image) values (?, ?, ?, ?)");
$stm->bind_param($name,$email,$message,$image);
if($flag==0){
$stm->execute();
}
}
I am new in stackoverflow and in PHP too, so I want to describe my problem first. Sorry if I have bad grammar, I am not native though.
I make a form that let user post (to database) with or without an image, this the code
$link = mysqli_connect('localhost', 'root', '12345!##', 'pklapps') or die("Couldnt connect to the server");
//public info dari admin
if (isset($_POST["title"]) && isset($_POST["content"]) && isset($_POST["category"])) {
$title = $_POST['title'];
$desc = $_POST['content'];
$lat = $_POST['latitude'];
$long = $_POST['longitude'];
$category = $_POST['category'];
$image = $_POST['userfile'];
if (!isset($_FILES['userfile'])) {
$query = "INSERT INTO public_info (title, content, category) VALUES ('$title','$desc','$category')";
$result = mysqli_query($link, $query);
if ($result) {
header('Location: post.php?success');
} else {
header('Location: post.php?error');
}
} else {
$fileName = $_FILES['userfile']['name'];
$target = "uploads/";
$fileTarget = $target.$fileName;
$tempFileName = $_FILES["userfile"]["tmp_name"];
$result = move_uploaded_file($tempFileName,$fileTarget);
/*
* If file was successfully uploaded in the destination folder
*/
if ($result) {
header('Location: post.php?success');
$query = "INSERT INTO public_info (title, content, category, imagePath) VALUES ('$title','$desc','$category', '$fileTarget')";
$link->query($query) or die("Error : ".mysqli_error($link));
} else {
header('Location: post.php?errimg');
}
mysqli_close($link);
}
}
and the form goes normal when i attach the image or file but it goes to header('Location: post.php?errimg'); when i not attach the image. Please help me with this, thank you.
Replace your if condition from !isset($_FILES['userfile']) to empty($_FILES['userfile']['name'])
//if (!isset($_POST[]))
$title = $_POST['title'];
$desc = $_POST['content'];
$lat = $_POST['latitude'];
$long = $_POST['longitude'];
$category = $_POST['category'];
$image = $_POST['userfile'];
if(empty($_FILES['userfile']['name'])) {
$query = "INSERT INTO public_info (title, content, category) VALUES ('$title','$desc','$category')";
$result = mysqli_query($link, $query);
if ($result) {
header('Location: post.php?success');
}
else {
header('Location: post.php?error');
}
}
else {
$fileName = $_FILES['userfile']['name'];
$target = "uploads/";
$fileTarget = $target.$fileName;
$tempFileName = $_FILES["userfile"]["tmp_name"];
$result = move_uploaded_file($tempFileName,$fileTarget);
/*
* If file was successfully uploaded in the destination folder
*/
if($result) {
header('Location: post.php?success');
$query = "INSERT INTO public_info (title, content, category, imagePath) VALUES ('$title','$desc','$category', '$fileTarget')";
$link->query($query) or die("Error : ".mysqli_error($link));
}
else {
header('Location: post.php?errimg');
}
mysqli_close($link);
}
// $query = "INSERT INTO public_info (title, content, category) VALUES ('$title','$desc','$category')";
} ?>
I've looked around and haven't found particularly what I'm after.
I have quite a few forms with text input and file uploads.
I've figured how to upload a file, give it a unique ID and get it into my web server folder. Pretty smooth sailing. However, I would like to also get that fancy new ID into my MySQL database.
I've separated my upload.php page with text forms going to the database
<?php
//Connecting and Sending data to the database follows
$dbc = mysqli_connect('localhost', 'root', 'root', 'surfboardhub')
or die('Error connecting to MySQL server');
//Get values from
$location = "";
$price = "";
$thick = "";
$width = "";
$height ="";
$model = "";
$brand = "";
$email = "";
$category = "";
if(isset($_POST['location'])){ $location = $_POST['location']; }
if(isset($_POST['price'])){ $price = $_POST['price']; }
if(isset($_POST['thick'])){ $thick = $_POST['thick']; }
if(isset($_POST['width'])){ $width = $_POST['width']; }
if(isset($_POST['height'])){ $height = $_POST['height']; }
if(isset($_POST['model'])){ $model = $_POST['model']; }
if(isset($_POST['brand'])){ $brand = $_POST['brand']; }
if(isset($_POST['email'])){ $email = $_POST['email']; }
//if(isset($_POST['image'])){ $imagename = $_POST['imagename']; }
//if(isset($_POST['mime'])){ $mime = $_POST['mime']; }
$query = "INSERT INTO uploads (location, price, thick, width, height, model, brand, email,category)
VALUES ('$location', '$price','$thick','$width','$height', '$model', '$brand', '$email','$category')";
$result = mysqli_query($dbc,$query)
or die('Error querying database.');
mysqli_close($dbc);
and then my bit to get the file to its new location in my web server.
$name = $_FILES['image']['name'];
$extension = strtolower(substr($name, strpos($name, '.') + 1));
$type = $_FILES['image']['type'];
$tmp_name = $_FILES['image']['tmp_name'];
if (isset($name)) {
if (!empty($name)) {
if (($extension=='jpg'||$extension=='jpeg'||$extension=='png'||$extension=="gif")&&$type=='image/jpeg'||$type=='image/png'||$type=='image/gif') {
$location = 'uploads/';
$location = $location . uniqid();
if (move_uploaded_file($tmp_name, $location.$name)) {
echo 'uploaded!';
}
else {
echo 'There was an error.';
}
} else {
echo 'File must be jpg/jpeg, png, or gif.';
}
} else {
echo 'Please choose a file';
}
}
?>
Basically, I need to get that new unique ID to go to where the text information is going, because they're all being submitted at once. And I'd like to be able to figure out who uploaded what if need be. If it didn't have a unique ID I can get it to work, but for some reason having that uniqid trips me up. Thoughts? Much obliged.
Save the uniqid() to a PHP variable and then you can use it in more than one place:
First, create an ID:
<?php
$ID = uniqid();
?>
Then, save your file, using your new $ID variable:
<?php
$name = $_FILES['image']['name'];
$extension = strtolower(substr($name, strpos($name, '.') + 1));
$type = $_FILES['image']['type'];
$tmp_name = $_FILES['image']['tmp_name'];
if (isset($name)) {
if (!empty($name)) {
if (($extension=='jpg'||$extension=='jpeg'||$extension=='png'||$extension=="gif")&&$type=='image/jpeg'||$type=='image/png'||$type=='image/gif') {
$location = 'uploads/';
$location = $location . $ID;
if (move_uploaded_file($tmp_name, $location.$name)) {
echo 'uploaded!';
} else {
echo 'There was an error.';
}
} else {
echo 'File must be jpg/jpeg, png, or gif.';
}
} else {
echo 'Please choose a file';
}
}
?>
Then, save your data to the db, including $ID
<?php
//Connecting and Sending data to the database follows
$dbc = mysqli_connect('localhost', 'root', 'root', 'surfboardhub')
or die('Error connecting to MySQL server');
//Get values from
$location = "";
$price = "";
$thick = "";
$width = "";
$height ="";
$model = "";
$brand = "";
$email = "";
$category = "";
if(isset($_POST['location'])){ $location = $_POST['location']; }
if(isset($_POST['price'])){ $price = $_POST['price']; }
if(isset($_POST['thick'])){ $thick = $_POST['thick']; }
if(isset($_POST['width'])){ $width = $_POST['width']; }
if(isset($_POST['height'])){ $height = $_POST['height']; }
if(isset($_POST['model'])){ $model = $_POST['model']; }
if(isset($_POST['brand'])){ $brand = $_POST['brand']; }
if(isset($_POST['email'])){ $email = $_POST['email']; }
//if(isset($_POST['image'])){ $imagename = $_POST['imagename']; }
//if(isset($_POST['mime'])){ $mime = $_POST['mime']; }
$query = "INSERT INTO uploads (ID, location, price, thick, width, height, model, brand, email,category)
VALUES ('$ID', '$location', '$price','$thick','$width','$height', '$model', '$brand', '$email','$category')";
$result = mysqli_query($dbc,$query)
or die('Error querying database.');
mysqli_close($dbc);
?>