|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Uploading with PHP
I created a form. Very simple - it emails the responses from the form submission to an email that I have specified in the response.php file.
I also have a text box and button that is supposed to allow users to upload a file and send it with their form submission. I wanted to know whether you guys could tell me how I could go about adding that function to my form? Your help would be extremely appreciated! Thanks in advance guys best regards, kh44na |
|
#2
|
|||
|
|||
|
Take a look at the "insert images into database" posting your answer is probably there
|
|
#3
|
|||
|
|||
|
<?php
if(!(copy($_FILES['userfile']['tmp_name'], "Upload/" . $_FILES['userfile']['name']))) die("Cannot upload files."); ?> I used the above code to create an upload script....it works fine - when I select a file to upload - it works fine...and I am directed to my thank you page.... HOWEVER...if I chose NOT to upload a file - I get an ugly php error saying file not uploaded.....(which makes sense) - but I want the users to be able to CHOSE whether to upload a file or not....and when they decide NOT TO UPLOAD A FILE....I want it to process the form normally and not give me that ugly php error. |
|
#4
|
|||
|
|||
|
How about:
if($_Files['userfile']){ if(!(copy($_FILES['userfile']['tmp_name'], "Upload/" . $_FILES['userfile']['name']))) die("Cannot upload files."); } |
|
#5
|
|||
|
|||
|
thanks for your quick reply - that idea does work - and when no file is selected or BROWSED, it DOES in fact successfully display my thank you page - but now when I DO upload a file (it doesn't copy into the UPLOAD directory)
|
|
#6
|
|||
|
|||
|
Here's what i'd try.
//Open or create the file for writing if($fp = fopen($filename, 'w')){ //Write to the file if ($x = fwrite($fp, $img)){ unset($x); } else{ $stats .= 'An image requested for this page could not be saved <br>'; } fclose($fp); } //This is part of a larger clase file. Wrap this in the if test I suggesred earlier Luck |
|
#7
|
|||
|
|||
|
that' gives me a variable error - ?
Any ideas?? basically the only problem that i'm having is that I want users to upload a file to a directory....but when they DON"T browse for anything, I want the thank you page to show up as normal. what code is required for that? |
|
#8
|
|||
|
|||
|
I'm going to paste my reponse page - to show you:
--------------------------------------------------------------------------- <?php $mailTo="ayyaz@dreamnation.ca"; $mailSubject="Blast Entertainment - Submit Article"; $mailBody=$mailBody."Full Name: " .$fullname. "\n"; $mailBody=$mailBody."Date of Report: " .$dateofreport."\n"; $mailBody=$mailBody."Time of Report: " .$report_time. "\n"; $mailBody=$mailBody."AM or PM: " .$amorpm. "\n"; $mailBody=$mailBody."Article Title: " .$articletitle. "\n"; $mailBody=$mailBody."Written by: " .$writtenby."\n"; $mailBody=$mailBody."Article: " .$articlepaste."\n"; $mailBody=$mailBody."Other Comments: " .$othercomments."\n"; mail($mailTo, $mailSubject, $mailBody, $mailHeaders); ?> <?php if(!$_FILES['userfile']['name']) die("Thank you"); if(!(copy($_FILES['userfile']['tmp_name'], "Upload/" . $_FILES['userfile']['name']))) die("Cannot upload files."); ?> <html> <head> <title> Response Page - Blast Entertainment </title> <head> <body bgcolor="#ffffff"> <font face="arial,helvetica,sans-serif" size="4" color="red"> <b> Article Submitted Successfully! </b> </font> <p><font size="2"" face="arial, helvetica, sans-serif">The article you submitted has been sent.</font><font face="arial, helvetica, sans-serif" size=2"><p> <a href=home.php"> Return to Main Page </a> </font> </body> </html> ------------------------------------------------------------------------------- Everything is fine - my thank you page is displayed if a file is uploaded. If a file isn't uploaded I receive a text phrase that says "Thank You" - as you will notice by looking at die("Thank you"); HOWEVER.....if a file isn't selected for upload.....how can I make my nice looking thank you page show up (as apposed to the Times New Roman text phrase that says "Thank You". maybe that makes a bit more sense? - and one of you guys can help me out....lol I REALLY APPRECIATE everything - thanks in advance. |
|
#9
|
|||
|
|||
|
Try it this way:-
//This tests wether the file has been uploaded or not. //Wrap the copy portion inside the exists test or it will return an error //Note the curly braces if(!$_FILES['userfile']['name']) { //If the file has been uploaded copy it if(!(copy($_FILES['userfile']['tmp_name'], "Upload/" . $_FILES['userfile']['name']))) die("Cannot upload files."); } die("Thank you"); /* If you don't get a file uploaded, try echo "$_FILES_['userfile']['name'] <br>"; inside the same curly braces, and before the copy */ |
|
#10
|
|||
|
|||
|
grrrr....
I don't have a clue why this doesn't seem to be working.... perhaps I'm not writing the code in correctly, or perhaps I'm not putting the code in the correct order...(I'm not a programmer at all - so you need to be really specific with me lol).... I REALLY appreciate your help....but this script is being very stubborn with me..... |
|
#11
|
|||
|
|||
|
Post your code, including your form.
Post your error messages If you swear to yourself a lot you are becoming a programmer. Where are you located? |
|
#12
|
|||
|
|||
I thought you would be fed up by now - but you're more helpful than I could have imagined dude - let me show you:MY HTML PAGE - OR FORM --------------------------------------------------------------------------------- <html> <head> <title>Blast Entertainment: Online Reporting Application - Movie in Theatres</title> </head> <body> <p><font color="#333333" size="5" face="Verdana, Arial, Helvetica, sans-serif">BLAST ENTERTAINMENT</font></p> <p><font color="#333333" size="5" face="Verdana, Arial, Helvetica, sans-serif">Submit Article</font></p> <p> </p> <form action="article_response.php" method="post" enctype="multipart/form-data"> <table width="587" border="0" align="center"> <tr> <td colspan="3"><font color="#006699" size="4" face="Verdana, Arial, Helvetica, sans-serif"><strong>USER INFORMATION:</strong></font></td> </tr> <tr> <td colspan="3"> </td> </tr> <tr> <td width="200"><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><br> Full Name:</font></strong></td> <td colspan="2"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><br> <input name="fullname" type="text" id="fullname"> </font></td> </tr> <tr> <td><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><br> Date of Submission:</font></strong></td> <td width="205"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"> <br> <strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif"> <input name="dateofreport" type="text" id="dateofreport"> </font></strong></font></td> <td width="168"><font color="#FF0000" size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong><br> E.G.) </strong>March 02, 2003</font></td> </tr> <tr> <td><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><br> Time:</font></strong></td> <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><br> <input name="report_time" type="text" id="report_time" size="5" maxlength="4"> <select name="amorpm" id="amorpm"> <option value="am">AM</option> <option value="pm">PM</option> </select> </font></td> <td><font color="#FF0000" size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong><br> E.G.) </strong>5:45</font></td> </tr> <tr> <td colspan="3"> </td> </tr> <tr> <td colspan="3"> </td> </tr> </table> <br> <br> <table width="587" border="0" align="center"> <tr> <td colspan="3"><font color="#006699" size="4" face="Verdana, Arial, Helvetica, sans-serif"><strong>MOVIE INFORMATION:</strong></font></td> </tr> <tr> <td colspan="3"> </td> </tr> <tr> <td width="198"><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><br> Article Title:</font></strong></td> <td colspan="2"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><br> <input name="articletitle" type="text" id="articletitle"> </font></td> </tr> <tr> <td><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><br> Written by:</font></strong></td> <td width="379"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"> <br> <strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif"> <input name="writtenby" type="text" id="writtenby"> </font></strong></font></td> </tr> <tr> <td colspan="3"> </td> </tr> </table> <br> <br> <table width="587" border="0" align="center"> <tr> <td colspan="3"><font color="#006699" size="4" face="Verdana, Arial, Helvetica, sans-serif"><strong>SUBMIT ARTICLE:</strong></font></td> </tr> <tr> <td colspan="3"><font color="#FF0000" size="2" face="Verdana, Arial, Helvetica, sans-serif">Please use the functions provided below to submit your article. You may paste the entire article in the text area provided below, or upload a word document if you have written your article using Microsoft Word.</font></td> </tr> <tr> <td width="230"><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><br> Article:</font></strong></td> <td colspan="2"><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><br> <textarea name="articlepaste" cols="50" rows="15" id="articlepaste"></textarea> </font></strong></td> </tr> <tr> <td><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><br> Browse for word document:</font></strong></td> <td colspan="2"><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><br> <input type="hidden" name="MAX_FILE_SIZE" value="1000000"> <input type="file" name="userfile"> </font></strong></td> </tr> <tr> <td><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><br> Any other comments:</font></strong></td> <td colspan="2"><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><br> <textarea name="othercomments" cols="40" rows="5" id="othercomments"></textarea> </font></strong></td> </tr> <tr> <td colspan="3"> </td> </tr> <tr> <td> </td> <td width="109"><input type="submit" value="Submit Article"></td> <td width="234"><input type="reset" name="Reset" value="Cancel"></td> </tr> <tr> <td colspan="3"> </td> </tr> </table> </form> </body> </html> ------------------------------------------------------------------------------------ MY RESPONSE PAGE - is provided above Perhaps that will help. I'm located in Toronto (Richmond Hill) - do you have icq? |
|
#13
|
|||
|
|||
|
Your form looks fine.
Try printing out everything you can. And send me any errors, or missing results. I'm in Nova Scotia, and I'll set up icq later |
|
#14
|
|||
|
|||
|
There is no problem....
<?php if(!$_FILES['userfile']['name']) die("Thank You"); if(!(copy($_FILES['userfile']['tmp_name'], "Upload/" . $_FILES['userfile']['name']))) die("Cannot upload files."); ?> that is the php code I'm using in my response page currently.....When I select a file for upload and hit submit, I get my html THANK YOU PAGE (nice looking page - with different fonts, and colours - etc). When I chose NOT to select a file for upload - I get an ugly times new roman text phrase that says Thank You - instead of that - if a user choses not to select something for upload - I want it to go and display my nice looking thank you page..... |
|
#15
|
|||
|
|||
|
Try This it worked on my machine
$imgname = $_FILES['userfile']['name']; if($imgname){ if(!(copy($_FILES['userfile']['tmp_name'], ], "Upload/" . $imgname))) { die("Cannot upload files."); } else{ echo "File Copied<br>"; } } die("Thank You"); |
|
#16
|
|||
|
|||
|