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
Related
Well in a particular website when I upload the image and then try to get the link of the image I get this:
data:image/svg+xml;base64,thebase64encodeOfMyImageCode
I have seen this in many of the website. After decoding the base64 code I find that its the code of my image file (guessed out by uploading svg image file ) . Can any one tell me how this work and where the image is actually being stored . There is no direct URL like website.com/image.svg etc. This is confusing me.
This image is not stored in a file. All the data for the image is stored in the data URL. The server most likely is storing this data in a database, which is then outputted in the webpage inline.
For more information, see MDN
When base64 encoding is used for the image - you simply cannot know where the actual image is stored, if it is available at all. It might not be stored on a file at all, but only on a database, or might be stored outside the domain. On some occasions, such images are generated in real time and not stored on a disk at all.
I would appreciate your thoughts on this method.
I have a page that uploads a pic given by user and displays it after saving it in a database now that's very simple so, I wanted users to be able to modify these images as they wish, hence I decided to add filters to these images using php. Now before these images are uploaded to the database I want the user to see a preview of what the image looked like after adding the filter.
The solution I came up was to send the image to php file using a form and AJAX using POST, do all necessary checks if file was safe. create a new image and add filter and then convert it to base 64 and send it back to browser and display it using a DATA URL
something like:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4/8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">
Is this a good idea or there is something I should worry about?
Base-64 encoding adds about 33% overhead. For this reason, it is usually preferable to serve the image in binary form.
You can do this with a PHP script easily.
header('Content-Type: image/png');
echo $yourImageData;
Then in your HTML:
<img src="yourPHPScript.php?id=12345" />
hello we have a library for that actually, we make use of GD library to be able to achieve the correct image output that we would like to have, you can check more about our image library here.. by the way, our library does not only contain image library.. it has a bunch of useful libraries that you can actually use on your projects and we opensource it
http://eden.openovate.com/documentation/core/images
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
I don't know which way is better to use about uploading and saving a file in my local server.
for example I see someone that INSERT image's link in the mysql field, I'm confused right now...
I want to upload some files and show that in other situation...
what's the best and secure way to perform that?
Store all the images in a folder called photos for example. Then, save an index of the file in your database assigning it an index number and other information. Save the file in the photos folder, renaming it [index_number].jpg, or whatever extension is needed. For example, if I upload the file coolpic.jpg, it will be assigned an index number of 2845. The file itself is saved in photos/2845.jpg.
Saving in Database may make some problems like as DB performance decrease (as result of reading and writing big files), DB crashes (as a result of delete of edits of rows fields), backup problems (because of huge dump file, some problems when table needs to be repaired.
also read file from mySQL will be delivered by Apache again.
I suggest you use of normal path with rewrite mode (virtual url)
Dont use img link.. its not necessary and all it does is just making you DB larger.
You shoud store just "picture.jpg"
and in documents use <img src="images/'.$row['image'].'">
Even better, you can create a function for it (displaying pictures).
Like
function DImage($image)
{
//you can do miracles here like checking images types, if is file and so on, padding, even adding divs and vspaces..
$output = '<img src="imagesfolder/'.$image.'">';
return $output;
}
so latter all you have to do is..
echo DImage($row['image']);
PS: if you ask about $_POST & $_FILE uploading, of course.. it is impossible for you to maintain images, names and updates I'm sure..
A friend of mine told me that saving images in the database is not much of a good idea. Instead save it to the directory and then save it's name in the database and when I want to display the images in the browser I'm just going to use the name in the database to find the picture. Can anyone please guide me on how to do this starting from scratch?
Any help would be much appreciated.
By the way I am using php and MySQL.
Do two things:
accept more answers, here's how: https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work
read this tutorial: http://articles.sitepoint.com/article/php-gallery-system-minutes
What is it exactly that you don't know how to do? Your question seems to contain its own answer:
set up a directory for images, accessible to your web server
copy some image files in there
go to phpmyadmin and create a table with a filename field, for example of type varchar 100
store the image filenames in the table
write a php script that fetches image names and displays the HTML to load the images