Jump to content

Getting two errors due to location of a script Rewritten for clarification


kenwvs

Recommended Posts

I have designed an html form that when you are finished entering about 16 fields you hit enter and it takes you to a confirmation page.  You can repeat this process for as many items as you have.  When the item is saved on the server, it saves it in csv format, using a filename based on an id (example test) that the person completing the form specifies.  When all the items have been entered, and the person entering the form is done, they have created a file that is now called test.csv  This is where I am running into a problem.  When they hit a button on the last form, it will automatically prompt them to save this file on their local computer, and this feature works.  I also have a script written so that it will email this file to them as an attachment, based on them entering in the same user id and their email address.  If I remove the script that prompts them to save it, it will generate an email, but I cannot get it to generate both the save and the email.  I am thinking it has something to do with where I have the script for the save command located in this php file.  It might have to do with the header, but not sure as I am new to all of this.

This first little script is what prompts them to save it to their computer.

<?php
$filename = "UploadForm/" . $_POST['id'] . ".csv";
$handle = fopen("$filename","r") or die("can't create file");
$content = fread($handle, filesize($filename));
fclose($handle);
header("Location:/$filename")

This is the complete script, including the above portion, showing how I currently have it set up.  I have also moved the above script to the very top, with no difference.  It will not generate the email if the above script is working.  If I take it out, the email will work.

<?php
if(!isset($_REQUEST['submit'])){
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Upload Form Results</title>
</head>
<p align="center"><font color="#FF0000"><b>Request CSV File Results</b></font></p>
You are just a step or two away from having your multiple upload file ready to be uploaded.

Once you have received this file, you will need to follow the steps below to complete the listing of your items.

<form action="uploadsave.php" method="post"
enctype="multipart/form-data">
<table>
<tr>
<td>Email Address:</td>
<td><input type="text" name="to" size="40"/> Please check that this email address is correct as your file will be sent to this address.</td>
</tr>
<tr>
<td></td>
<td><input type="hidden" name="from" value="For Sale 4 U" size="40" /></td>
</tr>
<tr>
<td></td>
<td><input type="hidden" name="re" value="Upload Form Results" size="40" /></td>
</tr>
<tr>
<td></td>
<td><input type="hidden" name="comments" value="Attached to this message is the file you just created to list your multiple items.  Please follow the instructions below to complete the listing of your items.

1.  Go to www.forsale4u.ca and log in using your regular username and password.
2.  On the left side of the screen, select Post Multiple Items.
3.  Browse for the file you have just received from this screen and select it.
4.  Select Insert
5.  On the left side of the screen, select My Uploaded Items and review them individually for accuracy.  Make any required changes.
6.  Select the items you wish to list, and press Post Items.
7.  Watch them Sell!! " size="300" /></td>
</tr>
<tr>
<td>User ID</td>
<td><input type="text" name="id" size="40" /> Please insert the same User ID that you used when entering your items.</td>
</tr>
<td colspan="2"><input type="submit" name="submit" value="Send Form" /></td>
</tr>
</table>
</form>
<p align="center">&nbsp;<b> <font color="#FF0000">Please be sure to save your
file as they are deleted from our server on a weekly basis.</font></b></p><hr>
<p align="center">&nbsp;</p>
<p align="center">If an error occurs and you do not receive your file,
please send an <b>
<a href="mailto:uploadfile@forsale4u.ca?subject=Missing Upload File">
<font color="#FF0000">EMAIL</font></a><font color="#FF0000"> </font></b>with
your User ID included in the body of the message and we will manually send a
copy of the file to you as soon as possible.</p>
<?php
}else{
extract($_POST);
$filename = "UploadForm/" . $_POST['id'] . ".csv";
$handle = fopen("$filename","r") or die("can't create file");
$content = fread($handle, filesize($filename));

        /*
Encode The Data For Transition using base64_encode();
And get a 32-character hexadecimal number
*/
        $content = chunk_split(base64_encode($content));
        $num = md5( time() );
        /*
Define the main message headers
*/
        $hdr  = "From:".$_REQUEST['from']."\r\n";
        $hdr .= "MIME-Version: 1.0\r\n";
        $hdr .= "Content-Type: multipart/mixed; ";
        $hdr .= "boundary=".$num."\r\n";
        $hdr .= "--$num\r\n";
/*
Define message section
*/
        $hdr .= "Content-Type: text/plain\r\n";
        $hdr .= "Content-Transfer-Encoding: 8bit\r\n\n";
        $hdr .= "".$_REQUEST['comments']."\r\n";
        $hdr .= "--".$num."\n";
        /*
Define the attachment section
*/
        //$hdr .= "Content-Type:". $filename_type." ";
        //$hdr .= "name=\"".$filename_name."\"r\n";
        $hdr .= "Content-Transfer-Encoding: base64\r\n";
        $hdr .= "Content-Disposition: attachment; ";
        $hdr .= "filename=\"".$filename."\"\r\n\n";
        $hdr .= "".$content."\r\n";
        $hdr .= "--".$num."--";
        /*
Send the email
*/
        mail( $_REQUEST['to'], $_REQUEST['re'], $_REQUEST['comments'], $hdr);
        /*
Close the attachment
*/
fclose($handle);
echo "Mail sent...";
}
?>
<?php
$filename = "UploadForm/" . $_POST['id'] . ".csv";
$handle = fopen("$filename","r") or die("can't create file");
$content = fread($handle, filesize($filename));
fclose($handle);
header("Location:/$filename")
?>

Thanks in advance, and I would appreciate an explanation of possible on why it makes a difference where a line is located, so I can learn and understand.

Thanks,

Ken
Link to comment
Share on other sites

It looks to me like this line:
$filename = "UploadForm/" . $_POST['id'] . ".csv";

is your problem. It seems $_POST['id'] contains nothing so your filename becomes Uploadform/.csv which does not exist so you cant open the file, and therefore cant close it for the 2nd, whilst the header error is probably due to the other errors.

try echoing $_POST['id'] just before and see what it contains.
Link to comment
Share on other sites

[quote]Insert Quote
It looks to me like this line:
$filename = "UploadForm/" . $_POST['id'] . ".csv";

is your problem. It seems $_POST['id'] contains nothing so your filename becomes Uploadform/.csv which does not exist so you cant open the file, and therefore cant close it for the 2nd, whilst the header error is probably due to the other errors.

try echoing $_POST['id'] just before and see what it contains.[/quote]

When the script runs, it gets this filename from a command, and does obtain it successfully.  I can get it to prompt me to save the file, or if I remove the "save script", it will email it to me, but I can't get it to do both, and I am wondering if it has to do with the headers, but I am really having troubles understanding what headers do, and I know it says they have to be placed before something (slips my mind what is was) and I don't understand if that means I need to list them at the top of the page, or if I have to make sure it runs before somethings, etc.

thanks for the help

Ken
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.