Log in

View Full Version : Mobile upload script request



davidjmorin
11-30-2007, 10:45 PM
Hey everyone. got a question thats maybe not so simple. how can i make my website accept photos that post onto a page that users upload from there cellphone?

example:
send pics to save@mywebsitealbum.com <this is the address users would send pics to>

view on www.mysite.com/myalbum.php <this is where the photos post to>

i know this is gonna require some database work. is there a tutorial out there anywhere that i can look at on how to do this. im not really even sure how to search this. any help would be great. thank you so much.

thetestingsite
11-30-2007, 10:54 PM
Just brainstorming here: you would probably have to set up a script to check the email address, then parse the emails to get the picture files. After parsing, save the files both to the server and to the database (just the url location would be fine). PHP could probably do this, but it would be very complex. Just off the top of my head, you may want to check php's IMAP functions to get an idea about the parsing of emails part.

Hope this helps.

djr33
11-30-2007, 11:01 PM
Easy, except for receiving the email. testingsite's info is accurate. trouble now is just figuring out how to get the attachment. IMAP is a good start, though you might need something more powerful.

Sending email with PHP is built in; receiving is much more complex.

Might be better to look into a different language, one integrated with the default mail server.

thetestingsite
11-30-2007, 11:07 PM
Just thinking out loud some more here, at work we have a program that takes emails sent to our IT Help Desk address and posts them in an SQL database, then it displays the request in our help desk program. I found out that the program is written in Java and comes with the webserver bundled in.

Hope this helps.

davidjmorin
11-30-2007, 11:10 PM
hmmm built in would be great hahaha. any idea what that is they use.

thetestingsite
11-30-2007, 11:13 PM
Well, the helpdesk is made by AdventNet and the product info is as follows:

http://manageengine.adventnet.com/products/service-desk/

Hope this helps.

davidjmorin
11-30-2007, 11:15 PM
thanks alot. big help to atleast get started. thanks

thetestingsite
11-30-2007, 11:27 PM
After doing a quick google search, this may help you out even further:

http://gallery.menalto.com/node/53819

Hope this helps.

davidjmorin
12-01-2007, 10:49 PM
so i found this program called mobilog. does anyone have any experience with this? also i cannot seem to get it to work. does anyone have a working version of this. looks simple to set up .

davidjmorin
12-05-2007, 06:10 PM
I am willing to pay for it
can somebody creat a script for me that i can just paste into my site to allow the following:

i have email mobile@mysite.com
i have page mysite.com/barname/mobilegallery

what i want is to have it so a person can send mms to mobile@mysite.com and have it post on the site.

send me email davidjmorin at gmail dot com with price and what you can do

thanks alot

BLiZZaRD
12-05-2007, 07:22 PM
Well, it certainly seems possible. Direct sms to web (me thinks) is not going to work. However, you could have them sms an email address, then set a cron job to use procmail and php to open inbox, find new messages and post subject/body/content to a php page.

thetestingsite
12-05-2007, 07:24 PM
Send an email with a photo attachment (fairly small for a demo) to demo[at]thetestingsite.net. Then, go to http://www.thetestingsite.net/test/ and it should redirect you to the "demo" folder with your attachment uploaded to the server. Let me know what you think and I'll modify it to suit your exact needs.

Hope this helps.

davidjmorin
12-06-2007, 04:11 PM
Wow i am so happy right now. so that site is great. and works awesome. thank you so much. you did what i want. now the one question i have is how you did it and how to have it post it onto a page instead of the directory look. This is a major step for me i can not thank you enough.

davidjmorin
12-06-2007, 04:16 PM
its nice to have people that help other ppl out there. especially with somebody new like me

thetestingsite
12-06-2007, 04:34 PM
Well, if you can tell me exactly what you want it to do, I can make the basic scripting for it. If you just want the base script, go to http://thetestingsite.net/test/index.phps and you can see the code that I used. The configuration for the email settings are at the top of the page. As for posting to a page, that would require a little more work. You would need a script to either read the directory and make thumbnail images of the photos uploaded, or add the links in a database then read that. Let me know if you need any more help with this and I'll see what I can do.

Hope this helps.

davidjmorin
12-06-2007, 04:41 PM
i actually think i can figure that stuff out. again thank you so much what a great help you have been. Thank you.

davidjmorin
12-06-2007, 06:26 PM
did you pull this script from somewhere?

thetestingsite
12-06-2007, 06:30 PM
I used some snippets from one of the websites I posted, but made some heavy modifications to get it to work the way that you were requesting.

davidjmorin
12-06-2007, 06:31 PM
what id like is to actually be able to have a script that does exactly what you did and then one that generates the file onto the page www.site.com/mobile and have them generate into a 4 colomn table. id be willing to pay. i think it sounds like alot but might not be. i know how to get the thumbs to generate just cant figure out the whole other stuff.

thetestingsite
12-06-2007, 06:38 PM
That shouldn't be that hard to do, but would take a little time to write the code and whatnot. I could probably work on it this weekend unless I can get the time today or tomorrow to work on it a little at a time. Let me see what I can come up with and I'll get back to you when I am done.

Hope this helps.

davidjmorin
12-06-2007, 06:38 PM
and would i be better off to use a database for this or just keep it simple with a php file and folder?

thetestingsite
12-06-2007, 06:44 PM
Either way; however, I believe that a database would make it run a little bit faster as it wouldn't have to read the directory every time the script is accessed. Then again; if you go the route of the db, if you delete an image from the directory and forget to delete that entry from the db, then there will be missing images in the gallery.

Hope this helps.

davidjmorin
12-06-2007, 07:06 PM
that would be great. thank you so much

davidjmorin
12-07-2007, 01:30 AM
here is my mail settings because i cannot get this to work so im not sure what im doing wrong

Mail Server Username: mobile+portlandnightlifeonline.com
Incoming Mail Server: mail.portlandnightlifeonline.com
Incoming Mail Server: (SSL) mail.portlandnightlifeonline.com
Outgoing Mail Server: mail.portlandnightlifeonline.com (server requires authentication) port 26
Outgoing Mail Server: (SSL) mail.portlandnightlifeonline.com (server requires authentication) port 26
Supported Incoming Mail Protocols: POP3, POP3S (SSL/TLS), IMAP, IMAPS (SSL/TLS)
Supported Outgoing Mail Protocols: SMTP, SMTPS (SSL/TLS)


and here is my file tree of where my email is in my dir
/mail/portlandnightlifeonline.com/mobile


and here is my index.phps file



<?php
$cfg = array();
######## CONFIG #######
$cfg['upload_dir'] = 'demo'; //what directory to upload images to $cfg['mail_server'] = 'mail.portlandnightlifeonline.com'; //email server $cfg['mail_port'] = '26'; //email server port

/* email server services as described on http://php.net/imap_open */ $cfg['mail_services'] = '/mail/portlandnightlifeonline.com/mobile';

$cfg['mail_username'] = 'username'; //username for the email address to check $cfg['mail_password'] = 'password'; //password for above user $cfg['debug'] = false; //true or false for debugging

/* Accepted File Types: if less than four characters, use a dot as the first char. */ $cfg['accepted'] = array('.3g2','.png','.jpg','.jpe','jpeg','.gif');
######## END CONFIG #########

$pid=2;

$type = array('text', 'multipart', 'message', 'application', 'audio', 'image', 'video', 'other'); $encoding = array('7bit', '8bit', 'binary', 'base64', 'quoted-printable', 'other');

if(!is_dir($cfg['upload_dir'])) {
@mkdir($cfg['upload_dir']) or die('Cannot create directory:
'.$cfg['upload_dir'].'! Make sure the parent folder has write permissions'); }

// open POP connection
$inbox = @imap_open
('{'.$cfg['mail_server'].':'.$cfg['mail_port'].$cfg['mail_services'].'}',
$cfg['mail_username'], $cfg['mail_password']) or die('Connection to server failed.');

// parse message body
function parse($structure) {
global $type;
global $encoding;

// create an array to hold message sections $ret = array();

// split structure into parts
$parts = $structure->parts;

for($x=0; $x<sizeof($parts); $x++) {
$ret[$x]['pid'] = ($x+1);
$that = $parts[$x];

// default to text
if ($that->type == '') {
$that->type = 0;
}
$ret[$x]['type'] = $type[$that->type] . '/' . strtolower($that->subtype);

// default to 7bit
if ($that->encoding == '') {
$that->encoding = 0;
}

$ret[$x]['encoding'] = $encoding[$that->encoding];
$ret[$x]['size'] = strtolower($that->bytes);
$ret[$x]['disposition'] = strtolower($that->disposition);

if ($that->ifparameters == 1) {
$params = $that->parameters;

foreach ($params as $p) {
if($p->attribute == 'NAME') {
$ret[$x]['name'] = $p->value;
break;
}
}
}
}
return $ret;
}

function get_attachments($arr) {
for($x=0; $x < sizeof($arr); $x++) {
if($arr[$x]['disposition'] == 'attachment') {
$ret[] = $arr[$x];
}
}
return $ret;
}

$count = @imap_num_msg($inbox) or die('No Messages in mailbox!'); //echo 'count: ' . $count;

// get message headers and structure
for ($c = 1; $c <= $count; $c++) {
$id = $c;
$headers = imap_header($inbox, $id);
$structure = imap_fetchstructure($inbox, $id);

// if multipart, parse
if(sizeof($structure->parts) > 1) {
$sections = parse($structure);
$attachments = get_attachments($sections); }

if ($cfg['debug']) {
echo 'Structure of message: ' . $id . '<BR><pre>'; print_r($structure); echo '</pre><BR>'; echo 'Sections of message: ' . $id . '<BR><pre>'; print_r($sections); echo '</pre><BR>'; }

// look for specified part
for($x=0; $x<sizeof($sections); $x++) {
if($sections[$x]['pid'] == $pid) {
$dtype = $sections[$x]['type'];
$dencoding = $sections[$x]['encoding'];
$filename = $sections[$x]['name'];
}
}

if ($cfg['debug']) {
echo ' type: ' . $dtype . '<BR>';
echo 'encoding: ' . $dencoding . '<BR>'; echo 'filename: ' . $filename . '<BR>'; echo ' id: ' . $id . '<BR><BR>'; }

$attachment = imap_fetchbody($inbox, $id, $pid);

if (!$cfg['debug']) {

//if (in_array(substr($filename, -4), $cfg['accepted'])) {
if ($dencoding == 'base64') {
// Decode and save attachment to a file
list($usec, $sec) = explode(' ', microtime());
$usec = substr($usec,2,3);
$name = date('Ymd.His');
$fp=fopen($cfg['upload_dir'].'/'.$name.'_'.$filename,'w');
fwrite($fp,imap_base64($attachment));
fclose($fp);
}
//}
}

}

if (!$cfg['debug']) {
// delete all emails
for ($i = 1; $i <= $count; $i++) {
imap_delete($inbox, $i);
}
imap_expunge($inbox);
}

imap_close($inbox);

if (!$cfg['debug']) {
header('Location: '.$cfg['upload_dir']); } ?>

thetestingsite
12-07-2007, 01:39 AM
On these lines, change to the highlighted values:



$cfg['mail_server'] = 'mail.portlandnightlifeonline.com'; //email server
$cfg['mail_port'] = '110'; //email server port

/* email server services as described on http://php.net/imap_open */
$cfg['mail_services'] = '/pop3';

$cfg['mail_username'] = 'YOUR_USERNAME@YOUR_DOMAIN.com'; //username for the email address to check
$cfg['mail_password'] = 'YOUR_PASSWORD'; //password for above user $cfg['debug'] = false; //true or false for debugging


Hope this helps.

davidjmorin
12-07-2007, 02:10 AM
i still get nothing. here is the link
ive sent a few.


does it matter that i use horde to check my mail?

here is the url for my mail


i am so lost right now. dont understand why it works for you but not for me

thetestingsite
12-07-2007, 02:36 AM
Ok, I played around with it from the user information you posted earlier (before editing to remove the info) ) and have come up with the following:



<?php
$cfg = array();
######## CONFIG #######
$cfg['upload_dir'] = 'demo'; //what directory to upload images to
$cfg['mail_server'] = 'mail.portlandnightlifeonline.com'; //email server
$cfg['mail_port'] = '110'; //email server port

/* email server services as described on http://php.net/imap_open */
$cfg['mail_services'] = '/pop3/notls';

$cfg['mail_username'] = 'username@portlandnightlifeonline.com'; //username for the email address to check
$cfg['mail_password'] = 'password'; //password for above user
$cfg['debug'] = false; //true or false for debugging

/* Accepted File Types: if less than four characters, use a dot as the first char. */
$cfg['accepted'] = array('.png','.jpg','.jpe','jpeg','.gif');
######## END CONFIG #########

$pid=2;

$type = array('text', 'multipart', 'message', 'application', 'audio', 'image', 'video', 'other');
$encoding = array('7bit', '8bit', 'binary', 'base64', 'quoted-printable', 'other');

if(!is_dir($cfg['upload_dir'])) {
@mkdir($cfg['upload_dir']) or die('Cannot create directory: '.$cfg['upload_dir'].'! Make sure the parent folder has write permissions');
}

// open POP connection
$inbox = @imap_open ('{'.$cfg['mail_server'].':'.$cfg['mail_port'].$cfg['mail_services'].'}', $cfg['mail_username'], $cfg['mail_password']) or die('Connection to server failed. Error: <BR>'.imap_last_error());

// parse message body
function parse($structure) {
global $type;
global $encoding;

// create an array to hold message sections
$ret = array();

// split structure into parts
$parts = $structure->parts;

for($x=0; $x<sizeof($parts); $x++) {
$ret[$x]['pid'] = ($x+1);
$that = $parts[$x];

// default to text
if ($that->type == '') {
$that->type = 0;
}
$ret[$x]['type'] = $type[$that->type] . '/' . strtolower($that->subtype);

// default to 7bit
if ($that->encoding == '') {
$that->encoding = 0;
}

$ret[$x]['encoding'] = $encoding[$that->encoding];
$ret[$x]['size'] = strtolower($that->bytes);
$ret[$x]['disposition'] = strtolower($that->disposition);

if ($that->ifparameters == 1) {
$params = $that->parameters;

foreach ($params as $p) {
if($p->attribute == 'NAME') {
$ret[$x]['name'] = $p->value;
break;
}
}
}
}
return $ret;
}

function get_attachments($arr) {
for($x=0; $x < sizeof($arr); $x++) {
if($arr[$x]['disposition'] == 'attachment') {
$ret[] = $arr[$x];
}
}
return $ret;
}

$count = @imap_num_msg($inbox) or die('No Messages in mailbox!');
//echo 'count: ' . $count;

// get message headers and structure
for ($c = 1; $c <= $count; $c++) {
$id = $c;
$headers = imap_header($inbox, $id);
$structure = imap_fetchstructure($inbox, $id);

// if multipart, parse
if(sizeof($structure->parts) > 1) {
$sections = parse($structure);
$attachments = get_attachments($sections);
}

if ($cfg['debug']) {
echo 'Structure of message: ' . $id . '<BR><pre>';
print_r($structure);
echo '</pre><BR>';
echo 'Sections of message: ' . $id . '<BR><pre>';
print_r($sections);
echo '</pre><BR>';
}

// look for specified part
for($x=0; $x<sizeof($sections); $x++) {
if($sections[$x]['pid'] == $pid) {
$dtype = $sections[$x]['type'];
$dencoding = $sections[$x]['encoding'];
$filename = $sections[$x]['name'];
}
}

if ($cfg['debug']) {
echo ' type: ' . $dtype . '<BR>';
echo 'encoding: ' . $dencoding . '<BR>';
echo 'filename: ' . $filename . '<BR>';
echo ' id: ' . $id . '<BR><BR>';
}

$attachment = imap_fetchbody($inbox, $id, $pid);

if (!$cfg['debug']) {

//if (in_array(substr($filename, -4), $cfg['accepted'])) {
if ($dencoding == 'base64') {
// Decode and save attachment to a file
list($usec, $sec) = explode(' ', microtime());
$usec = substr($usec,2,3);
$name = date('Ymd.His');
$fp=fopen($cfg['upload_dir'].'/'.$name.'_'.$filename,'w');
fwrite($fp,imap_base64($attachment));
fclose($fp);
}
//}
}

}

if (!$cfg['debug']) {
// delete all emails
for ($i = 1; $i <= $count; $i++) {
imap_delete($inbox, $i);
}
imap_expunge($inbox);
}

imap_close($inbox);

if (!$cfg['debug']) {
header('Location: '.$cfg['upload_dir']);
}
?>


Remember that you need to access the file test/index.php in order for it to work. Hope this helps.

davidjmorin
12-07-2007, 02:40 AM
got it to work i cannot believe it haha

finally. I thank you so much for all your help your the best.

need anything let me know maybe i can be of some help to you!!!!

thetestingsite
12-07-2007, 02:42 AM
Glad to hear it is working for you, let me know if you need anything else.

davidjmorin
12-07-2007, 02:49 AM
haha just one last questino.

can i run a cron job that will execute the index.php say every 1 min?

is that what a cron job does?

davidjmorin
12-07-2007, 02:51 AM
when i got to the url index.php i get the following error. it still works but why would i get this?

Warning: Cannot modify header information - headers already sent by (output started at /home/portlao9/public_html/test/index.php:1) in /home/portlao9/public_html/test/index.php on line 141

thetestingsite
12-07-2007, 03:38 AM
can i run a cron job that will execute the index.php say every 1 min?
is that what a cron job does?


You could and yes, although you may not want to run it every minute, instead every 10 minutes or so. if you were to run it every minute, it would get very server intensive and probably get your hosting account suspended (depending on your hosts TOS)



Warning: Cannot modify header information - headers already sent by (output started at /home/portlao9/public_html/test/index.php:1) in /home/portlao9/public_html/test/index.php on line 141


This usually means some sort of output has been sent to the browser before the redirect to the upload folder worked. Let me see if I can find where it is doing that and I'll let you know.

Hope this helps.

Did you add anything to the script that would send an output to the browser before the header redirect is executed? I can't find anything in my version that would cause it to error out.

djr33
12-07-2007, 05:17 AM
Any header data, including: header(), session_start(), and a few other functions, must be sent BEFORE ANY output of html, etc. Simple as that. You'll need to reorder your script and it'll work.

davidjmorin
12-07-2007, 12:03 PM
here is my full code



<?php
$cfg = array();
######## CONFIG #######
$cfg['upload_dir'] = 'demo'; //what directory to upload images to
$cfg['mail_server'] = 'mail.portlandnightlifeonline.com'; //email server
$cfg['mail_port'] = '110'; //email server port

/* email server services as described on http://php.net/imap_open */
$cfg['mail_services'] = '/pop3';

$cfg['mail_username'] = 'USERNAME@portlandnightlifeonline.com'; //username for the email address to check
$cfg['mail_password'] = 'PASSWORD'; //password for above user $cfg['debug'] = false; //true or false for debugging

/* Accepted File Types: if less than four characters, use a dot as the first char. */ $cfg['accepted'] = array('.3g2','.png','.jpg','.jpe','jpeg','.gif');
######## END CONFIG #########

$pid=2;

$type = array('text', 'multipart', 'message', 'application', 'audio', 'image', 'video', 'other'); $encoding = array('7bit', '8bit', 'binary', 'base64', 'quoted-printable', 'other');

if(!is_dir($cfg['upload_dir'])) {
@mkdir($cfg['upload_dir']) or die('Cannot create directory:
'.$cfg['upload_dir'].'! Make sure the parent folder has write permissions'); }

// open POP connection
$inbox = @imap_open
('{'.$cfg['mail_server'].':'.$cfg['mail_port'].$cfg['mail_services'].'}',
$cfg['mail_username'], $cfg['mail_password']) or die('Connection to server failed.');

// parse message body
function parse($structure) {
global $type;
global $encoding;

// create an array to hold message sections $ret = array();

// split structure into parts
$parts = $structure->parts;

for($x=0; $x<sizeof($parts); $x++) {
$ret[$x]['pid'] = ($x+1);
$that = $parts[$x];

// default to text
if ($that->type == '') {
$that->type = 0;
}
$ret[$x]['type'] = $type[$that->type] . '/' . strtolower($that->subtype);

// default to 7bit
if ($that->encoding == '') {
$that->encoding = 0;
}

$ret[$x]['encoding'] = $encoding[$that->encoding];
$ret[$x]['size'] = strtolower($that->bytes);
$ret[$x]['disposition'] = strtolower($that->disposition);

if ($that->ifparameters == 1) {
$params = $that->parameters;

foreach ($params as $p) {
if($p->attribute == 'NAME') {
$ret[$x]['name'] = $p->value;
break;
}
}
}
}
return $ret;
}

function get_attachments($arr) {
for($x=0; $x < sizeof($arr); $x++) {
if($arr[$x]['disposition'] == 'attachment') {
$ret[] = $arr[$x];
}
}
return $ret;
}

$count = @imap_num_msg($inbox) or die('No Messages in mailbox!'); //echo 'count: ' . $count;

// get message headers and structure
for ($c = 1; $c <= $count; $c++) {
$id = $c;
$headers = imap_header($inbox, $id);
$structure = imap_fetchstructure($inbox, $id);

// if multipart, parse
if(sizeof($structure->parts) > 1) {
$sections = parse($structure);
$attachments = get_attachments($sections); }

if ($cfg['debug']) {
echo 'Structure of message: ' . $id . '<BR><pre>'; print_r($structure); echo '</pre><BR>'; echo 'Sections of message: ' . $id . '<BR><pre>'; print_r($sections); echo '</pre><BR>'; }

// look for specified part
for($x=0; $x<sizeof($sections); $x++) {
if($sections[$x]['pid'] == $pid) {
$dtype = $sections[$x]['type'];
$dencoding = $sections[$x]['encoding'];
$filename = $sections[$x]['name'];
}
}

if ($cfg['debug']) {
echo ' type: ' . $dtype . '<BR>';
echo 'encoding: ' . $dencoding . '<BR>'; echo 'filename: ' . $filename . '<BR>'; echo ' id: ' . $id . '<BR><BR>'; }

$attachment = imap_fetchbody($inbox, $id, $pid);

if (!$cfg['debug']) {

//if (in_array(substr($filename, -4), $cfg['accepted'])) {
if ($dencoding == 'base64') {
// Decode and save attachment to a file
list($usec, $sec) = explode(' ', microtime());
$usec = substr($usec,2,3);
$name = date('Ymd.His');
$fp=fopen($cfg['upload_dir'].'/'.$name.'_'.$filename,'w');
fwrite($fp,imap_base64($attachment));
fclose($fp);
}
//}
}

}

if (!$cfg['debug']) {
// delete all emails
for ($i = 1; $i <= $count; $i++) {
imap_delete($inbox, $i);
}
imap_expunge($inbox);
}

imap_close($inbox);

if (!$cfg['debug']) {
header('Location: '.$cfg['upload_dir']); } ?>

thetestingsite
12-07-2007, 02:56 PM
Ok,
You have to make sure it stays in this format or else you will continue to get the error message. Currently, you have it set to where the variable $cfg['debug'] is in a comment (hence not being assigned); so everything that would be hidden if debug is set to false is being displayed.



<?php
$cfg = array();

######## CONFIG #######
$cfg['upload_dir'] = 'demo'; //what directory to upload images to

$cfg['mail_server'] = 'mail.portlandnightlifeonline.com'; //email server

$cfg['mail_port'] = '110'; //email server port

/* email server services as described on http://php.net/imap_open */
$cfg['mail_services'] = '/pop3';

$cfg['mail_username'] = 'USERNAME@portlandnightlifeonline.com'; //username for the email address to check

$cfg['mail_password'] = 'PASSWORD'; //password for above user

$cfg['debug'] = false; //true or false for debugging

/* Accepted File Types: if less than four characters, use a dot as the first char. */ $cfg['accepted'] = array('.3g2','.png','.jpg','.jpe','jpeg','.gif');
######## END CONFIG #########

$pid=2;

$type = array('text', 'multipart', 'message', 'application', 'audio', 'image', 'video', 'other'); $encoding = array('7bit', '8bit', 'binary', 'base64', 'quoted-printable', 'other');

if(!is_dir($cfg['upload_dir'])) {
@mkdir($cfg['upload_dir']) or die('Cannot create directory:
'.$cfg['upload_dir'].'! Make sure the parent folder has write permissions'); }

// open POP connection
$inbox = @imap_open
('{'.$cfg['mail_server'].':'.$cfg['mail_port'].$cfg['mail_services'].'}',
$cfg['mail_username'], $cfg['mail_password']) or die('Connection to server failed.');

// parse message body
function parse($structure) {
global $type;
global $encoding;

// create an array to hold message sections $ret = array();

// split structure into parts
$parts = $structure->parts;

for($x=0; $x<sizeof($parts); $x++) {
$ret[$x]['pid'] = ($x+1);
$that = $parts[$x];

// default to text
if ($that->type == '') {
$that->type = 0;
}
$ret[$x]['type'] = $type[$that->type] . '/' . strtolower($that->subtype);

// default to 7bit
if ($that->encoding == '') {
$that->encoding = 0;
}

$ret[$x]['encoding'] = $encoding[$that->encoding];
$ret[$x]['size'] = strtolower($that->bytes);
$ret[$x]['disposition'] = strtolower($that->disposition);

if ($that->ifparameters == 1) {
$params = $that->parameters;

foreach ($params as $p) {
if($p->attribute == 'NAME') {
$ret[$x]['name'] = $p->value;
break;
}
}
}
}
return $ret;
}

function get_attachments($arr) {
for($x=0; $x < sizeof($arr); $x++) {
if($arr[$x]['disposition'] == 'attachment') {
$ret[] = $arr[$x];
}
}
return $ret;
}

$count = @imap_num_msg($inbox) or die('No Messages in mailbox!');

// get message headers and structure
for ($c = 1; $c <= $count; $c++) {
$id = $c;
$headers = imap_header($inbox, $id);
$structure = imap_fetchstructure($inbox, $id);

// if multipart, parse
if(sizeof($structure->parts) > 1) {
$sections = parse($structure);
$attachments = get_attachments($sections);
}

if ($cfg['debug']) {
echo 'Structure of message: ' . $id . '<BR><pre>';
print_r($structure);
echo '</pre><BR>';
echo 'Sections of message: ' . $id . '<BR><pre>';
print_r($sections);
echo '</pre><BR>';
}

// look for specified part
for($x=0; $x<sizeof($sections); $x++) {
if($sections[$x]['pid'] == $pid) {
$dtype = $sections[$x]['type'];
$dencoding = $sections[$x]['encoding'];
$filename = $sections[$x]['name'];
}
}

if ($cfg['debug']) {
echo ' type: ' . $dtype . '<BR>';
echo 'encoding: ' . $dencoding . '<BR>';
echo 'filename: ' . $filename . '<BR>';
echo ' id: ' . $id . '<BR><BR>';
}

$attachment = imap_fetchbody($inbox, $id, $pid);

if (!$cfg['debug']) {

//if (in_array(substr($filename, -4), $cfg['accepted'])) {
if ($dencoding == 'base64') {
// Decode and save attachment to a file
list($usec, $sec) = explode(' ', microtime());
$usec = substr($usec,2,3);
$name = date('Ymd.His');
$fp=fopen($cfg['upload_dir'].'/'.$name.'_'.$filename,'w');
fwrite($fp,imap_base64($attachment));
fclose($fp);
}
//}
}

}

if (!$cfg['debug']) {

// delete all emails
for ($i = 1; $i <= $count; $i++) {
imap_delete($inbox, $i);
}
imap_expunge($inbox);
}
imap_close($inbox);

if (!$cfg['debug']) {
header('Location: '.$cfg['upload_dir']);
}
?>


Hope this helps.

davidjmorin
12-07-2007, 06:20 PM
great thanks testingsite. works like a charm. my next step is to figure out the table and thummbnail setup. thanks for all your help

davidjmorin
12-07-2007, 10:35 PM
ok last thing on my todo list this week LOL

i have www.mysite.com/gallery
inside that i have the following:
-gallery.html /shows the gallery of pics
-index.php /checks email for pics
-createthumb.php / creates thums of pics
-folder: images
-folder: thumbs

what i want is if someone goes to mysite.com/gallery it auto runs the createthumb.php script and the index.php script.
how do i go about doing this?



INDEX.PHP:




<?php
$cfg = array();
######## CONFIG #######
$cfg['upload_dir'] = 'images'; //what directory to upload images to
$cfg['mail_server'] = 'mail.portlandoldport.com'; //email server
$cfg['mail_port'] = '110'; //email server port

/* email server services as described on http://php.net/imap_open */
$cfg['mail_services'] = '/pop3';

$cfg['mail_username'] = 'USERNAME'; //username for the email address to check
$cfg['mail_password'] = 'PASS'; //password for above user $cfg['debug'] = false; //true or false for debugging

/* Accepted File Types: if less than four characters, use a dot as the first char. */
$cfg['accepted'] = array('.3g2','.png','.jpg','.jpe','jpeg','.gif');
######## END CONFIG #########

$pid=2;

$type = array('text', 'multipart', 'message', 'application', 'audio', 'image', 'video', 'other'); $encoding = array('7bit', '8bit', 'binary', 'base64', 'quoted-printable', 'other');

if(!is_dir($cfg['upload_dir'])) {
@mkdir($cfg['upload_dir']) or die('Cannot create directory:
'.$cfg['upload_dir'].'! Make sure the parent folder has write permissions'); }

// open POP connection
$inbox = @imap_open
('{'.$cfg['mail_server'].':'.$cfg['mail_port'].$cfg['mail_services'].'}',
$cfg['mail_username'], $cfg['mail_password']) or die('Connection to server failed.');

// parse message body
function parse($structure) {
global $type;
global $encoding;

// create an array to hold message sections $ret = array();

// split structure into parts
$parts = $structure->parts;

for($x=0; $x<sizeof($parts); $x++) {
$ret[$x]['pid'] = ($x+1);
$that = $parts[$x];

// default to text
if ($that->type == '') {
$that->type = 0;
}
$ret[$x]['type'] = $type[$that->type] . '/' . strtolower($that->subtype);

// default to 7bit
if ($that->encoding == '') {
$that->encoding = 0;
}

$ret[$x]['encoding'] = $encoding[$that->encoding];
$ret[$x]['size'] = strtolower($that->bytes);
$ret[$x]['disposition'] = strtolower($that->disposition);

if ($that->ifparameters == 1) {
$params = $that->parameters;

foreach ($params as $p) {
if($p->attribute == 'NAME') {
$ret[$x]['name'] = $p->value;
break;
}
}
}
}
return $ret;
}

function get_attachments($arr) {
for($x=0; $x < sizeof($arr); $x++) {
if($arr[$x]['disposition'] == 'attachment') {
$ret[] = $arr[$x];
}
}
return $ret;
}

$count = @imap_num_msg($inbox) or die('No Messages in mailbox!'); //echo 'count: ' . $count;

// get message headers and structure
for ($c = 1; $c <= $count; $c++) {
$id = $c;
$headers = imap_header($inbox, $id);
$structure = imap_fetchstructure($inbox, $id);

// if multipart, parse
if(sizeof($structure->parts) > 1) {
$sections = parse($structure);
$attachments = get_attachments($sections); }

if ($cfg['debug']) {
echo 'Structure of message: ' . $id . '<BR><pre>'; print_r($structure); echo '</pre><BR>'; echo 'Sections of message: ' . $id . '<BR><pre>'; print_r($sections); echo '</pre><BR>'; }

// look for specified part
for($x=0; $x<sizeof($sections); $x++) {
if($sections[$x]['pid'] == $pid) {
$dtype = $sections[$x]['type'];
$dencoding = $sections[$x]['encoding'];
$filename = $sections[$x]['name'];
}
}

if ($cfg['debug']) {
echo ' type: ' . $dtype . '<BR>';
echo 'encoding: ' . $dencoding . '<BR>'; echo 'filename: ' . $filename . '<BR>'; echo ' id: ' . $id . '<BR><BR>'; }

$attachment = imap_fetchbody($inbox, $id, $pid);

if (!$cfg['debug']) {

//if (in_array(substr($filename, -4), $cfg['accepted'])) {
if ($dencoding == 'base64') {
// Decode and save attachment to a file
list($usec, $sec) = explode(' ', microtime());
$usec = substr($usec,2,3);
$name = date('Ymd.His');
$fp=fopen($cfg['upload_dir'].'/'.$name.'_'.$filename,'w');
fwrite($fp,imap_base64($attachment));
fclose($fp);
}
//}
}

}

if (!$cfg['debug']) {
// delete all emails
for ($i = 1; $i <= $count; $i++) {
imap_delete($inbox, $i);
}
imap_expunge($inbox);
}

imap_close($inbox);

if (!$cfg['debug']) {
header('Location: '.$cfg['upload_dir']); } ?>


CREATTHUMB.PHP CODE:



<?php


function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
// open the directory
$dir = opendir( $pathToImages );

// loop through it, looking for any/all JPG files:
while (false !== ($fname = readdir( $dir ))) {
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'jpg' )
{
echo "Creating thumbnail for {$fname} <br />";

// load image and get image size
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );

// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );

// create a new tempopary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );

// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
}
}
// close the directory
closedir( $dir );
}

function createGallery( $pathToImages, $pathToThumbs )
{
echo "Creating gallery.html <br />";

$output = "<html>";
$output .= "<head><title>Thumbnails</title></head>";
$output .= "<body>";
$output .= "<table cellspacing=\"0\" cellpadding=\"2\" width=\"500\">";
$output .= "<tr>";

// open the directory
$dir = opendir( $pathToThumbs );

$counter = 0;
// loop through the directory
while (false !== ($fname = readdir($dir)))
{
// strip the . and .. entries out
if ($fname != '.' && $fname != '..')
{
$output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname}\">";
$output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"0\" />";
$output .= "</a></td>";

$counter += 1;
if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }
}
}
// close the directory
closedir( $dir );

$output .= "</tr>";
$output .= "</table>";
$output .= "</body>";
$output .= "</html>";

// open the file
$fhandle = fopen( "gallery.html", "w" );
// write the contents of the $output variable to the file
fwrite( $fhandle, $output );
// close the file
fclose( $fhandle );
}

// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
createThumbs("images/","thumbs/",100);
// call createGallery function and pass to it as parameters the path
// to the directory that contains images and the path to the directory
// in which thumbnails will be placed. We are assuming that
// the path will be a relative path working
// both in the filesystem, and through the web for links
createGallery("images/","thumbs/");
?>


GALLERY.HTML:


<html><head><title>Thumbnails</title></head><body><table cellspacing="0" cellpadding="2" width="500"><tr><td valign="middle" align="center"><a href="images/dsc01376.jpg"><img src="thumbs/dsc01376.jpg" border="0" /></a></td><td valign="middle" align="center"><a href="images/ethan christmas 3.jpg"><img src="thumbs/ethan christmas 3.jpg" border="0" /></a></td><td valign="middle" align="center"><a href="images/20071207.153126_ethans smiling santa pic people.jpg"><img src="thumbs/20071207.153126_ethans smiling santa pic people.jpg" border="0" /></a></td><td valign="middle" align="center"><a href="images/1111071014.jpg"><img src="thumbs/1111071014.jpg" border="0" /></a></td></tr><tr><td valign="middle" align="center"><a href="images/dsc01372.jpg"><img src="thumbs/dsc01372.jpg" border="0" /></a></td><td valign="middle" align="center"><a href="images/1111071051.jpg"><img src="thumbs/1111071051.jpg" border="0" /></a></td><td valign="middle" align="center"><a href="images/1111071057.jpg"><img src="thumbs/1111071057.jpg" border="0" /></a></td><td valign="middle" align="center"><a href="images/scottande.jpg"><img src="thumbs/scottande.jpg" border="0" /></a></td></tr><tr></tr></table></body></html>

tooth
12-28-2007, 10:24 PM
The script works great as far sending email with attachment, then sending the file to a speified folder...where I am having an issue is when the script renames the file in is not adding on the file extention.

ie, my file name is 173934.jpg >>> the script processes the file and changes the name to "20071228.150314_" without the file extention into specified folder. Where or how can I fix this...

Also having issue with other php accessing the folder to show all images on a page...the folder is set to 777

Thanks, any ideas or thoughts are helpful...
Tooth.