Download a file from MySQL database [duplicate] - php

I have an upload phone app that uploads either images or audio recordings. The image can be uploaded to a mysql database and displayed no problem in a webpage, The problem i am having is playing the audio on the webpage, i have tested the webpage by playing mp3 files i have placed in the database myself though the webpage. When i upload an mp3 file from the phone or emulator nothing plays. The database is populated with a file size and on the webpage a media player does load, but nothing plays
Edit 1
Since writing this i have learnt that android records in .amr files and not .mp3 which i had been creating the file as. Since then i can play the file on my desktop using either quicktime or real player. When i attempt to play the file through a web browser i get a quicktime logo with a question mark in the middle. Below is my code for playing the file.
header('Content-type: audio/amr');
$query = mysql_query("SELECT * FROM media WHERE media_id= '$idd'");
$row = mysql_fetch_array($query);
echo $row['file'];
Embed code where 110 is $idd in the above
<object data="sound.php?id=110 width='391' height='298'"> <embed src='sound.php' width='391' height='298'></embed>'ERROR' </object>
Edit 2
If i put the same file directly on my server and NOT in the database this code downloads the file
<OBJECT classid='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B' width="100" height="100"codebase='http://www.apple.com/qtactivex/qtplugin.cab'>
<param name='src' value="apr.amr">
<param name='autoplay' value="true">
<param name='controller' value="true">
<param name='loop' value="true">
<EMBED src="apr.amr" width="100" height="100" autoplay="true" controller="true" loop="true" pluginspage='http://www.apple.com/quicktime/download/'>
</EMBED>
</OBJECT>
Edit 3 upload code
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {
$size = $_FILES['image']['size'];
$type = $_FILES['image']['type'];
// Temporary file name stored on the server
$tmpName = $_FILES['image']['tmp_name'];
// Read the file
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
fclose($fp);
$data = addslashes($data);
// Create the query and insert
// into our database.
$query = "INSERT INTO media";
$query .= "(file, file_size, file_type) VALUES ('$data','$size','$type')";
$results = mysql_query($query, $link);
$mediaid = mysql_insert_id();
$gender = $_POST['gender'];
$cat_id = $_POST['cat'];
$name = $_POST['name'];
$lat = $_POST['lat'];
$lon = $_POST['lon'];
$user = $_POST['user'];
$query="INSERT INTO instance (name, gender, cat_id, lon, lat, user_id) VALUES ('$name', '$gender', '$cat_id', '$lon', '$lat', '$user')";
$result=mysql_query($query);
$instanceid = mysql_insert_id();
$query4 = "INSERT INTO media_link";
$query4 .="(media_id, instance_id) Values ('$mediaid','$instanceid')";
$results4 = mysql_query($query4, $link);
}
// Close our MySQL Link
mysql_close($link);
?>

Yes.
// Get the file contents from the database...
// Assuming a DB wrapper here.
$results = $db->query('SELECT `file_contents` FROM `mp3s` WHERE `id` = :id', array(':id' => $_GET['id'])->execute();
$fileContents = $results[0]['file_contents'];
// Send appropriate headers for content type and force download
header('Content-Type: mpeg/audio');
header('Content-Disposition: attachment; filename="BullsOnPowerade.mp3"')
echo $fileContents;

So if you have an AMR file (I haven't tested this, so you'll have to try this out) you will need to extract the AMR audio and convert it to MP3. Unfortunately it appears to be rather rare in Java, but a (rather painful) Google search turned up this link: http://www.benmccann.com/blog/extracting-amr-audio-from-android-3gp-files/ which claims to demonstrate extracting the audio from a 3gpp video file, so they at least have an AMR parser. I would look at the underlying library they use, isobox4j (discussed at http://groups.google.com/group/android-developers/browse_thread/thread/245c9de4132c2ab0?fwc=1) and see if you can either:
Extract the audio in the phone app, convert to mp3 and then upload the mp3 (probably the easiest option, frankly: keeps the server simple).
Extract the audio on the server and convert to mp3 before streaming the mp3 to the web page (probably harder, frankly).
Good luck.
EDIT: or you could hope the client has an appropriate browser plugin: the broken quicktime mark indicates quicktime doesn't have a codec for it.
EDIT 2: You probably need to set a content-type for the plugin so it knows what to do. For AMR try adding
header("Content-type: audio/amr");
before the echo statement in your PHP and if that isn't enough add a mime type to your embed like this:
<object data="sound.php?id=110 width='391' height='298'"> <embed src='sound.php' type="audio/amr" width='391' height='298'></embed>'ERROR' </object>
See if that works.
EDIT: try this. Not sure if the $row['file'] is considered a string or an array, but the trim might do what you need.
<?php
header('Content-type: audio/amr');
$query = mysql_query("SELECT * FROM media WHERE media_id= '$idd'");
$row = mysql_fetch_array($query);
echo trim($row['file']);
exit;

If HTML5 is not an option, for inline playback, the best option supported out of the box by the majority of current browsers will be flash based. Flash still requires a plugin, but it's much more widely installed than Quicktime AFAIK.
The Google mp3 player that Tomasz suggested is a good choice. There are many more out there though. See for example http://www.webdesignbooth.com/10-easy-to-implement-flash-based-mp3-players-for-your-website/

This is a really really broad question, but here are some starter tips:
Look at the apache httpcomponents project for a good HTTP library, including the ability to do multi-part MIME attachments and POST requests
Take a look at the PHP docs for handling file uploads
If you don't want your app to lock up during uploads, I would encourage you to look at the android AsyncTask pattern and supporting classes so that your UI thread does not block during the uploads
There are any number of ways to play an audio file in a web page (Flash being the most prevalent probably)

Yes, the audio file is just a binary file and can be saved an a blob data field. Then recalled from a query. Then play it any way you want.You may be better off saving the audio file to local storage and keeping track of where you put it in the database.

You could use Flash on the web page. On 2.3 you could use the audio tag. See HTML5 <audio> tag on Android .

If you want to embed a video on your webpage, you can read this support article on Apple's website and this is a list of tag attributes...
Hope this can help! :)

easiest method is to use nowadays is HTML5 tag..
html5 audio
<audio src="horse.ogg" controls="controls">
Your browser does not support the audio element.
</audio>
using google mp3 player - replace mp3_file_url with the actual url of the file... although im not sure if it runs .amr files
<embed type="application/x-shockwave-flash" flashvars="audioUrl=MP3_FILE_URL" src="http://www.google.com/reader/ui/3523697345-audio-player.swf" width="400" height="27" quality="best"></embed>
EDIT:
quicktime - change WIDTH and HEIGHT in both cases.. and choose false/true in the params you want
<OBJECT classid='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B' width="_WIDTH_" height="_HEIGHT_"codebase='http://www.apple.com/qtactivex/qtplugin.cab'>
<param name='src' value="apr.amr">
<param name='autoplay' value="true/false">
<param name='controller' value="true/false">
<param name='loop' value="true/false">
<EMBED src="apr.amr" width="_WIDTH_" height="_HEIGHT_" autoplay="true"
controller="true" loop="true" pluginspage='http://www.apple.com/quicktime/download/'>
</EMBED>
</OBJECT>

Related

How to add HTML 5 audio player?

I'm getting source file from a folder. When I select a ready-made mp3 file like song as src in audio tag, It works. But when I select a mp3 file(call recording file) created from base64 encoded string, I doesn't work. File is perfectly created as I checked in folder and played. Problem is that when I move curser on player, it becomes transparent just like an image. If anyone know answer then please explain with an example. Thank You. Here is my code.
<?php
$data = $_REQUEST['data'];
$filename = $_REQUEST['filename'] . ".3gpp";
$imei = $_REQUEST['imei'];
$dir = __DIR__ . "/recordings/";
$file = file_put_contents($dir . $filename, base64_decode($data));
$rloc = "recordings/" . $filename;
$str = "INSERT INTO recording(data, filename, imei) VALUES('$rloc', '$filename', '$imei')";
$qry = mysql_query($str);
?>
<html>
<audio controls autoplay="">
<source src="<?php echo $row['data']; ?>">
</audio>
</html>
You need to prefix the data:audio/mp3;base64, to the Data URI. Use this instead:
<audio controls autoplay="">
<source src="data:audio/mp3;base64,<?php echo $row['data']; ?>">
</audio>
I found this short example:
<audio controls autoplay>
<source src="horse.ogg" type="audio/ogg">
<source src="horse.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
May it's helpful.
<audio controls="controls" autobuffer="autobuffer" autoplay="autoplay">
<source src="data:audio/wav;base64,UklGRhwMAABXQVZFZm10IBAAAAABAAEAgD4AAIA+AAABAAgAZGF0Ya4LAACAgICAgICAgICAgICAgICAgICAgICAgICAf3hxeH+AfXZ1eHx6dnR5fYGFgoOKi42aloubq6GOjI2Op7ythXJ0eYF5aV1AOFFib32HmZSHhpCalIiYi4SRkZaLfnhxaWptb21qaWBea2BRYmZTVmFgWFNXVVVhaGdbYGhZbXh1gXZ1goeIlot1k6yxtKaOkaWhq7KonKCZoaCjoKWuqqmurK6ztrO7tbTAvru/vb68vbW6vLGqsLOfm5yal5KKhoyBeHt2dXBnbmljVlJWUEBBPDw9Mi4zKRwhIBYaGRQcHBURGB0XFxwhGxocJSstMjg6PTc6PUxVV1lWV2JqaXN0coCHhIyPjpOenqWppK6xu72yxMu9us7Pw83Wy9nY29ve6OPr6uvs6ezu6ejk6erm3uPj3dbT1sjBzdDFuMHAt7m1r7W6qaCupJOTkpWPgHqAd3JrbGlnY1peX1hTUk9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9+f35+f35/gH+Af4B/gICAgICAgICAgICAgYCBgIGAgIGAgYGBgoGCgYKBgoGCgYKBgoGCgoKDgoOCg4KDgoOCg4KDgoOCg4KDgoOCg4KDgoOCg4KDgoOCg4KDgoOCg4KDgoOCg4KDgoOCg4KDgoOCg4KCgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGBgYCBgIGAgYCBgIGAgYCBgIGAgYCBgIGAgYCBgIGAgYCAgICBgIGAgYCBgIGAgYCBgIGAgYCBgExJU1RCAAAASU5GT0lDUkQMAAAAMjAwOC0wOS0yMQAASUVORwMAAAAgAAABSVNGVBYAAABTb255IFNvdW5kIEZvcmdlIDguMAAA" />
</audio>
A Data URI takes the format:
data:[][;charset=][;base64],
The MIME-type specifies what type of data the URI contains.
The charset in which it is encoded.
last the encoded data
It looks like you are using wrong variable to set the source. You don't have $row['data'] defined in your php code. Maybe it should be $rloc instead:
<audio controls autoplay="">
<source src="<?php echo $rloc; ?>">
</audio>
Edit:
Did you convert uploaded file (3gpp) to other formats (mp3, ogg), or did you just rename it to a different extension? Looking at binary data at links you posted in comments to other answer this is your situation: all links you posted are exactly the same file, with different name. Binary, they are identical, just file name is different. Format details for the file which is on all your links are:
Format : MPEG-4
Format profile : 3GPP Media Release 4
Codec ID : 3gp4
You can not just change file extension and expect it will play in browser. You need to convert file to other format. You can use ffmpeg or any other conversion tool to convert from 3gpp to mp3 and ogg.
It is possible to use a Data URI for audio, as others have suggested. However, it's not a good idea. Not only are you adding 33% overhead for this encoding, and the CPU overhead on both ends, but there is a 1 MB limit which won't take long to hit with audio.
If you must serve your audio data this way, you can simply reference your PHP script which outputs nothing but raw binary audio data.
<audio src="/audio.php?id=12345"></audio>
Be sure to set the appropriate Content-Type header.
Finally, note that you have serious security issues in your code. As it stands right now, anyone can write pretty much whatever they want to whatever path they want on your hard drive, because you're letting them specify the filename on disk which can contain ../. Also, your SQL is subject to SQL injection attacks. Use prepared/parameterized queries to avoid this issue entirely.

PHP Email Pixel Tracking Not Firing in Yahoo Mail

In order to track email open rates, I'm firing a pixel in a mass email I'm sending from my server. The script is working in Mac Mail. The email is received and the pixel is downloaded.
However, it's not working in the Yahoo mail client. The email is received, the referenced images are downloaded and shown, however the pixel does not fire/download, nor does the php script run (to my knowledge). Does anyone know why this would happen with Yahoo mail client and potentially other clients that I have yet to test?
Here is the html img tag:
<img src="http://mysite.com/email_track.php?email=email_value&country=country_value&state=state_value" />
Here is the php script:
<?php
// Database code omitted
$result= mysql_query("INSERT INTO `CelebrationOpens` SET `time` = NOW(), `country` = '$country', `state` = '$state', `email` = '$email' ") or die(mysql_error());
// Create an image, 1x1 pixel in size
$im=imagecreate(1,1);
// Set the background colour
$white=imagecolorallocate($im,255,255,255);
// Allocate the background colour
imagesetpixel($im,1,1,$white);
// Set the image type
header("content-type:image/jpg");
// Create a JPEG file from the image
imagejpeg($im);
// Free memory associated with the image
imagedestroy($im);
?>
I've also tried to fire the pixel like this:
$name = './concert/pixel.png';
$fp = fopen($name, 'rb');
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));
fpassthru($fp);
exit;
You mentioned you are using all embedded images, which is why they always show, independently of whether you opt to download images for your email. Embedding images is the workaround for image blocking, but results in a large filesize for the email.
All non-embedded images need to be shown to work. Your tracking pixel is one. Apple clients download all images by default, while other clients do not. The tracking image is not firing because you haven't downloaded images on your email in Yahoo (or any other client).
Unfortunately this is a limitation to open tracking and why the data is incomplete and always skewed towards Apple clients. Open tracking really means 'They opened it and unblocked the images OR They opened on Apple'.
I was able to find the culprit, and it was related to caching the pixel link. I appended a random string to the img src and it works in both ymail and gmail now.
The image tag now looks something like this:
<img src="http://mysite.com/email_track.php?email=email_value&country=country_value&state=state_value&random_value=<?php echo rand() ?>" />
Thanks for the help in guiding me towards this discovery.

Playing mp4 files from rar or zip files

I have one problem. I would like to order from (zip) files play video in such a way to get out connection so you can include it in HTML5 badge. As I gave an example. But this not working.
<?php $video = fopen('zip://video.zip#video.mp4', 'r'); ?>
<video>
<source src='<? echo $video; ?>' type='video/mp4' />
</video>
$video in the above code is just a server-side file handle you could use to read the file from the zip. It's not directly usable for the browser.
You'll need to handle reading the file and returning it in a separate HTTP request. Usually you'd use a second script for this. (Or if your video is relatively small, you might be able to use data urls, but it's not something I'd try to do.) Additionally, if you want to allow for byte range requests, you'd have to handle that yourself in your video serving logic.
Here's a fairly simple scenario:
My videos.zip file contains a couple of different videos, and I want to retrieve a specific one and show it on a page called video.php
First I have my viewer, say video.php, [edit: containing the video tag and with a URL to my second script as the source. Since I might want to serve the other file at some point, I set it up to accept a filename in the v query parameter.]
..some html/php..
<video>
<source src='zipserve.php?v=itsrainintoast.mp4' type='video/mp4' />
</video>
..more html/php..
Then, in zipserve.php I have something like this:
$filename = $_GET['v']; //You probably want to check that this exists first, btw.
$fp = fopen('zip://videos.zip#'.$filename, 'r');
if($fp)
{
header('content-type: video/mp4');
//Note: you should probably also output an appropriate content-length header.
while(!feof($fp))
{
echo fread($fp, 8196);
}
fclose($fp);
}
else
{
echo 'Some error message here.';
}
--Addendum--
It should also be noted that this'll require the zip php extension to be enabled.
A more complete example of a video fetching script with range handling and the like can be found in the question at mp4 from PHP - Not playing in HTML5 Video tag but you'd need to tweak it to allow reading from the zip file.

PHP - display thumbnails of images from Mysql

I have a table in my mysql db that contains path for images.
I want to display thumbnails of this images.
Can you tell me what is the best solution for that.
This is how i display the full size images from db:
<?php for($i=1; $i<=5; $i++)
{$query = mysql_query("SELECT * FROM photos WHERE product_id='".$i."'");
$row = mysql_fetch_array($query);
$t="img/";
$file = $t .$row[1];
echo "<li><img src = " . $file . "></li>";
} ?>
You can use this http://mightystuff.net/php-thumbnail-script
Either resize the images in css/html (bad) or make a php script that gets a filename as input then returns the scaled down version as output (better) or have thumbnails stored on your server (best)
You should avoid having to resize the images using CSS or HTML. Before saving the images onto the server, you should use ImageMagick to resize it. Do you really want to store a bunch of 6MB images? If that's not feasible, then saving the images as is and pointing to the path would suffice. However, if you end up with hundreds or thousands of images, you'll want to explore a better option.
I agree with the approach of storing the file path (instead of the binary file itself).
So your question is "how do you create a thumbnail?"
The best answer is ImageMagick:
http://www.imagemagick.org/
http://valokuva.org/?p=45
http://php.net/manual/en/book.imagick.php

Headache with this programming problem. (PHP, Tiff images and Duplex printers)

i'm just feeling that my head will explode unless someone help me with this problem:
I have stored a pair of TIFF images (related by a common key) for each one of almos 100.000 registries. And I create a PHP script that receives a key and echo the tiff image, so the browser return the tiff image:
<?php
// Determine the primary key to relate with the image table
$numero_registro = $_GET['numero_registro'];
$imagen = $_GET['imagen'];
if ($numero_registro != "")
{
$con = mysql_connect("localhost","XXXXX","XXXXXX");
if (!$con)
{
die('Problems with db: ' . mysql_error());
}
mysql_select_db("XXXXX", $con);
$result = mysql_query("SELECT img FROM image_table i WHERE i.fk_civil_registry_code = $numero_registro");
$i = 1;
while($row = mysql_fetch_array($result) )
{
if ( $imagen == $i )
{
#img is a long blob field
$ext="tiff";
header("Content-type: image/{$ext}");
echo $row['img'];
}
$i++;
}
mysql_close($con);
}
?>
This just works and the tiff image is displayed by the browser. But, this is a tiff image, so is displayed lonely (and viewed using alternaTiff). Until know this was no problem, cause I just needed to print a single image. But now my boss buy a big automatic duplex printer, and put it on his office, so I need a way to generate a pdf (of two pages) and put both images (echo $row['img'];) each one on a single page, so they can print the PDF.
Can anyone help me to do that?
Thank you very much.
So you want to generate a 2-page PDF which consists of a tiff image on each page?
Perhaps the following links will be of interest:
http://www.fpdf.org/
http://kevin.vanzonneveld.net/techblog/article/php_tiff2pdf/
Then you can just flush the PDF to the browser.
Are you stuck with PHP? If you can work with ASP.NET, my company has a set of tools that will display and print TIFF images from AJAX controls as well as code that will generate self-printing PDF files. If you did the latter, you could keep your web work in PHP and hook up to a .NET service that takes N tiff files and generates a single printable PDF.
To give you a sense of what that would look like, the C# code to take two tiff images and convert to PDF would be:
FileSystemImageSource images = new FileSystemImageSource(pathToTiff1, pathToTiff2);
PdfEncoder encoder = new PdfEncoder();
encoder.CreateSelfPrintingPdf = true;
encoder.Save(outputStream, images, null);
Instead of a pdf document you could also use an html document with page-break-before css properties. e.g. try
<html>
<head><title>...</title></head>
<body>
<div><img src="http://sstatic.net/so/img/logo.png" /></div>
<div style="page-break-before:always;"><img src="http://sstatic.net/so/img/logo.png" /></div>
</body>
</html>
and then go to the print preview in your browser.

Categories