473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to attach files residing on the server

omerbutt
638 512MB
Hi i am trying to send email with attachments , i am using the same code at another application and the only difference is that one is getting the file from the user means the user submits and the other one is attaching the file already residing on the server and this one does not work , means the file size is shown 0KB in the mail and when i download and open it , it says

"Excel cannot open the file because the file format or the file extension is not valid. verify that the file has not been corrupted and that the file extension matches the format of the file"

here is the code i am using
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.     include("..\config.php");
  3.     if(!empty($_POST['file_idZ'])){
  4.         $file_idZ        =    $_POST['file_idZ'];
  5.         $files_array    =    explode("|",$file_idZ);
  6.  
  7.         $q="SELECT * from ".CALL_FILES." WHERE";
  8.  
  9.         for($r=0; $r<sizeof($files_array); $r++){
  10.             $q.=" file_id='".$files_array[$r]."' OR";
  11.         }
  12.         $limit=strlen($q)-3;
  13.         $q=substr($q,0,$limit);
  14.  
  15.         $r    =    execute($q);
  16.  
  17.         $count=0;
  18.         if(mysql_num_rows($r)){
  19.             while($f=mysql_fetch_array($r)){
  20.                 $userfile[$count]="../".DOCS.$f["file_name"];
  21.                 $count++;
  22.             }
  23.         }
  24.         // we'll begin by assigning the To address and message subject
  25.         $to        =    $_POST['send_to_email'];
  26.  
  27.         $subject="Call group files recieved";
  28.  
  29.         $from    =    $_SESSION['SESSION_user_email'];
  30.  
  31.         // get the sender's name and email address
  32.         // we'll just plug them a variable to be used later
  33.         $from = stripslashes($from);
  34.  
  35.         // generate a random string to be used as the boundary marker
  36.         $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
  37.  
  38.         // now we'll build the message headers
  39.         $headers = "From: $from\r\n" ."MIME-Version: 1.0\r\n" ."Content-Type: multipart/mixed;\r\n" ." boundary=\"{$mime_boundary}\"";
  40.         // here, we'll start the message body.
  41.         // this is the text that will be displayed
  42.         // in the e-mail
  43.         $message = "From: ".$from."\r\n";   
  44.  
  45.         // next, we'll build the invisible portion of the message body
  46.         // note that we insert two dashes in front of the MIME boundary
  47.         // when we use it
  48.         $message = "This is a multi-part message in MIME format.\n\n" ."--{$mime_boundary}\n" ."Content-Type: text/plain; charset=\"iso-8859-1\"\n" ."Content-Transfer-Encoding: 7bit\n\n" .
  49.         $message . "\n\n";
  50.  
  51.         // now we'll process our uploaded files
  52.         for($e=0; $e<sizeof($userfile); $e++){
  53.           // store the file information to variables for easier access
  54.           $tmp_name = $userfile[$e];
  55.           $name = basename($userfile[$e]);
  56.           $size = filesize($userfile[$e]);
  57.           $type="application/octet-stream ";
  58.           //}
  59.           // if the upload succeded, the file will exist
  60.           if (file_exists($tmp_name)){
  61.  
  62.              // check to make sure that it is an uploaded file and not a system file
  63.              if(is_uploaded_file($tmp_name)){
  64.  
  65.                 // open the file for a binary read
  66.                 $file = fopen($tmp_name,'rb');
  67.  
  68.                 // read the file content into a variable
  69.                 $data = fread($file,filesize($tmp_name)) or die("Cant read the file");
  70.  
  71.                 // close the file
  72.                 fclose($file);
  73.  
  74.                 // now we encode it and split it into acceptable length lines
  75.                 $data = chunk_split(base64_encode($data));
  76.              }
  77.  
  78.              // now we'll insert a boundary to indicate we're starting the attachment
  79.              // we have to specify the content type, file name, and disposition as
  80.              // an attachment, then add the file content.
  81.              // NOTE: we don't set another boundary to indicate that the end of the
  82.              // file has been reached here. we only want one boundary between each file
  83.              // we'll add the final one after the loop finishes.
  84.              $message .= "--{$mime_boundary}\n" ."Content-Type: {$type};\n" ." name=\"{$name}\"\n" ."Content-Disposition: attachment;\n" ." filename=\"{$fileatt_name}\"\n" ."Content-Transfer-Encoding: base64\n\n" .
  85.              $data . "\n\n";
  86.           }
  87.         }
  88.         // here's our closing mime boundary that indicates the last of the message
  89.         $message.="--{$mime_boundary}--\n";
  90.         // now we just send the message
  91.         if(mail($to, $subject, $message, $headers)){
  92.             echo "Mail Sent";
  93.         }
  94. }
  95. ?>
  96.  
Jan 27 '11 #1

✓ answered by omerbutt

i have done it it was the check of
Expand|Select|Wrap|Line Numbers
  1. if(is_uploaded_file()){
  2. }
  3.  
just commented it and it started working

1 1697
omerbutt
638 512MB
i have done it it was the check of
Expand|Select|Wrap|Line Numbers
  1. if(is_uploaded_file()){
  2. }
  3.  
just commented it and it started working
Jan 28 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: kumu | last post by:
How to Split a XML file to multiple small XML Files Hello, I am trying to split a XML file to multiple small xml files in vb.net and am trying to get the best possible approach to this. Any help...
2
by: Domenico Discepola | last post by:
Hello all. Before my arrival at my current employer, our consultants physically set up our MSSQL 7 server as follows: drive c: contains the mssql engine drive d: contains the transaction log...
0
by: Kristian Todd | last post by:
Hi I remember vaguely that there is an XML file for .NET that allows you to set the values of things like the page timout and the max size of files that can be uploaded to a server, does anybody...
0
by: Lev | last post by:
Hi all, Anybody know how to send email message with attached files via WebDAV for Exchange (MS Exchange 2003 Server). Please, please, please! Need help! ASAP!!!! Tnx, Lev
4
by: RedHair | last post by:
I'd like to set up a file system for the ASP.NET 2.0 application to store user-uploaded files, since the members are more than 100,000 people, the basic requirements are as below: (1) The file...
2
by: chetanspace | last post by:
when sizeof(classname) operator is use it shows size 1 even if no member is declared inside a class. Can you plz explain me why it is so? e.g. class test { }; void main() { cout <<...
4
by: bienwell | last post by:
Hi all, I developed an web page in ASP.NET to upload file into the server. In the Web.config file, I declared <httpRuntime executionTimeout="1200" maxRequestLength="400000" /> The MAX...
0
by: AAaron123 | last post by:
V2008 recent projects shows 2 iexplore If I click on either of them it shows iexplore.exe in the solutions window. Properties show the file to be C:\Program Files\Internet Explorer\iexplore.exe...
1
by: Man4ish | last post by:
I am trying to create a file of fixed size. I am using setw() e.g. 43 68 + 169161408 115 287 + 169161408 I am using setw() after first column ...
1
by: vinayreddy | last post by:
Please go through the file which i have attached to it and give me solution for the problem. password for the file is dbCommunity.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.