I am uploading the description, the number and the image path through the PHP database.
When trying to call the image from the controller, an error is displayed which states: Undefined property: stdClass::$imgPath .... on line 9. On the other hand, the text elements are uploaded successfully.
Please find the PHP code below:
<?php
$prov = json_decode(file_get_contents("php://input"));
require_once("connection.php");
$connection = connectToMySQL();
// Check if image file is a actual image or fake image
$proverbDescription = $prov->proverbDescription;
$proverbNumber = $prov->proverbNumber;
$imgPath = $prov->imgPath;
$query = "INSERT INTO tbl_proverb (proverbDescription, proverbNumber, imgPath) VALUES ('$proverbDescription', '$proverbNumber', '$imgPath')";
echo($proverbDescription);
echo($proverbNumber);
echo($imgPath);
if (move_uploaded_file($_FILES['imgPath']['tmp_name'], $target_file)) //for uploading file
{
echo("File uploaded to: " . $target_dir);
}
$result = mysqli_query($connection, $query)
or die("Error in query: ". mysqli_error($connection));
if(mysqli_affected_rows($connection) > 0){
$success = true;
}else{
$success = false;
}
?>
The AngularJS Controller:
app.controller('proverbCtrl', function($scope, $http, $location) {
$scope.addProverb = function(prov) {
$http.post('php/addProverbs.php', prov).success(function(prov) {
console.log(prov);
// $location.path("/home");
});
};
});
And my HTML form:
<form id="demo" class="collapse" ng-submit="addProverb(prov)" enctype="multipart/form-data" method="POST">
<label>Image:</label>
<input type="file" ng-model="prov.imgPath" name="imgPath" id="imgPath" accept="image/*">
<label>Proverb Description:</label>
<input type="text" ng-model="prov.proverbDescription" ><br><br>
<label>Proverb Number:</label>
<input type="text" ng-model="prov.proverbNumber"><br><br>
<input type="submit" name="submit"><br>
See your post
</form>
If all you need is to get the file name to PHP try something like this:
HTML:
<input type="file" onchange="angular.element(this).scope().fileName(this)"
ng-model="prov.imgPath" name="imgPath" id="imgPath" accept="image/*">
Controller:
$scope.fileName= function(element) {
$scope.$apply(function(scope) {
scope.prov.imgPath = element.name;
});
};
Related
I'm new to database designing and I'm working on an upload formula.
In my example I have 3 input buttons. The upload of all selected file to my uploads folder works fine, but my problem is, only the first of all selected files gets inserted in my database.
Here is my code:
$db = mysqli_connect("localhost", "root", "", "xy");
if (isset($_POST['upload_docs'])) {
for($mi = 0; $mi < count($_FILES['sel_file']['tmp_name']); $mi++) {
$doc = $_FILES['sel_doc']['name'][$mi];
$tmp_doc = $_FILES['sel_doc']['tmp_name'][$mi];
$target = "doc_uploads/". basename($doc);
if (move_uploaded_file($$tmp_doc, $target)) {
echo "Upload successfully!";
}
else {
echo "Upload failed!";
}
}
}
$sql_f = "INSERT INTO docs (doc_file) VALUES ('$doc')";
mysqli_query($db, $sql_f);
<form method="POST" action="upload.php" enctype="multipart/form-data">
<div>
<input type="file" name="sel_doc[]" multiple="multiple">
<input type="file" name="sel_doc[]" multiple="multiple">
<input type="file" name="sel_doc[]" multiple="multiple">
</div>
<div id="sel_button">
<button type="submit" name="upload_docs">upload</button>
</div>
</form>
I'm a php/mysql newbie and need your help!
I have an upload formula to upload multiple files to a database, selected one by one.
The files upload works fine, but I want to add a title field for each selected file. I have no idea how to implement this in my code.
Here is my code:
$db = mysqli_connect("localhost", "root", "", "xy");
if (isset($_POST['upload_docs'])) {
for($mi = 0; $mi < count($_FILES['sel_doc']['tmp_name']); $mi++) {
$doc = $_FILES['sel_doc']['name'][$mi];
$tmp_doc = $_FILES['sel_doc']['tmp_name'][$mi];
$target = "doc_uploads/". basename($doc);
if (move_uploaded_file($tmp_doc, $target)) {
echo "Upload successfully!";
}
else {
echo "Upload failed!";
}
$sql_f = "INSERT INTO docs (doc_file) VALUES ('$doc')";
mysqli_query($db, $sql_f);
}
}
<form method="POST" action="upload.php" enctype="multipart/form-data">
<div>
<input type="file" name="sel_doc[]">
<input type="file" name="sel_doc[]">
<input type="file" name="sel_doc[]">
<input type="file" name="sel_doc[]">
<input type="file" name="sel_doc[]">
</div>
<div id="upload_button">
<button type="submit" name="upload_docs">upload</button>
</div>
</form>
This is how 1 upload would looks like:
| id_doc | doc_file | doc_title|upload_id|
++++++++++++++++++++++++++++++++++++++++++++++++
|.....1......|...a.pdf...|....vvv....|....1....|
|.....2......|...b.pdf...|....www....|....1....|
|.....3......|...c.pdf...|....xxx....|....1....|
|.....4......|...d.pdf...|....yyy....|....1....|
|.....5......|...e.pdf...|....zzz....|....1....|
In my orginal code I insert in 2 tables for 1:n realationship (because of the "upload_id").
HTML
<div>
<?php for ($i=0; $i<5; $i++){ ?>
<b><?php echo $i ?></b><br>
File: <input type="file" name="sel_doc[<?php echo $i ?>]"><br>
Title: <input name="title[<?php echo $i ?>]"><br>
<?php } ?>
</div>
Will print this:
<div>
<b>0</b><br>
File: <input type="file" name="sel_doc[0]"><br>
Title: <input name="title[0]"><br>
<b>1</b><br>
File: <input type="file" name="sel_doc[1]"><br>
Title: <input name="title[1]"><br>
<b>2</b><br>
File: <input type="file" name="sel_doc[2]"><br>
Title: <input name="title[2]"><br>
<b>3</b><br>
File: <input type="file" name="sel_doc[3]"><br>
Title: <input name="title[3]"><br>
<b>4</b><br>
File: <input type="file" name="sel_doc[4]"><br>
Title: <input name="title[4]"><br>
</div>
PHP code
$db = mysqli_connect("localhost", "root", "", "xy");
if (isset($_POST['upload_docs'])) {
for($mi = 0; $mi < count($_FILES['sel_doc']['tmp_name']); $mi++) {
$doc = $_FILES['sel_doc']['name'][$mi];
$tmp_doc = $_FILES['sel_doc']['tmp_name'][$mi];
$target = "doc_uploads/". basename($doc);
$title = (isset($_POST['title'][$mi])?$_POST['title'][$mi]:"");
if (move_uploaded_file($tmp_doc, $target)) {
echo "Upload successfully!";
}
else {
echo "Upload failed!";
}
$sql_f = "INSERT INTO docs (doc_file, title) VALUES ('$doc', '$title')";
mysqli_query($db, $sql_f);
}
}
Btw; This example code is not safe for sql injection.
I am getting the img_description but my img_name remain empty.
Basic php code
$msg = "";
if(isset($_POST['upload'])) {
$target = "images/".basename($_FILES['image']['name']); // get error here
$image = $_FILES['image']['name']; // get error here
$text = $_POST['text'];
$sql = "INSERT into images (img_name, img_description) values ('$image', '$text')";
$smt = $heidisql->prepare($sql);
$smt->execute();
if(move_uploaded_file($_FILES['image']['tmp_name'], $target)) { //error here
$msg = "Image upload sucessfully";
}else {
$msg = "Image failed to upload properly";
}
My basic Form
<form method="POST" action="checkout.php" enctype="multipart/form-date">
<input type="hidden" name="size" value="1000000">
<div>
<input type="file" name="image">
</div>
<div>
<textarea name="text" cols="40" rows="4" placeholder="img text decription" ></textarea>
</div>
<div>
<input type="submit" name="upload" value="upload img">
</div>
</form>
Table: images, attribute -> img_id, img_name, img_description
Error: Undefined index: image
You have a typo in the enctype in your form tag.
Change
multipart/form-date
to
multipart/form-data
You have a write mistake enctype="multipart/form-date" use enctype="multipart/form-data" instead.
i wanted to upload the mp3 file to the related folder name /uploads/ inside my project folder. But something not working correctly in php. Image file are uploading without any error but when i tried to upload the mp3 file its not working.
Here is my html form
<form action="act_songs.php" method="post" enctype="multipart/form-data">
<?php
if (isset($_SESSION['msg'])) {
echo $_SESSION['msg'];
unset($_SESSION['msg']);
}
?>
<p>
<label>Song Title</label>
<input type="text" name="sng-title">
</p>
<p>
<label>Song Name</label>
<input type="file" name="mp3" accept="audio/*" runat="server">
</p>
<p>
<input type="submit" name="add-song" value="ADD">
</p>
</form>
And here is my php code
if (isset($_POST['add-song'])) {
$title = $_POST['sng-title'];
$audio = $_FILES['mp3']['name'];
$audio_type = $_FILES['mp3']['type'];
$audio_size = $_FILES['mp3']['size'];
$tmp_location = $_FILES['mp3']['tmp_name'];
$audio_url = "../uploads/".$audio;
if ($type == '.mp3' || $type == '.wav') {
if ($size <= 5000) {
move_uploaded_file($tmp_location, $audio_url);
}
}
if (!empty($title)) {
$sql = "insert into `tbl_songs` (`title`,`songs`) values ('$title','$audio_url')";
$sql_run = mysql_query($sql);
if ($sql_run) {
$_SESSION['msg'] = "<div class='alert'>Record Saved</div>";
header('location:songs.php');
}
else{
$_SESSION['msg'] = "<div class='alert'>Record Cannot Saved</div>";
header('location:add-songs.php?invalid');
}
}
else{
$_SESSION['msg'] = "<div class='alert'>Title Must be requiired.</div>";
header('location:add-songs.php?invalid');
}
}
What is the problem i am unable to debug the problem. If anybody have solution then place your answer
I am uploading an image using the following php code but the file is not getting upload.
if(isset($_POST['submit'])){
$title = $_POST['title'];
$target_folder = "../newsimageuploads/";
$bannerimagelink = "http://example.com/newsimageuploads";
$bannerimage = addslashes(file_get_contents($_FILES['bannerimage']['tmp_name']));
$bannerimage_name = addslashes($_FILES['bannerimage']['name']);
$bannerimage_size = getimagesize($_FILES['bannerimage']['tmp_name']);
if ($bannerimage!=""){
$rand = rand(111111, 9999999);
$fname = $_FILES["bannerimage"]["name"];
$newname = "Image ".$rand.".png";
move_uploaded_file($_FILES["bannerimage"]["tmp_name"], $target_folder.$newname);
$bannerimage_location = $bannerimagelink."/".$newname;
}
$query =mysql_query("INSERT INTO mytable (title,image) VALUES ('$title','$bannerimage_location')")or die(mysql_error());
if (($query) === TRUE) {
echo "<p style='color:green;'>Added Successfully</p>";
} else {
echo "Some Error Occured :(";
}
}
And my HTML part is
<form action="#" method="post">
<input type="text" name="title">
<input type="file" name="bannerimage" accept="image/jpeg,image/png,image/gif">
<button type="submit" name="submit">Add</button>
</form>
My title is getting insert in the MySQL table but image does not.
You are missing enctype='multipart/form-data' in your form
<form action="#" method="post" enctype="multipart/form-data">
Look here for more details
Add
enctype="multipart/form-data"
to the form tag. Without this attribute you will only get the name of the file. But the file itself won't be uploaded.
<form action="#" method="post" enctype="multipart/form-data">
<input type="text" name="title">
<input type="file" name="bannerimage" accept="image/jpeg,image/png,image/gif">
<button type="submit" name="submit">Add</button>