April 25th, 2007phpld Image Uploader MOD
With this modification you can allow users to upload a profile image that will be placed in their details page.
1. Create a folder called uploads in the same folder as submit.php (root of the directory)
2. chmod 777 uploads
3. create a file image_upload.php in the same folder as submit.php, with the content below:
<?php
$error=$_FILES["IMAGE"]["error"];
if ($error == UPLOAD_ERR_OK) {
$parsed = parse_url($_POST["URL"]);
$image_domain = $parsed['host'];
$image_type=$_FILES['IMAGE']['type'];
$image_loc="uploads/".$image_domain.".jpg";
$tmp_name = $_FILES["IMAGE"]["tmp_name"];
if ($image_type =="image/pjpeg" ||
$image_type =="image/jpeg" ||
$image_type=="image/gif" ||
$image_type=="image/png"){
move_uploaded_file($tmp_name, $image_loc);
}
}
?>
4. in submit.php,
AFTER: require_once 'init.php';
ADD: require_once "image_upload.php";
5. in submit.tpl
FIND :
<form action="" method="post">
REPLACE BY:
<form action="" method="post"
enctype="multipart/form-data">
FIND:
<td class="field">
{* Load category selection *}
{include file="category_select.tpl"}
</td>
</tr>
AFTER it, ADD:
<tr>
<td class="label">Image</td>
<td class="field">
<input type="file" name="IMAGE" />
</td>
</tr>
7. in detail.tpl, where you want the image to appear.
ADD:
{php}
$uploads_path="/path/to/uploads/";
$parsed = parse_url($this->_tpl_vars[URL]);
$image_domain = $parsed['host'];
$abs_image_loc = $uploads_path.$image_domain.".jpg";
$image_loc = "../uploads/".$image_domain.".jpg";
if (file_exists($abs_image_loc)) {
echo "<img src=\"" . $image_loc . "\" />";
}
{/php}
EDIT : $uploads_path=”/path/to/uploads/”;
in the code above to something to the absolute path
to your uploads folder something like
“/home/domain/public_html/uploads/”
That’s it.
The mod can be improved by adding small test on the image size and dimensions.
26 Responses to “phpld Image Uploader MOD”
Leave a Reply
You must be logged in to post a comment.


May 30th, 2007 at 7:56 am
Cant get it to work, I have tried different directories for the image, just to test. But it wont upload anything, it does not give an error message either.
May 30th, 2007 at 1:20 pm
Bolli, did you check the access permission of the folder you created? it should be 777
May 31st, 2007 at 2:16 am
Yep, it is set to 777.
/Bolli
May 31st, 2007 at 12:02 pm
did you set $uploads_path to the value it should be? the absolute path to the uploads folder.
May 31st, 2007 at 10:43 pm
AFter some diging I finaly did make it work. The problem was on the server side.
June 2nd, 2007 at 1:31 am
Can you guide how to force a specific size and file type (eg-jpg)
Also, can we automatically resize the image to 150px width?
June 3rd, 2007 at 2:53 pm
agnivo, for size and type you should use these two variables in the php file:
$_FILES['IMAGE']['size']
$_FILES['IMAGE']['type']
you can add a test to accept only specific attributes.
As for resizing I didn’t have a chance to look into it but shouldn’t be too hard to implement.
here is a sample code for this from http://www.blazonry.com/scripting/upload-size.php
Re-Sizing the Uploaded Image
To resize the uploaded image we use the pnmscale function which scales images in the PNM format. We use djpeg to convert the JPEG images to PNM, and cjpeg to convert them back. Here’s the code to convert, scale and write out a scaled JPG image.
/*== where storing tmp img file ==*/
$tmpimg = tempnam(”/tmp” “MKPH”);
$newfile = “$uploaddir/scaled.jpg”;
/*== CONVERT IMAGE TO PNM ==*/
if ($ext == “jpg”) { system(”djpeg $imgfile >$tmpimg”); }
else { echo(”Extension Unknown. Please only upload a JPEG image.”); exit(); }
/*== scale image using pnmscale and output using cjpeg ==*/
system(”pnmscale -xy 250 200 $tmpimg | cjpeg -smoo 10 -qual 50 >$newfile”);
—
I am not sure this is the best way to do it, just googled and found this.
June 8th, 2007 at 3:03 pm
Hi admin. is there a preview of this mod thats working?
thanks
June 9th, 2007 at 12:19 am
a demo can be found here: http://www.rakcha.com/phpld/Directories/enQuira-directory-18.html
June 18th, 2007 at 4:40 am
Hi rakcha.. i cant make this mod work for my directory. can you help me out? im not sure what my absolute path for my site. so i tried using the url of the site but still didnt work.. please advice.. i will wait for you at MSN..
June 18th, 2007 at 7:58 am
It work so far that images are uploaded, and named after the URL. But I cant view theimage from the link.tpl. The variable $parsed['host'] does not contain anything.
June 18th, 2007 at 8:50 am
yes bolli the images is uploaded in the folder.. my problem is i dont know what is the right absolute path at my site..
Kojak
June 18th, 2007 at 9:21 am
Sorry Kojak, I was asking admin about this.
The absolute path to ythe folden can be found via a php-file in the image dorectory that contain this:
June 18th, 2007 at 9:22 am
Hmm php-code was deleted from the comment:
echo getcwd();
June 18th, 2007 at 9:31 am
Bolli, it is the detail.tpl that should be edited.
Thanks for helping Kojak.
June 18th, 2007 at 9:48 am
Hi i know now my absolute path. i asked DP members http://forums.digitalpoint.com/showthread.php?t=369571
but still doesnt work.. am i missing something?
June 18th, 2007 at 9:50 am
The image is uploaded in the folder but the image is not appearing at the detail page…
June 18th, 2007 at 9:59 am
You probably got the same problem as me, the variable $parsed[’host’] does not contain anything. And I cant find where to load it with info.
June 18th, 2007 at 3:37 pm
Bolli it is finally working for me check this http://submitdirectory.org/detail/link-3564.html under the “website logo”
Make sure the absolute path is right. thats the last thing i did..
KOjak
June 19th, 2007 at 4:33 am
No, the path is correct, my problem is that the websiteURL i.e. the filename isnt inserted into the variable that holds that parameter.
June 19th, 2007 at 4:35 am
Admin -> I dont have a template-file called detail. the link.tpl is the file holding the actual listing.
June 20th, 2007 at 12:23 am
Bolli maybe youre using version 2.x thats why theres no detail.tpl and i think this tutorial is for version 3.x
Kojak
June 20th, 2007 at 3:56 am
Ah okiesw, yes I do, but everyting work as it should, exept for the part with listing the image. I dont know how to ad the image name to the variable.
June 21st, 2007 at 2:11 am
Admin –> Do you knkow how to get this variabe in 2.x?
July 10th, 2007 at 12:27 pm
I have this instead of wht you have said above wht to do now.
You said find : this but it dosent seems to be in my submit.tpl
Please let me know wht to do now
Thanks
Muneeb
August 7th, 2007 at 4:43 pm
Hi,
What if I would like the image to be displayed even within the listing in the categories page?
Thanks