Read Image Immediately into MySQL to Solve No Write Permissions Problem
- How to Get Around Needing Write Permissions When Saving Images on Server
- Make image into string to put in mysql to solve no write permissions problem
- Read image immediately into mysql to solve no write permissions problem
- Image enlarges on mouseover but uses PHP script in all image tags
- Building a picture from scratch and displaying it on a web page but with no write permissions

Building a picture from scratch and displaying it on a web page but with no write permissions
This page is a tutorial on Read Image Immediately into MySQL to Solve No Write Permissions Problem. Our overall method is to use MySQL and PHP. All over the Internet, the discussions of using PHP to upload image files to the server seem to have a common thread. "You have to have write permissions." This is sort of true and sort of not true. All of our experiments were done with CHMOD 750 on our public_html folder and CHMOD 755 on our test subfolder. Server warnings were all we got from trying to CHMOD from either a script or from our FTP program. So we had to live without write permissions (sigh . . . ). But we still got the job done 3 different ways!
Since none of the forums or expert sites (in our research) ever mentioned any alternatives to the write permissions method, we felt prompted into action—hence this tutorial.
For those of you with write permissions, How to Get Around Needing Write Permissions When Saving Images on Server contains listings of the 2 files that will let you upload and manipulate images.
The following method is a perfect workaround for needing write permissions that you simply do not have and cannot get.
The method uses the PHP file function that we are allowed to use even with no 777 permissions: READING (as opposed to WRITING). (If you're not even allowed file reading, change hosts or convince him to change directives.) We used fopen() to open an image as binary and we read the image in small chunks at a time with
fread(), and then we use the feof() function which tests for end-of-file on a file pointer, and the mysql_real_escape_string() function is used to get it safe to put into a MySQL table. Then we put it in the db table and utilize header('Content-Type: image/jpg') and echoing of the image data, and then we use the <img src="image-show.php?p1=4"> method on an HTML page to display the image on a web page by running the script from an image tag. Note the use of a URL query string so we can send data to GET in the script that will tell the script which image we want.
If you'd prefer file stringifying functions like file_get_contents() to file functions like fread(), feel free to use our other workaround: Make image into string to put in mysql to solve no write permissions problem.
As trivial an HTML page as the following can display your MySQL-stored image in the browser (a width parameter of 100 will turn a large image into a thumbnail, while leaving out parameters will let the image display at its original size):
<html>
<head>
</head>
<body>
<img src="image-show.php?p1=4" alt="image">
</body>
</html>
Anyway, the Read Image Immediately into MySQL to Solve No Write Permissions Problem script is below, and it is composed of 2 parts. Of course, to actually use the script on a web page, you need some version of the script above which merely runs the image-show.php script from a web page image tag, thereby displaying one or more image, and using a URL query string so the receiving script will know which image to get. The first script is called: image-store.php
<?php
include_once"config.php";
$sql="CREATE TABLE IF NOT EXISTS myimages (
id int NOT NULL AUTO_INCREMENT,
Image MEDIUMBLOB,
PRIMARY KEY (id)
) ENGINE=MyISAM AUTO_INCREMENT=1";
mysql_query($sql);
$f = fopen("http://mysite.com/woman.jpg", "rb");
if($f){
while(!feof($f)){
$img = $img . fread($f, 1024);}}
if($f != "none") {
$img = mysql_real_escape_string($img);
mysql_query("INSERT INTO myimages (id,Image) VALUES (NULL,'$img')") or die("Query failed.");
}else{
echo "Picture upload failed.";}
?>
In the script above, we use the config.php file to get connected up to the correct MySQL table since we are about to WRITE to it. WE are allowed to write to it since we are the 7 in the CHMOD 750 of our public_html folder. There wouldn't be much use of a host offering MySQL in their control panels if one could not use them! So now you see the trick. We avoid the need for write permissions by using the methods that ARE open to us, like fopen() and fread() and MySQL queries. This may not upload the image files to file folders, but it surely does upload their data to MySQL, which affords us the further opportunity of displaying the data as an image on a web page(s) of our choice. Think of those mean old hosts as merely protecting our files and folders from dastardly devils with hacking in mind, by denying the CHMOD 777. Surely we can appreciate that! So—use our workarounds.
Now we make the MySQL table, if we haven't already done so, using the mediumblob type for our data. It has a maximum of 16MB, although in practice we'd tend to disallow any image over 200K. If the users have a bigger one, introduce these users to Irfanview or ImageForge or other graphics apps that can easily optimize their images to a much smaller file size. How to Get Around Needing Write Permissions When Saving Images on Server has an upload feature (for those allowed 777) that uses imagejpeg($tmp,null,70) to optimize and reduce file size, as well as a type checker and size checker to ensure only JPEGs under 200K are uploaded, as well as an image dimension tweaker that brings the size to a 570-pixel-wide maximum. (You may prefer different parameters—if so, tweak away!) You may find it convenient to store the file length and name as well, in your db table, so you have more than the id number to reference the image with.
Note that the image data concatenation process in which we read the image data chunk by chunk—merging chunks as we go—uses fopen() to open the file, "rb" to read it binary style, feof() to check if the chunks we read into the $img variable finally reach the end of the file, and fread() in 1024-byte chunks to make sure too big a "byte" doesn't make the script choke. (Obviously you won't want an image URL to be hardcoded in like we did, but rather use a form and input process instead. Color us lazy.) Finally this image string data gets a taste of mysql_real_escape_string() to make it safer to put into MySQL. Then we INSERT the data into the database table and get ready for a call to action that grabs the data and makes it into an image again. But that script is below.
The next script is called image-show.php
<?php
include_once"config.php";
$id=intval($_GET['p1']);
if(!$id){die("That image is not valid.");}
$result = mysql_query("SELECT Image FROM myimages WHERE id='$id'") or die("Query failed."); ;
$row = mysql_fetch_row($result);
$data = $row[0];
if($data){
header('Content-type: image/jpeg');
echo $data;
}else{
echo "Picture display failed.";}
?>
In the script above, we use the config.php file to get connected up to the correct MySQL table. And we use a URL query string like <img src="image-show.php?p1=4" alt="Lady" width="100px"> on your web page and a GET in this image-show.php script that grabs the image id and uses that in the WHERE part of the SELECT phrase in the MySQL query, since GETs grab query strings from URLs without needing POSTs.
So we use the header('Content-Type: image/jpg') and the echo functions to make the image. But since the web page script below, or a tweaked one, like the one mentioned below, is where the actual image will display, we need to recall that echo $data creates the image but is fine about the fact that what it makes is shown elsewhere—think of it as a wee bit of notoriety. (A header('Content-Disposition: inline') line after the header('Content-Type: image/jpg') line is an optional function that would encourage the browser to display the image, not try to send it somewhere as an attachment. The main thing that went wrong in our experiments was the image elicited a Would You Like To Open or Save the file, or Cancel? But our workarounds seem to have precluded such browser impertinence. I.e., they work super for us. Your mileage may vary—probably due to different PHP directives or no GD support.)
Although you will like Image enlarges on mouseover but uses PHP script in all image tags better (since it uses the show-pic.php script but does more, although you will need to replace the show-pic.php references to image-show.php), as trivial an HTML page as the following can display your MySQL-stored image in the browser and use a URL query string so the receiving script will know which image to get. A width parameter of 100 will turn a large image into a thumbnail, while leaving out parameters will let the image display at its original size:
<html>
<head>
</head>
<body>
<img src="image-show.php?p1=4" alt="image">
</body>
</html>