I want to get the database picture path so that i can used it and store the same file-name path in the image folder so both can be matched up. Actually i am using a unique-id so that pictures have unique names but don't know how to use it rightly. Any help would be appreciated.
$insert = "UPDATE USER_LOGIN SET PICTURE = '".uniqid().$_FILES['file']['name']."' WHERE USERNAME = '".$_COOKIE['username']."'";
$result = oci_parse($con, $insert);
// Executes a statement.
$check = oci_execute($result);
$UploadDirectory = '/wamp/www/img/Users/Users/'.$row['PICTURE'];
Working Solution:
if($row)
{
$Filename = uniqid().$_FILES['file']['name'];
$DirectoryPath = '/wamp/www/img/Users/Users/'.$Filename;
if(move_uploaded_file($_FILES['file']['tmp_name'], $DirectoryPath))
{
$insert = "UPDATE USER_LOGIN SET PICTURE = '".$Filename."' WHERE USERNAME = '".$_COOKIE['username']."'";
$result = oci_parse($con, $insert);
// Executes a statement.
$check = oci_execute($result);
if($check)
{
echo "Saved";
// Commit the changes to the table.
oci_commit($con);
}
else
{
// Rollback changes to table.
oci_rollback($con);
}
}
else
{
//die('error uploading File!');
}
}
Related
I want to have a separate query that updates only the imagename and imagetext when the imagefile is left blank and update all the imagename,imagetext and the image itself when the user selects a new image here is my query:
if (isset($_POST['update'])) {
$update_id = $_POST["id"];
$update_imagename = $_POST['imagename'];
// Get image name
$update_imagefile = $_FILES['imagefile']['name'];
// Get text
$update_imagetext = mysqli_real_escape_string($db, $_POST['imagetext']);
// image file directory
$target = "images/portfolio/" . basename($update_imagefile);
$update_query = "UPDATE `images` SET `imagename`='$update_imagename',`imagetext`='$update_imagetext',`imagefile`= '$update_imagefile' WHERE `imageid`='$update_id'"; //delete query
$run = mysqli_query($db, $update_query);
if ($run) {
//javascript function to open in the same window
echo "<script>window.open('artworksupdate.php?=image has been updated','_self')</script>";
}
to determine if you have uploadFile, just do !empty($_POST['imagefile']), following you have sample.
if (!empty($_POST)) {
$update_id = $_POST["id"];
// Get image name
$update_imagename = $_POST['imagename'];
// Get image description
$update_imagetext = $_POST['imagetext'];
// Determine if we have input file set
$haveUploadedFile = !empty($_POST['imagefile']);
if( $haveUploadedFile ) {
// [...] do your upload stuff here.
$update_imagefile = 'myAwesomeImage.png';
$update_query = "UPDATE `images` SET `imagename`='$update_imagename',`imagetext`='$update_imagetext',`imagefile`= '$update_imagefile' WHERE `imageid`='$update_id'"; //delete query
}
else {
$update_query = "UPDATE `images` SET `imagename`='$update_imagename',`imagetext`='$update_imagetext' WHERE `imageid`='$update_id'"; //delete query
}
$run = mysqli_query($db, $update_query);
//[...]
}
I'm storing in DB only profile and profile heading images. [BLOB]
If i change my profile picture or my header picture its works fine, its storing in my DB.
The funny thing is that when i go back one page the last uploaded image appears again.
I recorded it here in this video
Watch it so you will understand whats going on.
I want to know why this happens?
this are my codes.
function profile_pic(){
//uploads imgs
global $conn;
if(isset($_FILES['proImage']['tmp_name'])){
$img = file_get_contents($_FILES['proImage']['tmp_name']);
$imgName = addslashes($_FILES['proImage']['name']);
$imgSize = getimagesize($_FILES['proImage']['tmp_name']);
if ($imgSize == false){
$_SESSION['empError'] = 'Error';
}else{
$stmt = $conn->prepare("UPDATE users SET ProfileImage=? , ProfileImageName=? WHERE email=?");
$stmt->bind_param("sss",$img, $imgn, $em);
$img=$img;
$imgn=$imgName;
$em = escape_string($_SESSION['useremail']);
$stmt->execute();
$lastId=$stmt->insert_id;
$_SESSION['empError']= "Updated.";
//return $lastId;
}
}
}
display imgs
function display_pic(){
global $conn;
$stmt = $conn->prepare("SELECT * FROM users WHERE email=?");
$stmt->bind_param("s", $em);
$em = $_SESSION['useremail'];
$stmt->execute();
$result = $stmt->get_result();
$rows = $result->fetch_assoc();
$url =$rows['ProfileImage']; //blob row
$b64 = base64_encode($url);
$uri = 'data:image/jpeg;base64,' . $b64;
echo '<img class="avatar border-white" src="' . $uri . '" />';
}
Hi i'm trying to receive my images from the database. I already can insert the images, but I don't know if it goes wrong overthere or that I do something wrong with getting the image.
The code for inserting the image:
public function Save(){
/*** check if a file was uploaded ***/
if(is_uploaded_file($_FILES['userfile']['tmp_name']) && getimagesize($_FILES['userfile']['tmp_name']) != false)
{
/*** get the image info. ***/
$size = getimagesize($_FILES['userfile']['tmp_name']);
/*** assign our variables ***/
$type = $size['mime'];
$imgfp = fopen($_FILES['userfile']['tmp_name'], 'rb');
$size = $size[3];
$name = $_FILES['userfile']['name'];
$maxsize = 99999999;
/*** check the file is less than the maximum file size ***/
if($_FILES['userfile']['size'] < $maxsize )
{
/*** connect to db ***/
$db = new Db();
/*** our sql query ***/
$sql = "INSERT INTO Foto (image_type ,image, image_size, image_name)
VALUES ('". $type ."',
'". $imgfp ."',
'". $size ."',
'". $name ."');";
$db->conn->query($sql);
$sql = "SELECT * FROM Foto ORDER BY id DESC LIMIT 1;";
$select = $db->conn->query($sql);
$numberofRows = $select->num_rows;
if($numberofRows == 1)
{
while ($oneSelect = $select->fetch_assoc())
{
return $oneSelect;
}
} else {
throw new Exception("Fout");
}
}
else
{
throw new Exception("Bestand is te groot");
}
}
else
{
throw new Exception("Dit extensie is niet ondersteund");
}
}
The code for loading the images:
<?php
include_once("classes/Db.class.php");
//$id = $_GET['id'];
$id = "33";
$db = new Db();
$sql = "SELECT image, image_type FROM Foto WHERE id = '". $id ."';";
$result = $db->conn->query($sql);
$row = $result->fetch_assoc();
if(sizeof($row) == 2)
{
header("Content-type: ".$row['image_type']);
echo $row['image'];
}
else
{
throw new Exception("Out of bounds Error");
}
?>
You're not actually inserting the image data. Read up on the fopen() function, it doesn't return the image data, but a file handle that can then be used for reading from or writing to a file (using, for example, fread() or fwrite()). Easy way out is using file_get_contents().
But take my advice, it's really bad practice to store images in the database. They're files and better off stored on the file system (hence the name). Makes serving them a lot faster too, as no PHP request has to be executed. Store the filename and maybe a relative path to the file in the database instead.
I have a file which can possibly be moved into either 2 places depending on which button the user clicked on a seperate page:
At the moment I have stated if the the file moves into 'ImageFiles' folder then move the folder and insert a database row.
But I want to also include that if the file hasn't been uploaded into 'ImageFiles' folder, then delete database row using this code below:
$imagecancelsql = "DELETE FROM Image
WHERE ImageFile = 'ImageFiles/".
mysql_real_escape_string($_FILES['fileImage']['name'])."'";
mysql_query($imagecancelsql);
Is this possible to do and if so how can it be written in the current if/else statement I have in the php script?
Below is php script:
<?php
session_start();
...//connect to DB
$result = 0;
if( is_file("ImageFiles/".$_FILES['fileImage']['name'])) {
$parts = explode(".",$_FILES['fileImage']['name']);
$ext = array_pop($parts);
$base = implode(".",$parts);
$n = 2;
while( is_file("ImageFiles/".$base."_".$n.".".$ext)) $n++;
$_FILES['fileImage']['name'] = $base."_".$n.".".$ext;
move_uploaded_file($_FILES["fileImage"]["tmp_name"],
"ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;
$imagesql = "INSERT INTO Image (ImageFile)
VALUES ('ImageFiles/".mysql_real_escape_string($_FILES['fileImage']['name'])."')";
mysql_query($imagesql);
}
else
{
move_uploaded_file($_FILES["fileImage"]["tmp_name"],
"ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;
$imagesql = "INSERT INTO Image (ImageFile)
VALUES ('ImageFiles/".mysql_real_escape_string($_FILES['fileImage']['name'])."')";
mysql_query($imagesql);
}
If I've understood correctly, the following code should work as intended. If the file has been found, the new file will replce it and inserts a new entry to your database.
If the file wasnt found, the database entry will be removed from you table.
// Stores the final result
// 0 = File wasnt uploaded and the row has been removed from table
// 1 = File was uploaded and the row has added to the table
$result = 0;
if (is_file("ImageFiles/" . $_FILES['fileImage']['name'])) {
move_uploaded_file($_FILES["fileImage"]["tmp_name"], "ImageFiles/" . $_FILES["fileImage"]["name"]);
$imagesql = "INSERT INTO Image (ImageFile) VALUES ('ImageFiles/".mysql_real_escape_string($_FILES['fileImage']['name'])."')";
mysql_query($imagesql);
// Mark result as 1 - we have added the file to teh folder and a entry to the table
$result = 1;
} else {
$imagecancelsql = "DELETE FROM Image WHERE ImageFile = 'ImageFiles/" . mysql_real_escape_string($_FILES['fileImage']['name'])."'";
mysql_query($imagecancelsql);
}
I am trying to update and insert a created thumbnail into a MYSQL BLOB.
I have tried almost anything but can not get it to insert the created picture to the Database.
function update($email)
{
if(file_exists("$email.jpg"))
{
$image = "$email.jpg";
$tbl = 'tpctmembers';
$query1 = "SELECT image FROM $tbl WHERE email = '$email'";
$result = queryMysql($query1);
$rows = mysql_num_rows($result);
if($rows > 0)
{
$query2 = "UPDATE $tbl SET image ='$image' WHERE email ='$email'";
queryMysql($query2);
}
else
{
$query3 = "INSERT into $tbl(image) VALUES('$image') where email = '$email'";
queryMysql($query3);
}
}
}
Thank You !
You need to read the image file, and insert that data, not the string naming the file. See this tutorial.