LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-30-2005, 09:40 PM   #1
benrose111488
Member
 
Registered: Jun 2004
Location: Wantage, NJ
Distribution: Fedora 7
Posts: 177

Rep: Reputation: 30
PHP file writing


hey everyone, I'm writing a PHP script that will take an attachment from an IMAP stream and put it on a server

I have the following code:

PHP Code:
$fileContent imap_fetchbody($mbox,$msgno,"2");
if (!
file_exists("/home/scavenger/scavengerxhunt.com/subimgdir/12345678.jpg"))
{
if(
copy($fileContent,"/home/scavenger/scavengerxhunt.com/subimgdir/12345678.jpg"))
{echo(
"SUCCESS!");}
else
{echo(
"FAILURE");}
}
echo(
"<img src=\"subimgdir/12345678.jpg\">"); 
and the function copy() doesn't seem to work here because $fileContent holds the actual image itself rather than a path to the file

can anyone suggest a better function or tell me how to make a variable hold the actual path to the file?

Thanks in advance =)
 
Old 07-01-2005, 12:36 AM   #2
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
There is no path to the attachment file, because the file contents were encoded within the email (which is likely stored as a single unit). You will need to write $fileContent to a file stream yourself, not use the copy() function.
 
Old 07-01-2005, 01:13 PM   #3
benrose111488
Member
 
Registered: Jun 2004
Location: Wantage, NJ
Distribution: Fedora 7
Posts: 177

Original Poster
Rep: Reputation: 30
how do I do that??

:-/

I also tried...

PHP Code:
$fileContent "imap_fetchbody($mbox,$msgno,\"2\")";
if (!
file_exists("/home/scavenger/scavengerxhunt.com/subimgdir/12345678.jpg"))
{
if(
copy($fileContent,"subimgdir/12345678.jpg"))
{echo(
"SUCCESS!");}
else
{echo(
"FAILURE");}

to no avail

how do I write to this stream, though??
 
Old 07-01-2005, 01:51 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
use fwrite()

PHP Code:
$fileContent imap_fetchbody($mbox,$msgno,'2');
$filename '/home/scavenger/scavengerxhunt.com/subimgdir/12345678.jpg';

if (
file_exists($filename)) {
    echo 
'FAILURE : ';
    echo 
"$filename already exists ...<br />";
    exit;
}

if (!
$fdfopen($filename'w')) {
    echo 
'FAILURE : ';
    echo 
"Cannot create file ($filename)";
    exit;
}
if (
fwrite($fd$fileContent) === FALSE) {
    echo 
'FAILURE : ';
    echo 
"Cannot write to file ($filename)";
    exit;
}
fclose($fd);
echo 
'SUCCESS!'
 
Old 07-01-2005, 03:18 PM   #5
benrose111488
Member
 
Registered: Jun 2004
Location: Wantage, NJ
Distribution: Fedora 7
Posts: 177

Original Poster
Rep: Reputation: 30
this code works in writing the file, however results in the image not being displayed because it contains errors

is there some sort of encoding I need to remove from the file before writing it to disk?

Thanks again
 
Old 07-01-2005, 03:34 PM   #6
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
My guess would be that $fileContent is still base64 encoded. Try writing imap_base64($fileContent) instead of just $fileContent. Check the notes on imap_fetchbody to see how to tell if a particular section is encoded or not.
 
Old 07-01-2005, 03:57 PM   #7
benrose111488
Member
 
Registered: Jun 2004
Location: Wantage, NJ
Distribution: Fedora 7
Posts: 177

Original Poster
Rep: Reputation: 30
Checked it out, and you were correct, 'twas base64 encoded

looks like the function works perfectly now

thanks muchly to all who contributed!! :-)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Ruby file writing NSKL Programming 0 10-30-2005 07:15 AM
writing to file ej25 Programming 17 11-19-2004 11:23 AM
writing files on to apache with php djgerbavore Programming 12 11-15-2004 02:01 PM
writing a strcutures to a file xemous Programming 2 09-03-2004 02:57 AM
File writing error xylum Linux - General 2 09-08-2002 01:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration