I just want to know if it's possible. How do I save an html page and all of its contents to a database? like for example, I have a database wherein it consists all HTML pages.
Just want to know if its possible. And how to retrieve it too. We're using PHP as our language.
Thank you.
Well, you'll need to:
Grab that page by using a HTTP request, just like your browser does
Parse that HTML to find external resources (script, img, object, etc)
Grab those external resources
Save all them on your database into a BLOB field
Optionally alter your original HTML document, to change that resources location
Is this what you are trying to do? http://www.phpfreaks.com/forums/index.php?topic=219271.0
You can simply store the $out in the db instead of saving as html
Assuming MySQL, here is the way to connect to the database and write data into it.
PHP and MySQL
HTML is just text. You can tore it in a database in a TEXT field.
There are plenty of DBMS you can use and plenty of ways to do it.
You can have a look at the PDO extension to directly consume a MySQL or SQlite connection for instance.
You can also use an ORM like Doctrine
If you are trying to save the final results of your PHP script (ie. what is sent to the browser) you will need to look into Output Buffering.
As others already suggested, yes its possible to save HTML pages inside databases like mysql or sqlite etc. Another way you can perceive "databases" is flat files. Therefore, just like web crawlers or tools like wget/curl that crawls(and download) pages to disk, you can program something like that in PHP (using libraries such as curl) and save those pages to your disk. How to retrieve?? just display them with web browser OR do normal opening of file , display the contents and closing the file, all with PHP.
Related
I will try to keep this simple without lots of extra information. I have been investigating MongoDB and I believe it will work well for my next project. There is one thing I am fuzzy about though: storing and retrieving files (chunks) from GridFS.
Lets take a CMS for example. If I wanted to display (output via the browser) an image, I would go my MySQL database, find the key and pull the metadata that would include the file source on the File System and then display an image tag with that href. I know that I can store image/video/etc files in Mongo beautifully and I can retrieve binary, but if I wanted to display that file (push it to the browser) would I have to write the contents to a temporary file and then echo my img tag with the href? That can't be more efficient.
I feel like I'm missing something. For this circumstance, is MongoDB any better?
For clarification: I'm using PHP and Apache on a typical LAMP stack (development not production) and working on a platform to enable creative collaboration between artists. So, I would have several artists collaborating on the same files, and I would like to be able to search inside those files, index them, keep all metadata together and employ sharding. I really seems like MongoDB is the way to go.
Thanks!
Michael
If you want to store binary files in the database, you can serve them directly without writing them to a filesystem first. All you have to do is write a PHP script (or whatever) to send the data in response to an incoming HTTP request, with the proper media type in the response HTTP header.
So if we're talking about images, in your HTML you'd do something like this:
<img src="/your_img_script.php?image=1234"></img>
Then just write your_img_script.php to lookup image ID 1234 and dump the data out with a image/jpeg (or whatever) content type header.
I want to display an HTML page at a later date from the database by the user just having to enter the ID of the page.
Is there a better way to do this than just saving it as text in the database?
Thanks!
Storing huge chunks of text (especially markup) in a DB tends to come with a host of annoying problems. I would instead just store the name of the file in the database and store the actual file on the filesystem.
You could store the actual useful information from the file in the database.
Like the title of the page and the content for each section of the page, and then render it using a template from on the disk somewhere. This means you can abstract all the information that is the same from every HTML file and save disk space too. (Its a small bonus though, it might be simpler to do as the other answer suggested and just save the file name in the database and have the file on the filesystem.) Having the information in the database itself might mean it will get backed up with the database, which might be useful to you. Who knows...
Something like flatpages from the django framework. https://docs.djangoproject.com/en/dev/ref/contrib/flatpages/?from=olddocs
I am trying to generate a RSS feed from a mysql database I already have. Can I use PHP in the XML file that is to be sent to the user so that it generates the content upon request? Or should I use cron on the PHP file and generate an xml file? Or should I add the execution of the php file that generates the xml upon submitting the content that is to be used in the RSS? What do you think is the best practice?
All three approaches are technically possible. However, I would not use cron, because it delays the update process of your XML-files after the database content has changed.
You can easily embed PHP-Code in your XML-files, you just have to make sure that the files are interpreted as PHP on the serverside, either by renaming them with a *.php extension or by changing the server directives in the .htaccess-file.
But I think that the best practice here is to generate new XML-files upon updating the database contents. I guess that the XML-files are viewed more often than the database content changes, so this approach reduces the server load.
Use a cron to automate a PHP script that builds the XML file. You can even automate the mail part as well in your PHP.
The third method you mentioned. I don't understand how cron can be used here, if there are data coming in users' request. The first method cannot be implemented.
Set the Content-type header to text/xml and have your PHP script generate XML just as it would generate any other content. You may want to consider using caching though, so you don't overwhelm the server by accident.
I want to read the content from website and save then into a csv file in php, can anyone please tell me how I can do this.
How do you want to save a website's content as csv file? CSV means comma separated values, and it's really easy to save things as csv file but a website's content?
You say "the content from a website" - normally you'll start reading one sites content, which includes html markup, scripts and styles. Or do you only want to get the text contents or some meta data?
If your server supports opening urls via fopen I'd try this one (php.ini option: allow_url_fopen) - otherwise you'll have to use cURL or something.
Here's some more information about reading websites in php.
Regarding the storage of websites as csv I think you should be more precise what you want to achieve.
Regards, Daniel
There's no instant-magic answer for your question. We (You) need to know which website is in question, how the table is presented. If you know everything about your scenario, you should use PHP's DOM functions and parse your table then export it to a CSV.
I need a simple code to upload images to mySQL using PHP... short! snippet... and is it possible to upload an html, css file to mySQL?... its reason is complicated but all answers are appreciated!... EDIT:: say I have 1000 users.. and they each have their own layout for their page.. So inside their MYSQL record will be a html file, css file(possibly), and image(s)...
I am a big fan of using a filesystem for storing physical files, i've yet to see any solid reason why they are better off in a database.
To automate this process you could have a shell script called through exec
exec("/home/some/path/my_filesystem_creator.sh ".escapeshellarg($args));
or PHP's native mkdir or anything really. If you went for a structure like:
/common/
/userdirs/1/
/userdirs/2/
essentially all i would imagine you would need to do is create a user dir, and copy into it the default versions of their site assets - images/css/html etc.
This should be easy enough to manage
Are you asking how to store a file in the database?
http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/uploading-files-to-mysql-database.aspx
Or do you need to know how to upload a file to your web server in order to display it in a PHP/MySQL website?
Your page would be faster, if you generate a directory on your filespace for each user and store their css/js/image files there.
The reason for this is, that when you like to output your images to the browser, you will need to establish an own db connection for each file (since each is an own HTTP request to a PHP file, selecting the image).
You might want to take a look at http://mysqldump.azundris.com/archives/36-Serving-Images-From-A-Database.html and http://hashmysql.org/index.php?title=Storing_files_in_the_database before doing that. Storing files in mysql is generally considered a bad idea.
Just use different CSS rules for each user. Create the CSS dynamically though PHP based on user-specific variables. For example, if they have a div with an avatar or some other personal image, just create a class that uses variables for images, and then you really only need one or two files at most to do the whole thing. I would use a heredoc, but you could just use quotation marks to integrate the PHP.
php creates your css -
.useravatar{ 'background: url($baseurl.$urseridpic)'}
In the html, the div just needs the class of 'useravatar' never needing to be changed.