How to Get Around Needing Write Permissions When Saving Images on Server
- 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 How to Get Around Needing Write Permissions When Saving Images on Server. 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.
The first way we got around the permissions curse was random luck. You may skip this section unless you wonder what luck has to do with anything, since this first method is to be avoided—period. We used standard file uploading methods, featuring the $_FILES[] functions and form uploads that use enctype="multipart/form-data". The result was lots of errors about permissions, but once in a while we were able to hit page refresh a bunch and the file would actually upload into the folder. But the PHP GD (graphics draw) and Image Functions did not work on this illegally uploaded image, especially the imagecopyresampled() function. Even the move_uploaded_file($_FILES[uploadfile][tmp_name],$path) function and getimagesize() function worked occasionally, after refeshes. Why? Don't know. But it's obvious that this method is pretty useless without 777 permissions, since it's terribly unreliable!
For those of you with write permissions, here are the 2 files that will let you upload and manipulate images.
Let's start with the upload form:
<html>
<head>
</head>
<body>
<BR><BR><BR><BR><BR><form enctype="multipart/form-data" action="optimize.php" method="POST">
Upload this JPG image: <INPUT NAME="uploadfile" TYPE="file">
<INPUT TYPE="submit" VALUE="Send File"></FORM>
</body>
</html>
And then there's the script:
<?php
header("Content-Type: image/jpeg");
if(isset($_FILES['uploadfile'])){
$succeed="true";
$size=$_FILES['uploadfile'][size];
$t=$_FILES[uploadfile][type];
$path="uploads/".$_FILES[uploadfile][name];
$n=basename($_FILES[uploadfile][name]);
list($width, $height) = getimagesize($path);
$percent_resizing = 570/$width;//570 is simply our preferred width
$new_width=$width;$new_height=$height;
if($width>570){
$new_width = round($percent_resizing*$width);
$new_height = round($percent_resizing*$height);}
if ($size>200000){$a=$a.$n." is more than 200KB so please reduce the file size and re-upload. Save quality should be at 70% and resize/resample width should be 570 pixels or less to reduce the file size.<BR>";
$succeed="false";}
if ($t<>"image/pjpeg"){$a=$a."Your uploaded file must end with .jpg and be a JPG image.<BR>";
$succeed="false";}
if($succeed=="true"){
if(move_uploaded_file($_FILES[uploadfile][tmp_name],$path)){
echo "Success!";
$tmp = imagecreatetruecolor($new_width,$new_height);
$img = imagecreatefromjpeg($path);
imagecopyresampled($tmp,$img,0,0,0,0,$tn_width, $tn_height,$width,$height);
imagejpeg($tmp,null,70);//might as well optimize to minimize file size
imagedestroy($img);imagedestroy($tmp);
}else{
echo "Upload failed. Inform us via our Contact page, please.";}
}else{echo $a;}
}
?>
The next two methods are perfect workarounds for needing write permissions that you simply do not have and cannot get.
Here are the first workaround's steps: get the image URL via a form; and use the PHP file_get_contents() function to put the image into a string; and the base64_encode() function to make it POST better; and, once POSTed to the db-stuffing script, the base64_decode() function is used to decode the encode; 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 use the imagecreatefromstring() function to turn the long string into an image; and the header('Content-Type: image/jpg') and imagejpeg() functions to make the image; and the <img src="show-pic.php"> method on an HTML page to display the image on a web page by running the script from an image tag.
The last method is similar to the previous one, except that it 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.
As trivial an HTML page as the following can display your MySQL-stored image in the browser (the width parameter shown 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="show-pic.php" alt="Hillary" width="100px">
</body>
</html>
However, the script you're likely to prefer is one where the centered thumbnail image, when hovered over with the mouse cursor, will enlarge greatly until you stop the hovering. We call it Image enlarges on mouseover but uses PHP script in all image tags.
And then there's a script that uses the PHP GD (graphics draw) and Image Functions like crazy, Building a picture from scratch and displaying it on a web page but with no write permissions. The results are in the image at the top of this page. Our PHP Page View Counter for Anywhere script uses similar techniques, but the Building a picture from scratch and displaying it on a web page but with no write permissions script uses TrueType fonts—arial.ttf is the file. We searched our computer and found plenty, and chose this Arial font due to its familiarity. Find all kinds of cool fonts on your own computer to use in these picture building scripts. And no write permissions are needed. Cool, huh? You may not be able to write nearly any type of file, but these workarounds should help a lot!
If you'd like to avoid putting images into MySQL, use the file upload scripts at the beginning of this page after you find a way to get write permission, or simply load MySQL with the image URL addresses, image names, lengths, and usernames—for each image—and avoid burdening the server. This solution is safest and often the wisest course.