Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Mail

Strip filename of chars before upload

the mail() function

Moderators: macek, egami, gesf

Strip filename of chars before upload

Postby hdbrandon » Wed Jul 20, 2011 5:52 pm

Here is my problem guys: I have a custom photo gallery where users can upload photos, which are written to a database and displayed. The same upload form sends (based on a checkbox) the photo to an email address that forwards to facebooks email to wall feature.

The problem: When I get the emails, it includes the directory path ../gallery/X.jpg ....which is required for the gallery display, but facebook doesnt seem to accept it for security seasons I guess.

So, how can I strip the "../gallery/" out of the filename while still keeping the attachment. I've tried using string replace, which works and the filename is correct but the photo is 0k and not attached.

Code: Select all

<?php
include("../framework/config.php");

//get IP address of uploader
$ipAddress = getenv("REMOTE_ADDR");

//use timestamp+filename to ensure filename is unique when saved
$path_temp = date("Y-m-d_H-i",time()) . "_" . $HTTP_POST_FILES['ufile']['name'][0];

$path1= "../gallery/". $path_temp;


//copy file to store
copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);


echo "File Name :".$HTTP_POST_FILES['ufile']['name'][0]."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size'][0]."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type'][0]."<BR/>";
echo "<img src=\"$path1\" width=\"150\" height=\"150\">";

//strip html and slashes, etc
$altOne=htmlspecialchars(stripcslashes($_POST[altOne]), ENT_QUOTES);
$descOne=htmlspecialchars(stripcslashes($_POST[descOne]), ENT_QUOTES);


$bwOne="$_POST[bwOne]";
$facebookOne="$_POST[facebookOne]";
$date = date("Y-m-d_H-i",time());
$store="$_POST[store]";

echo"Alt: $altOne <br> Desc: $descOne <br> Best/Worst: $bwOne <br>Facebook: $facebookOne<br><br><br>";


//display the error or success.

$filesize1=$HTTP_POST_FILES['ufile']['size'][0];


if($filesize1 != 0){

echo "We have recieved your files";

   if($facebookOne == 'Y'){


      function mail_attachment($filename, $mailto, $from_mail, $from_name, $replyto, $subject) {

                $file = $filename;
                $file_size = filesize($file);
                $handle = fopen($file, "r");
                $content = fread($handle, $file_size);
                fclose($handle);
                $content = chunk_split(base64_encode($content));
                $uid = md5(uniqid(time()));
                $name = basename($file);
                $header = "From: ".$from_name." <".$from_mail.">\r\n";
                $header .= "Reply-To: ".$replyto."\r\n";
                $header .= "MIME-Version: 1.0\r\n";
                $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
                $header .= "This is a multi-part message in MIME format.\r\n";
                $header .= "--".$uid."\r\n";
                $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
                $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
                $header .= "--".$uid."\r\n";
                $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
                $header .= "Content-Transfer-Encoding: base64\r\n";

                $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";

                $header .= $content."\r\n\r\n";
                $header .= "--".$uid."--";
                  if (mail($mailto, $subject, $message, $header)) {
                    echo "<br/><br/>Mail Sent!"; // or use booleans here
                } else {
                    echo "<br/><br/>Mail FAILD!";
                }
            }

            $my_name = "My Name";
            $my_mail = "facebook@domain.com";
            $my_replyto = "facebook@domain.com";
            $my_subject = $_POST['descOne'];

            mail_attachment($path1_temp, "facebook@domain.com", $my_mail, $my_name, $my_replyto, $my_subject);


   }
   else{
   }
}


else {
echo "ERROR.....";
}

//////////////////////////////////////////////

// What files that have a problem? (if found)

if($filesize1==0) {
echo "There're something error in your first file";
echo "<BR />";
}

$sql="INSERT INTO $table(filename,userID,alt,date,IPaddress,facebook,BW,store,descText)values('$path_temp','userID','$altOne','$date','$ipAddress','$facebookOne','$bwOne','$store','$descOne');";
echo"Path1:  $path1
<br/><br/>
$sql

<br/><br/>

";

mysql_query($sql);

?>
hdbrandon
New php-forum User
New php-forum User
 
Posts: 1
Joined: Wed Jul 20, 2011 4:59 pm

Return to PHP coding => Mail

Who is online

Users browsing this forum: Bing [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron