Jump to content

[SOLVED] PHP - After file upload, send user e-mail


Gamerz

Recommended Posts

Hello,

I currently have a file uploader website.

 

I want to be able to let the user's enter their own e-mail on upload.php (below) and when they click Send, there own file URL will be sent to there e-mail. NOT there file attachment, but the link..

 

Here is the upload form:

<form action="./upload.php" method="post" enctype="multipart/form-data">

      <label for="file">Select a file:</label> 
      <input type="file" name="userfile" id="file" size="20"><br><label for="file">   <p align="center">
<img src="loader.gif" name="loading" style="visibility:hidden;" width="220" height="19"><p align="center">
<input type="Submit" style="visibility:visible;" name="upload"  value="Upload File" onclick="this.style.visibility='hidden'; loading.style.visibility='visible'">  <p align="center">


</form>

 

 

 

 

Here is the actual process upload form to upload the actual file:

 

<?php
        $max_filesize = 10485760; // Maximum filesize in BYTES (currently 0.5MB).
       $download='download.php?file=';
      $upload_path = 'uploads/'; // The place the files will be uploaded to (currently a 'files' directory).
       $url = 'uploads/'; // The place the files will be uploaded to (currently a 'files' directory).
$url2 = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://';
$url2 .= $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
   $filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
   $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
$email = $_POST['email'];
$sendto = "$email";
$subject = "$name uploaded a file to your domain";
$message = "Name: $name\n\nEmail: $email\n\nCompany: $company\n\nFile name: $filename";
   // Now check the filesize, if it is too large then DIE and inform the user.
   if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
      die('<center><h1>The file you attempted to upload is too large.<br>We have a 10MB/Per file Limit</center></h1>');

   // Check if we can upload to the specified path, if not DIE and inform the user.
   if(!is_writable($upload_path))
      die('You cannot upload to the specified directory, please CHMOD it to 777.');

    // This restrics certain types of files from being uploaded.
     if ($_FILES['imagefile']['size'] > 100000000  )
{
die ("<center><h1>The file you attempted to upload is too large.</center></h1>");

}

$blacklist = array(".php",".htm",".aspx",".dll",".exe",".html",".css",".phtml", ".php3", ".php4", ".js", ".shtml", ".pl" ,".py");
foreach ($blacklist as $file)
{
if(preg_match("/$file\$/i", $_FILES['userfile']['name']))
{
echo "<center><h1>Error: The file you attempted to upload is restricted!</center></h1>\n";
exit;
}
}
   // We'll start handling the upload in the next step
  // Upload the file to your specified path.
   if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
  
         echo '<h3 align="center">Success! Your file has been uploaded.<strong><br>
mail($sendto, $subject, $message);

Your file/image upload of <em>'. $filename . '</em> 
was successful.</strong></h3>
	 </p>
<form action="upload22.php" method="post" enctype="multipart/form-data">
<p align="center"><strong>Your uploaded file/image is now live on our servers. 
        Scroll down below to obtain the URL to your file/image. Optionally, you can use one of the embed methods which have been generated for you.</strong></font></p>
	<p align="center">You can also send the uploaded file URL (' . $url2 . $url . $filename. ') to your e-mail by using the form below: </p>

<p align="center"><b>E-mail to send file URL:</b> <br> <input type="email" name="email" id="email" size="40"><p align="center"><label for="email">  <input type="submit" name="email" />
</form>
    <center> <table border="0" cellspacing="10" cellpadding=0">
	 <tr>
	 <td><center>If you just need the general link to your uploaded file/image, then 
         use this code below:</td>
	 </tr>
	 <tr>
         <td><strong><center>Direct viewing link to your 
         uploaded file/image:</font></strong><center>  <textarea cols="35" rows="3" onfocus="this.select()" onclick="this.select()">' . $url2 . $url . $filename . '</textarea><br><br>
         <strong>Download link of your uploaded file/image:
         </font></strong><br><textarea cols="35" rows="3" onfocus="this.select()" onclick="this.select()">' . $url2 . $url . $download . $filename . '</textarea></td>
         </tr>
	 </table>
	<center>  <table border="0" cellspacing="10" cellpadding="0" height="114">
	 <tr>
	 <td height="19">If you uploaded an image and need it embedding in a 
         forum, then use this code below</font>:</td>
	 </tr>
	 <tr>
	 <td height="75"><strong><center> Embedding the image on MySpace, Facebook, YouTube, 
         or even a forum:</font></strong><center> <textarea cols="35" rows="3" onfocus="this.select()" onclick="this.select()">[img=' . $url2 . $url . $filename . ']</textarea></td>
	 </tr>
	 </table>
	 <table border="0" cellspacing="10" cellpadding="0">
	 <tr>
	 <td><center> If you want a hyperlink for your file/image, then refer to 
         the below:</font></td>
	 </tr>
	 <tr>
	 <td>
	 <strong><center> Hotlink for Websites</font></strong>:</font><br/> <textarea cols="35" rows="3" onfocus="this.select()" onclick="this.select()"><a href="' . $url2 . $url . $filename . '"></a></textarea></td>
         </tr>
         </table>
         <table border="0" cellspacing="10" cellpadding="0" height="114">
	 <tr>
	 <td height="19">Share your uploaded file/image by referring below:</font></td>
	 </tr>
	 <tr>
	 <td height="75"><strong><center> Sharing your 
         uploaded file/image to 
         your friends</font>:</strong><br/> <center> <textarea cols="35" rows="3" onfocus="this.select()" onclick="this.select()">' . $url2 . $url . $filename . '</textarea></td>
	 </tr>
	 </table>
<br><br><center><a href="."><font size="5" color="#000080"><b>Click here to upload another file!<br>
         </b></font><font size="4"><br></font></a>

	 ';
       else
         echo '<center><h1>There was an error during the file upload.  Please try again.</font></center><br><br></h1>'; // It failed .
?>

 

 

As you can see on the second script, I have a field box to send the person e-mail...but the form action just leads to an incorrect page...I want to know how to actually send the link to the users email.

 

~Thanks...

first up you have two form elements called 'email'... and you need to pass the URL on to the email script, so you need a hidden element with the url

<input type="email" name="email" id="email" size="40"><p align="center"><label for="email">  <input type="submit" name="email" />

 

change to

<input type="email" name="email" id="email" size="40">

<p align="center"><input type="submit" name="submit" />

<input type="hidden" name="url" value="'. $url2 . $url . $filename . '" />

 

 

make the form action go to say

sendmail.php

which would look like

<?php
if (isset($_POST['email'], $_POST['url']) && !empty($_POST['email']) && !empty($_POST['url'])) {
$to = $_POST['email'];
$url = $_POST['url'];
$subject = 'File Upload URL';
$message = "Your URL is: $url";
$from = "[email protected]";
$headers = 'From: $from' . "\r\n" .
    'Reply-To: $from' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
}

//redirect user to whatever page
header("location: index.php");
exit();
?>

 

obviously change the message / subject / from etc to whatever you want... and also make sure you change the index.php in the header/redirect to whatever page you want them to be forwarded to

 

Ok, another thing...

 

When I tried your mailing script, and I send a test mail to myself, but when I open the mail, it says: "This message has been blocked for your safety. Open message" on my hotmail.

 

How do I get rid of that message? Because obviously, my message is safe, and I don't want people to think it's a virus or anything lol...anyways?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.