Android PHP - Upload photo to mySQL using UPDATE - php

I have an android app that will upload a photo to mysql database. This photo serves as a profile photo. Which means I must UPDATE the existing row of the user.
Table - user
Columns - id, name, photo, schoolid, password
Using the id of the user, he will upload the photo in his row.
I followed this tutorial.
https://www.simplifiedcoding.net/android-volley-tutorial-to-upload-image-to-server/
This is the script.
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$schoolid = $_POST['schoolid'];
$photo = $_POST['photo'];
require_once('dbConnect.php');
$sql ="SELECT schoolid FROM user WHERE schoolid = '$schoolid'";
$res = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($res)){
$schoolid = $row['schoolid'];
}
$path = "uploads/$schoolid.png";
$actualpath = "http://192.168.0.20/quizmaker/$path";
$sql = "UPDATE user SET photo = '$actualpath' WHERE schoolid = '$schoolid'";
if(mysqli_query($con,$sql)){
file_put_contents($path,base64_decode($photo));
echo "Successfully Uploaded";
}
mysqli_close($con);
}else{
echo "Error";
}
In my android.
String photo = getStringImage(bitmap);
Map<String,String> params = new Hashtable<>();
params.put("photo", photo);
params.put("schoolid", schoolid);

Related

size = 0 when try to upload image to server by php and android

i try to upload image to phpmyadmin server but in every time i get same error first error : Notice: Undefined index: image_path in /storage/ssd2/750/2564750/public_html/hi.php on line 12
Your Image Has Been Uploaded.
image is uploaded to server but its size =0 "zero"
php code
<?php
include 'DatabaseConfig.php';
// Create connection
$conn = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$DefaultId = 0;
$ImageData = $_POST['image_path'];
$ImageName = $_POST['image_name'];
$GetOldIdSQL ="SELECT id FROM UploadImageToServer ORDER BY id ASC";
$Query = mysqli_query($conn,$GetOldIdSQL);
while($row = mysqli_fetch_array($Query)){
$DefaultId = $row['id'];
}
$ImagePath = "images/$DefaultId.png";
$ServerURL = "https://xxxxx.000webhostapp.com/$ImagePath";
$InsertSQL = "insert into UploadImageToServer (image_path,image_name) values ('$ServerURL','$ImageName')";
if(mysqli_query($conn, $InsertSQL)){
file_put_contents($ImagePath,base64_decode($ImageData));
echo "Your Image Has Been Uploaded.";
}
mysqli_close($conn);
}else{
echo "Not Uploaded";
}
?>
my database (id, image_path,image_name) both path and name datatype is text
i use postman to test my test like the following
https://i.imgur.com/VrIXOsN.png

How to display an image from postgrsql database in virtual machine to my local

I'm trying to display an image from PostgreSQL database and I'm retrieving a black screen . in the field store_fname i'm having the field will give you the path to the image file inside my Odoo
filestore in the form 'ab/abcdef0123456789'
<?php
$conn = pg_connect("user = admin password = admin host = 192.168.1.11 dbname = odoo");
$res = pg_query($conn, "SELECT id, store_fname FROM ir_attachment
WHERE res_model = 'res.partner' AND res_field = 'image' AND res_id = 3; ");
$raw = pg_fetch_result($res, 'store_fname');
header('Content-type: image/jpeg');
echo pg_unescape_bytea($raw);
?>

Upload image to server from android volley via php

I am trying to upload an image to my server but can't seem to get it to work. I am using android and volley to get the image and then sending it to the php to upload.
I am using wamp as well.
Says "successfully uploaded" but is not in the directory?
PHP CODE:
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$image = $_POST['image'];
$username = $_POST['username'];
require_once('dbConnect.php');
$sql ="SELECT profilepictureID FROM tbl_userprofilepictures ORDER BY profilepictureID ASC";
$res = mysqli_query($con,$sql);
$profilepictureID = 0;
while($row = mysqli_fetch_array($res)){
$profilepictureID = $row['profilepictureID'];
}
$path = "000001/$profilepictureID.JPG";
$actualpath = "http://*wamp server ip*/Users/Images/$path";
$sql = "INSERT INTO tbl_userprofilepictures(username,profilepicturepath) VALUES ('$username','$actualpath')";
if(mysqli_query($con,$sql)){
file_put_contents($path,base64_decode($image));
echo "Successfully Uploaded";
}
mysqli_close($con);
} else{
echo "Error";
}
?>

Struggling to display blob image with php

I am building a simple website, I want to allow the users to upload and change their avatars. At present I have been able to upload images to a mysql database, stored as blobs with the code as follows:
//connected to DB, userID fetched
$image = $FILES['fileToUpload']['tmp_name'];
$fp = fopen($image, 'r');
$content = fread($fp, filesize($image));
$content = addslashes($content);
fclose($fp);
$sql = "UPDATE tbUsers SET profileImage = '".$content."' WHERE userID = ".userID;
$result = mysql_query($sql) or die (mysql_error());
When I download the files from phpmyadmin after upload they are saved as .bin files, but can be viewed normally. I'm not sure if this is correct or not.
My code to display the images is as follows:
HTML:
<?php echo '<img src ="showPic.php?q='.$_SESSION['profile'].'"/>'; ?>
PHP:
if (!empty($_GET['profile']) && is_numeric($_GET['profile']))
{
$con = mysql_connect("localhost", "root", "");
$mysql_select_db("projectDB");
$sql = "SELECT profileImage FROM tbUsers WHERE userID = ". $_GET['profile'];
$result = mysql_query($sql) or die (mysql_error());
header('Content-type: image/jpeg');
$row = mysql_fetch_object($result);
echo $row['image_data'];
}
I am unsure if I am attempting to display the image in the correct way, any help (corrections/alternative solutions) would be greatly appreciated :)
You can do this :
if (!empty($_GET['profile']) && is_numeric($_GET['profile']))
{
$con = mysql_connect("localhost", "root", "");
$mysql_select_db("projectDB");
$sql = "SELECT profileImage FROM tbUsers WHERE userID = ". $_GET['profile'];
$result = mysql_query($sql) or die (mysql_error());
$content = mysql_result($result,0,"file_content");
$name = mysql_result($result,0,"file_name");
$type = mysql_result($result,0,"file_type");
$size = mysql_result($result,0,"file_size");
header("Content-type: $type");
echo $content
}
Note : You should have these column in you table where you save your BLOB data
file_name = for save filename
$_FILES['file']['name']
file_type = for save file type
$_FILES['file']['type']
file_size = for save file size
$_FILES['file']['size']
You select this
$sql = "SELECT profileImage FROM tbUsers WHERE userID = ". $_GET['profile'];
and refer to not selected column
echo $row['image_data'];

PHP & MySQL delete image link problem

I'm trying to create a delete image link if the image is present and when the user clicks the delete image link it should delete the image. But for some reason this is not working can someone help me fix the delete image link problem? Thanks!
Here is the PHP code.
if (isset($_POST['delete_image'])) {
$img_dir = "../members/" . $user_id . "/images/thumbs/";
$img_thmb = "../members/" . $user_id . "/images/";
$image_name = $row['image'];
if(file_exists($img_dir . $image_name)){
if(unlink($img_dir.$image_name) && unlink($img_thmb.$image_name)){
$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli, "DELETE FROM users* WHERE image_id = '.$image_id.' AND user_id = '$user_id'");
}else{
echo '<p class="error">Sorry unable to delete image file!</p>';
}
}
}
if(isset($_POST['image']) || !empty($image)) {
echo 'Delete Image';
}
"DELETE FROM users* WHERE image_id = '.$image_id.' AND user_id = '$user_id'"
should be
"DELETE FROM users WHERE image_id = $image_id AND user_id = $user_id"
This is assuming $image_id and $user_id are both integers. If they're strings, put the single quotes around them.
Also, double check your link:
Delete Image
Is the user really passing in the link via POST?
Your code is vulnerable to SQL injection attacks. Please consider using parametrized queries.
"DELETE FROM users WHERE image_id = $image_id AND user_id = $user_id"
And also
$path = "Your Image folder Path";
$image_name = "Your Image Name";
unlink($path."$image_name);

Categories