Issue in php with "header" - php

I am developing an android app that download songs(so type of data is blob) from db.
I have the following download image code example:
<?php
if($_SERVER['REQUEST_METHOD']=='GET'){
$id = $_GET['id'];
$sql = "select * from images where id = '$id'";
require_once('dbConnect.php');
$r = mysqli_query($con,$sql);
$result = mysqli_fetch_array($r);
header('content-type: image/jpeg');
echo base64_decode($result['image']);
mysqli_close($con);
}else{
echo "Error";
}
How do I change "header" and "echo"(under header) to download an mp3 audio file ?

You'll want to send the following header for a .mp3 file:
Content-Type: audio/mpeg3
Refer to https://www.sitepoint.com/web-foundations/mime-types-complete-list/ for a good list of MIME types.

Related

Can't see result of application/json output with PHP

I'm building a very simple API in PHP to give me data from my DB in application/json format.
Here is my muscular-groups.php file :
<?php
include '../inc/db.php';
if (isset($_GET['id'])) {
$req = $pdo->prepare('SELECT * FROM muscular_groups WHERE id = ?');
$req->execute([$_GET['id']]);
$res = $req->fetchAll();
}
else {
$req = $pdo->prepare('SELECT * FROM muscular_groups');
$req->execute();
$res = $req->fetchAll();
}
header('Content-Type: application/json');
echo json_encode($res);
The problem is that in local all works well as you can see below :
But when I want to access it on my server (at www.example.com/api/muscular-groups), I have a white screen without any error message or error logs.
What is strange : If I replace header('Content-Type: application/json'); and echo json_encode($res); with var_dump($res) in muscular-groups.php, it appears on the screen.
P.S.: very strange, only one of my endpoints works on my server (eg: www.example.com/api/offers) and displays a json output, here's the code :
<?php
include '../inc/db.php';
$req = $pdo->prepare('SELECT * FROM offers');
$req->execute();
$res = $req->fetchAll();
header('Content-Type: application/json');
echo json_encode($res);
I'm lost...
I finally got it : the problem was there was UTF-8 characters, and PDO was not configured to give them as-is. So $pdo->exec("SET NAMES 'utf8'"); saved my life!

Replacement of json url with php url not working

I am working on android app in which a listview with some text retrieve from server The source is here source link. This is my php code
<?php
require_once("dbConnect.php");
$sql = "SELECT image,fullname,location from uploadfinding";
$res = mysqli_query($conn,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result, array(
"image"=>$row[0],
"fullname"=>$row[1],
"location"=>$row[2]));
echo " over";
}
echo json_encode($result);
mysqli_close($conn);
?>
and this is my json response
Connected successfully over over over[{"image":myurl\/uploadfinding\/uploads\/2016-04-25 06:38:051461584281226.jpg","fullname":"adi","location":"fgh"},{"image":myurl\/uploadfinding\/uploads\/2016-04-25 06:38:201461584297706.jpg","fullname":"adi2","location":"fgh2"},{"image":myurl\/uploadfinding\/uploads\/2016-04-25 06:45:441461584739479.jpg","fullname":"adi23","location":"cn"}]
I have lots of things tried but result is none.the complete source is here:
I am not posting here any java file bacause this example is worked fine for this url:
http://api.androidhive.info/json/movies.json
But when i replace this with my url:
http://myurl/PhotoUpload/getAllImages.php
It return only empty activity.
Try This
<?php
header("content-type:application/json");
require_once("dbConnect.php");
$sql = "SELECT x1,x2,x3 from table_name";
$res = mysqli_query($conn,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result, array(
"x1"=>$row["x1"],
"x2"=>$row['x2'],
"x3"=>$row["x3"]));
echo " over";
}
echo json_encode($result);
$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($result));
fclose($fp);
mysqli_close($conn);
?>
replace your url "http://myurl/PhotoUpload/getAllImages.php" with "http://myurl/PhotoUpload/results.json"
Just to clarify:
from within your app, you can simply refer to the emulator as
'localhost' or 127.0.0.1.
or
If you are running with real device means pass correct url like
http://xxx.xxx.xxx.xxx/android/php_server/register.php
Edit:
Try compare your url,
myurl/uploadfinding/uploads/2016-04-25 06:38:051461584281226.jpg"
with
"http://api.androidhive.info/json/movies/1.jpg" this.
At your response, " double quote is missing at front of image tag
pass image link like,
"your_link"

PHP - download dialog box for given videos url

I am pretty new in php .so hopefully this all make sense . I am using below script to extract url from database.I want down-loader will popup in browser while reading the url from database....
$q=" Select url from videos where id = '".$_REQUEST['id']."' ";
$result=mysql_query($q);
while( $rows =mysql_fetch_assoc($result) )
{
$url=$rows['url '];
//here i want code to send this url to down-loader of browser..
//and browser should popup the down-loader
$myarray1=array("url "=>$url );
}
I have searched codes on google but i don't understand how to use for this scenario...
First of all, you need to fix the SQL Injection problem.
Use MySQLi instead of MySQL
Here's a code:
$request_id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
if(!empty($request_id)){
$mysqli = new mysqli("localhost", "user", "password", "database_name");
$request_id = $mysqli->real_escape_string($request_id);
$query = "SELECT url FROM videos WHERE id='" . $request_id . "'";
$query_result = $mysqli->query($query);
if(!empty($query_result)){
if($query_result->num_rows > 0){
$my_result_array = array();
while($row = $query_result->fetch_assoc()){
$my_result_array[] = $row['url'];
}
}
}
mysqli_close($mysqli);
}
Concerning the download problem, you need to redirect the user to a PHP page with a specific header
<?php
$file_name = "";
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment;filename=\"" . $file_name . "\"");
header("Cache-Control: max-age=0");
?>
You need to add the $file_name and the specific Content-Type.
Here's a list of Content-Types: http://davidwalsh.name/php-header-mime
In order to redirect a user to the download page, you can use the PHP header() function.

Viewing Image from database

I have a file in which a user can view its stored files. I want that only the logged in user and it can only be viewed by that member who has stored that file. It works fine when I view other files like .html, .txt etc. But when I view any image, it doesn't work.
This is my script :
require ('config.php');
$id = intval($_GET['id']);
$dn2 = mysql_query('select authorid from files where uploadid="'.$id.'"');
while($dnn2 = mysql_fetch_array($dn2))
{
if(isset($_SESSION['username']) and ($_SESSION['userid']==$dnn2['authorid'] or $_SESSION['username']==$admin)){
$query = "SELECT data, filetype FROM files where uploadid=$id"; //Find the file, pull the filecontents and the filetype
$result = MYSQL_QUERY($query); // run the query
if($row=mysql_fetch_row($result)) // pull the first row of the result into an array(there will only be one)
{
$data = $row[0]; // First bit is the data
$type = $row[1]; // second is the filename
Header( "Content-type: $type"); // Send the header of the approptiate file type, if it's' a image you want it to show as one :)
print $data; // Send the data.
}
else // the id was invalid
{
echo "invalid id";
}
}
}
How can I view the image?

File Downloading error from database php

I have read every tutorial I can find on this topic, but none of them helped fix my issue. I don't know why, but my code is just giving me an html file whenever I click on the download button, instead of downloading an image file.
HTML:
Download Now
PHP:
<?php
$id= $_GET['id'];
$con= mysqli_connect("localhost","root","","dbname");
if (mysqli_connect_errno($con))
{
echo "<script type=\"text/javascript\">alert('Failed To connect to MySQL: "+"mysqli_connect_error();"+"');</script>";
}
else
{
$sql = "SELECT * FROM users_files WHERE file_id = ".$id;
$result = mysql_query($sql);
$row_cnt = mysqli_num_rows($result);
echo "<script type=\"text/javascript\">alert('".$row_cnt."');</script>";
if(!$result || !mysql_num_rows($result)){
echo "<script type=\"text/javascript\">alert('Invalid file chosen.');</script>";
//echo '<script language="javascript" type="text/javascript" >';
//echo ' window.location.assign("ViewMyFiles.php");';
//echo '</script>';
mysqli_close($con);
}
$curr_file = mysql_fetch_assoc($result);
$size = $curr_file['file_size'];
$type = $curr_file['file_type'];
$name = $curr_file['file_name'];
$content = $curr_file['file_content'];
header("Content-length: ".$size."");
header("Content-type: ".$type."");
header('Content-Disposition: attachment; filename="'.$name.'"');
readfile($content);
$_SESSION['email']=$nemail;
mysqli_close($con);
}
?>
I named the above PHP file getfile.php and I'm getting on download result as getfile.htm.
Debug!
Comment out the headers and see, what really gets to the client. Then comment them in and check in browser dev console (header + response).
You're also vulnerable to SQL injections.

Categories