How do I load video from my MySQL DB into my website? - php

I have uploaded a video into my MySQL DB and saved it under a BLOB column, now how do I load this data into my HTML / PHP webpage?
(I know saving video in a MySQL DB isn't recommended, but work with me on this. I'm doing this for learning purposes)
I've looked into doing this mostly with HTML, but I don't think inserting a directory through a PHP variable would work, as it's saved in my DB and not in a folder I can specify.
All help is appreciated.

You have to grab it from db and save into a file on disk and then play it. (i did not test it myself, but it should work)
after saving it into the file redirect browser to another page like this (you can pass the movie location in address or session or other ways). my example pass in url:
try this:
file_put_contents('/path/to/movie', $my_blob_movie);
header("Location: playmovie.php?location=/path/to/movie");
die();
and in playmovie.php get location as this:
$location = $_GET['location'];
and then play file....

Please clarify:
I've looked into doing this mostly with HTML, but I don't think
inserting a directory through a PHP variable would work, as it's saved
in my DB and not in a folder I can specify.
If you've got php and can create new scripts and upload them by FTP or whatever, you should be able to modify your upload script to write to a file.
Anyways, if it is uploaded using a web from, the file is already on your server.
In any case, you could write a php script to read the data from the database, and echo it out, after setting the HTTP Content-Type header:
// connect to DB and query for data
header("Content-Type: video/quicktime"); // Specify the mime type fr the specific video format
// output the video now
echo $videoblobdata;
See the Internet video mime types on Wikipedia:
http://en.wikipedia.org/wiki/Internet_media_type#Type_video

Related

PHP temporarily work with a file

I want to be able to upload a CSV file to a Webpage, and then have PHP store that information to a array and do stuff with it WITHOUT saving the CSV file to the server. How is that possible?
Looking into it, there's the normal GET and POST, which upload the file to the server. There's also PUT, but it looks like it just saves on top of an already existing file on the server.
And from the looks of the process to extract data from a CSV, PHP needs to know the location of the file.
Is it possible to just have the PHP work with the CSV file without saving it to the server somewhere? That way I don't have to worry about security issues with uploading files to a server. I don't need to hold onto the CSV data afterwords, just manipulate it in the current session.
When a file is uploaded to the server from a form, the file is stored temporarily ( $_FILES['someinputname']['tmp_name'] ) . No one says you have to do anything with this file before you can use it. You can read directly from the temporary path and forget about it. This, of course, does not leave you free from harm. Validating the file type is what you expect and not of malicious nature MUST be done before doing anything with a file you don't trust.
No lazy way around being safe.
CSV is just a text file. So you can read the file using javascript from client side and get the text from the file.
Javascript - read local text file
And then send the text to server and then work with it.

PHP load user data file

This seems like a simple question...I am trying to allow users to 'load' a saved data file with a Load button, choose file, etc. Can I read the data file directly into a variable from their file or does it need to be uploaded to the server first then opened and read closed and then unlinked?
Thank you,
Todd
Because PHP is SERVERSIDE you can't do anything without uploading the file. Unless this file already is on the server, there is no way around this problem.
I prettier way of doing it could be to use a jQuery-plugin to upload the file (without the page getting refreshed) and then access the content using ajax
It needs to be uploaded for PHP to access it, unless the file's contents are sent via JavaScript to PHP. That relies on a cutting edge browser.
Yes, you have to upload the file first because you have no access to the user's filesystem through browser neither using PHP nor JavaScript.

codeigniter image reader

hello i am trying to use Codeigniter to build a simple user image gallery where a user can upload an image and then this will be auto displayed on another page.
I have the image uploading done as explain on the user guide on Codeigniter and this works fine dumps the image in a folder in the root just placing the URL in one table area and echoing this out works but this will not cover all image names.
what I would like to know is there a way of reading what the user has uploaded and auto storing this in the database to be echoed out.
if not what's the best practice way to do this?
ps new to codeigniter framework but quite familiar to PHP mysql many thanks.
When uploading data using the uploading class you can retrieve information about the uploaded file using the following:
$this->upload->data();
This will return an array containing the name, path, dimensions, etc.
I am not totally familiar with the Codeigniter framework, but from a standard PHP/MySQL perspective this can be done using a BLOB field with MySQL. Simply fread() the uploaded image (addslashes to the content before sending the data) and send that binary output to the BLOB field with a standard SQL query. After that, you will probably need to create a separate file to actually call the image back (PHP will need to reconstruct the image from the data). Familiarize yourself with the HTTP-HEADER "Content-Type:" and how it works for images.
For instance, if you stored a .jpeg image to the database, you will want to set
header("Content-type: image/jpeg");
echo $query_string_array['Image_Data_Row'];
This would need to be in its own file (or part of file ?image=some_img) so it can display only the image. But that is the basic concept of storing an image to the database; you simply break it down, store the information, and reconstruct it when you need it.
Regards,
Dennis M.
As Yorick alluded to: after uploading the file, you can access the file path with the $this->upload->data() method. Specifically, $this->upload->data('full_path'). After the upload, assign this to a variable, then send it to a model or insert the path directly into the db. This will be the full file path to the image on your system. If you want a http accessible path stored instead, you can do something like:
$this->load->helper('url');//load url helper if not autoloaded
$fileName = $this->upload->data('file_name');
$httpPath = base_url().'uploadFolder/'.$fileName;
$this->db->insert('image_table',array('path'=>$httpPath));
HTH

PHP / SQL picture upload to a database

I need a php code and sql code that will let someone upload an image to a database. The only thing I can find is very glictchy and not accepted by some browsers. Any ideas?
NEVER store images in the database. NEVER EVER EVER EVER. There are tons of other questions posted here about it that you may want to ready up on.
Always store directly on filesystem, and store the image URL of the file in the database.
If I understood correctly, you want to upload image files (or any files) via browser to the server and save them in the database. If that is the case, read this:
http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/uploading-files-to-mysql-database.aspx
There are likely lots of available tutorials online to show you how to do this (you might take a look at this one: http://www.codewalkers.com/c/a/Database-Articles/Storing-Images-in-Database/).
I think that this is not the most efficient way to handle images, however. You might consider writing them to a folder and simply keep the name of the file and its location in the database. This stackoverflow question might help: How to store file name in database, with other info while uploading image to server using PHP?

File uploads with php - displaying a list of files

I am in the middle of making a script to upload files via php. What I would like to know, is how to display the files already uploaded, and when clicking on them open them for download. Should I store the names and path in a database, or just list the conents of a directory with php?
Check out handling file uploads in PHP. A few points:
Ideally you want to allow the user to upload multiple files at the same time. Just create extra file inputs dynamically with Javascript for this;
When you get an upload, make sure you check that it is an upload with is_uploaded_file;
Use move_uploaded_file() to copy the file to wherever you're going to store it;
Don't rely on what the client tells you the MIME type is;
Sending them back to the client can be done trivially with a PHP script but you need to know the right MIME type;
Try and verify that what you get is what you expect (eg if it is a PDF file use a library to verify that it is), particularly if you use the file for anything or send it to anyone else; and
I would recommend you store the file name of the file from the client's computer and display that to them regardless of what you store it as. The user is just more likely to recognise this than anything else.
Storing paths in the database might be okay, depending on your specific application, but consider storing the filenames in the database and construct your paths to those files in PHP in a single place. That way, if you end up moving all uploaded files later, there is only one place in your code you need to change path generation, and you can avoid doing a large amount of data transformation on your "path" field in the database.
For example, for the file 1234.txt, you might store it in:
/your_web_directory/uploaded_files/1/2/3/1234.txt
You can use a configuration file or if you prefer, a global somewhere to define the path where your uploads are stored (/your web directory/uploaded files/) and then split characters from the filename (in the database) to figure out which subdirectory the file actually resides in.
As for displaying your files, you can simply load your list of files from the database and use a path-generating function to get download paths for each one based on their filenames. If you want to paginate the list of files, try using something like START 0, LIMIT 50; in mySQL. Just pass in a new start number with each successive page of upload results.
maybe you should use files, in this sense:
myfile.txt
My Uploaded File||my_upload_dir/my_uploaded_file.pdf
Other Uploaded File||my_upload_dir/other_uploaded.html
and go through them like this:
<?php
$file = "myfile.txt";
$lines = file($file);
$files = array();
for($i=0;$i<=count($lines)-1;$i++) {
$parts = explode($lines[$i]);
$name = parts[0];
$filename = parts[1];
$files[$i][0] = $name;
$files[$i][1] = $filename;
}
print_r($files);
?>
hope this helps. :)
What I always did (past tense, I haven't written an upload script for ages) is, I'd link up an upload script (any upload script) to a simple database.
This offers some advantages;
You do not offer your users direct insight to your file system (what if there is a leak in your 'browse'-script and you expose your whole harddrive?
You can store extra information and meta-data in an easy and efficient way
You can actually query for files / meta-data instead of just looping through all the files
You can enable a 'safe-delete', where you delete the row, but keep the file (for example)
You can enable logging way more easily
Showing files in pages is easier
You can 'mask' files. Using a database enables you to store a 'masked' filename, and a 'real' filename.
Obviously, there are some disadvantages as well;
It is a little harder to migrate, since your file system and database have to be in sync
If an operation fails (on one of both ends) you have either a 'corrupt' database or file system
As mentioned before (but we can not mention enough, I'm afraid); _Keep your uploading safe!_
The MIME type / extension issue is one that is going on for ages.. I think most of the web is solid nowadays, but there used to be a time when developers would check either MIME type or extension, but never both (why bother?). This resulted in websites being very, very leaky.
If not written properly, upload scripts are big hole in your security. A great example of that is a website I 'hacked' a while back (on their request, of course). They supported the upload of images to a photoalbum, but they only checked on file extension. So I uploaded a GIF, with a directory scanner inside. This allowed me to scan through their whole system (since it wasn't a dedicated server; I could see a little more then that).
Hope I helped ;)

Categories