downloading Blob file in cakephp - mysql

i am using this query to get image saved in medium blob in mysql which calls download function in brands controller
SELECT *,CONCAT(\"<img src='../../brands/download/?file_id=\",id,
\"&name=\",logo_name,\"'/>\") AS file
FROM c_brands WHERE merchant_id=" .$merchant_session;
DOWN LOAD FUNCTION
function download()
{
//$this->view = 'Media';
Configure::write('debug', 1);
$id = $_GET["file_id"];
$file = $this->Brand->findById($id);
header('Content-type: ' . $file['Brand']['logo_type']);
header('Content-length: ' . $file['Brand']['logo_size']);
header('Content-Disposition: inline; filename='.$file['Brand']['logo_name']);
echo $file['Brand']['logo'];
exit();
}
But somehow it is just displaying the placeholder for image.

Can you explain more? I mean, what's the relation between the first query (the one with Concat) and the download() action, plus how are you using this query? Also, I notice that you are putting <img src='../../brands/download/?file_id=\", and that relative path is wrong. It must be src="/brands/download".
Still, your question is not clear.

Related

Load one of multiple items from API result that has a "list / set"

I have a API result that contains multiple photo's in a set / list / childs (I do not know the right term), the result looks like this.
"photos":[{"small":"https:\/\/pararius-office-prod.global.ssl.fastly.net\/10379\/files\/photos\/api-little\/52016521.1667319824-357.jpeg?fit=crop&width=100&height=100",
"big":"https:\/\/pararius-office-prod.global.ssl.fastly.net\/10379\/files\/photos\/api-export\/52016521.1667319824-357.jpeg?width=600",
"huge":"https:\/\/pararius-office-prod.global.ssl.fastly.net\/10379\/files\/photos\/api-huge\/52016521.1667319824-357.jpeg?width=1000&fit=bounds",
"middle":"https:\/\/pararius-office-prod.global.ssl.fastly.net\/10379\/files\/photos\/api-middle\/52016521.1667319824-357.jpeg?width=1000&fit=bounds",
"category":"photo"}]
I want to shot the big photo but I don't know how.
The rest is working fine.
I tried to load it with '. $property->city . ' but that does not work.
I assume you need to get the 'big' photo from "photos".
I start by copying your photo data into $results.
echo '<pre>';
$results = '"photos":[{"small":"https:\/\/pararius-office-prod.global.ssl.fastly.net\/10379\/files\/photos\/api-little\/52016521.1667319824-357.jpeg?fit=crop&width=100&height=100",
"big":"https:\/\/pararius-office-prod.global.ssl.fastly.net\/10379\/files\/photos\/api-export\/52016521.1667319824-357.jpeg?width=600",
"huge":"https:\/\/pararius-office-prod.global.ssl.fastly.net\/10379\/files\/photos\/api-huge\/52016521.1667319824-357.jpeg?width=1000&fit=bounds",
"middle":"https:\/\/pararius-office-prod.global.ssl.fastly.net\/10379\/files\/photos\/api-middle\/52016521.1667319824-357.jpeg?width=1000&fit=bounds",
"category":"photo"}]';
echo "$results\n\n";
I then get a substring from the 10th character, removing photos:[
Remove the slashes.
And trim the trailing ].
We now have valid JSON.
$results = stripslashes(trim(substr($results,10),']'));
echo "$results\n\n";
To confirm the JSON is now valid I convert it to an array.
$results = json_decode($results,1);
var_export($results);
I then get the 'big' photo src.
$src = $results['big'];
echo "\n\nsrc = $src\n\n";
echo '</pre>';
Then show the photo:
echo "<img src=\"$src\" />";
The Result:

How to keep metadata fields on one line

I am trying to customize the metadata for my posts. Specifically, I want to change the separator from the default forward slash (/) to a vertical line (|). I also want to add the word "Updated" before the date displayed. And, I want to keep the option to display reading time. (*FYI: I know basically nothing about coding, just trying to override the metadata output from Astra)
I used this code to change the separator (found in a post about how to customize post meta in Astra theme):
add_filter('astra_single_post_meta', 'custom_post_meta');
function custom_post_meta($old_meta)
{
$post_meta = astra_get_option('blog-single-meta');
if (!$post_meta) return $old_meta;
$new_output = astra_get_post_meta($post_meta, "|");
if (!$new_output) return $old_meta;
return "<div class='entry-meta'>$new_output</div>";
}
See the output in image 1 - looks great!
output of new code for separator
This is close, but still needed to add "Updated" before the date. So, I used this code (from same post mentioned above):
function astra_post_date()
{
$format = apply_filters('astra_post_date_format', '');
$published = esc_html(get_the_date($format));
$modified = esc_html(get_the_modified_date($format));
$output = '<p class="posted-on">';
$output .= 'Updated: <span class="published" ';
$output .= 'itemprop="datePublished">' . $published;
$output .= '</span>';
$output .= '</p>';
return apply_filters('astra_post_date', $output);
}
See output in image 2 - content is perfect, but formatting is wrong. Can't figure out how to edit the code to get all 3 fields on the same line.
output of new code for adding "Update"
What do I need to change in the 2nd block of code to keep everything on one line like the 1st block of code?
Thanks for any help!

MODX MIGX data in chunk

I have created similar table to this one
http://rtfm.modx.com/display/ADDON/MIGX.Simple+opening+hours+table
I have successfully exported data to resource, but i want to show it in a chunk so i can display it in getresources.
I use getresources to display resources and besides title and intro text i would like to show datesTV data.
I use template chunk for migx:
[[+date:notempty=`<td>[[+date:strtotime:date=`%d.%m.%Y, %H.%M`]]</td>`:default=`<td colspan="2">No show!</td>`]]
If i use this in other chunk for getresources [[+tv.datesTV]] i get this array out:
[{"MIGX_id":"1","date":"2012-10-28 21:00:00"},{"MIGX_id":"2","date":"2012-10-28 01:45:00"},{"MIGX_id":"3","date":"2012-10-30 02:45:00"}]
How can I display this data as it should be in a chunk.
Ok here you can se how my snippet looks like..
<?php
$strJSON = $modx->resource->getTVValue('spored');
$arrJSON = $modx->fromJSON($strJSON);
foreach($arrJSON as $arrJSONDataSet)
{
foreach($arrJSONDataSet as $key => $value)
{
echo $key . ' => ';
echo $value;
echo '<br />';
}
}
With MIGX you need a snippet to parse and format the raw TV data as it's stored as JSON.
For a rough example of how to do this, refer back to the link you mentioned and try the getImageList snippet:
http://rtfm.modx.com/display/ADDON/MIGX.Simple+opening+hours+table#MIGX.Simpleopeninghourstable-ParsingtheData
You'll need to include that snippet call in your getResources chunk which is going to be really inefficient; it would be better to code up a custom snippet to retrieve the necessary data.
But see how that goes first...

mysql_affected_rows sometimes returns 0 instead of 1

I have a strange problem with php scripts - mysql_affected_rows() sometimes returns "0" for no reason.
There is a similar question #stackoverflow and answer to this question is:
MySQL only actually updates a row if there would be a noticeable difference before and after the updat.
But this is not my case. For example, if value before update is 1320402744 and value after update is 1320402944 mysql_affected_rows() anyway return "0". Is this difference not enough noticable?
Below are 3 files. As you can see, all files include file "functions.inc.php" which calls function "online()".
File "login.php" is working fine. It inserts a new row in "session" table correctly.
File "content.php" is working fine - it displays content and correctly runs function "online() in "functions.inc.php".
Then I call file "test.php". It deletes "something from sometable" correctly. Then it refreshes itself (Header("Location: /test.php");). After refreshing I am logged off.
I added this to "online()" function:
echo "affected_rows";
It returns 0.
I added more code to "online() function:
$checkuser = mysql_query("SELECT userid FROM session WHERE userid = '" . $_SESSION['id'] . "'") or die('Error');
$found = mysql_num_rows($checkuser);
echo $found;
$result = mysql_query("UPDATE session SET time='$ctime' WHERE userid='".$_SESSION['id']."'") or die('Error');
$affected_rows = mysql_affected_rows();
if ($affected_rows != 1) #session_destroy();
echo $affected_rows;
The result is 1 and 0.
I checked the database. "time" field in session table has been updated.
So, I can't understand how is it possible that the row exists, it updates correctly but mysql_affected_rows(); returns 0, and why this happends only if te same page has been refreshed.
functions.inc.php
<?php
#ob_start();#session_start();
#mysql_connect(C_HOST, C_USER, C_PASS) or die('Cant connect');
#mysql_select_db(C_BASE) or die('Cant select DB');
function online() {
$ctime = time()+1800;
if((isset($_SESSION['id']))&&(is_numeric($_SESSION['id']))) {
$query = mysql_query("UPDATE session SET time='$ctime2' WHERE userid='".$_SESSION['id']."'") or die('Error');
$affected_rows = mysql_affected_rows();
if ($affected_rows != 1) #session_destroy();
}
}
//many other functions go here
online();
?>
login.php
<?php
include_once 'configuration.inc.php';
include_once 'functions.inc.php';
//many things go here
$upd = mysql_query("INSERT INTO session VALUES ('" . $i['id'] . "','$ctime')") or die('Error2');
Header("Location: /content.php?justlogged=1");
die;
?>
content.php
<?php
include_once 'configuration.inc.php';
include_once 'functions.inc.php';
//many thing go here
echo "content";
?>
test.php
<?php
include_once 'configuration.inc.php';
include_once 'functions.inc.php';
if (isset($_GET['tid'])&&(is_numeric($_GET['tid']))){
$result = mysql_query("delete from some_table where something = '" . $_GET['tid'] . "'") or die('Error123a');
Header("Location: /test.php");
die;
}
//file content
?>
In your function.inc.php you call online() - session time is changed every second. But can it be that you're switching between pages (login, content, test) more faster than 1 second? In that case time would be the same and you'd get session destroy because of unaffected rows
Edit:
Yes. As I thought.
See how it comes:
you call login.php: after successful login it creates new session with time X. After this you're immediately redirected to content.php (time is still X) which calls online again. And of course, as you redirected immediately - time is the same.. so already at point of content.php session is already destroyed, because time wasn't changed.

Combine PDF Blob Files from MySQL Database

How do I combine numerous PDF blob files into a single PDF so that can then be printed?
<?php
include 'config.php';
include 'connect.php';
$session= $_GET[session];
$query = "
SELECT $tbl_uploads.username, $tbl_uploads.description,
$tbl_uploads.type, $tbl_uploads.size, $tbl_uploads.content,
$tbl_members.session
FROM $tbl_uploads
LEFT JOIN $tbl_members
ON $tbl_uploads.username = $tbl_members.username
WHERE $tbl_members.session= '$session'";
$result = mysql_query($query) or die('Error, query failed');
while(list($username, $description, $type, $size, $content) =
mysql_fetch_array($result))
{
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: inline; filename=$username-$description.pdf");
}
echo $content;
mysql_close($link);
exit;
?>
How do I combine numerous PDF blob files into a single PDF so that can then be printed?
You don't - at least not by simply combining the byte streams. You will need to process each file, and merge them into a new PDF document.
Some pointers, maybe one of the solutions, depending on the platform you're on, works for you:
PHP - How to combine / merge multiple pdf’s
How to combine images inside pdf by programme?
Need to merge multiple pdf’s into a single PDF with Table Of Contents sections
pdftk will merge multiple PDF files, as well as PDFsam..