spacepoet Posted March 17, 2011 Share Posted March 17, 2011 Hi again: What I want to do - and am stuck on - is to allow the user to add files to the feedback, submit the form, and the data will get stored in the DB (and the uploaded files will get stored in "uploads"), plus an email will get sent to the client who can click the files links in the email, and download them that way. I have done this via ASP, but never tried it with PHP ... This is what my database table looks like" CREATE TABLE `myProductRightData2` ( `myDate` varchar(55) default NULL, `myNameAndProduct` text, `myWebsite` text, `myProductUse` text, `myProductProblemSolver` text, `myProductUnique` text, `myWhyBetter` text, `myProductAppeal` text, `myProductSelling` text, `myProductResearch` text, `file1` varchar(255) default NULL, `myProductStory` text, `mySpokesperson` text, `myProductReviewed` text, `myProductDecision` text, `file2` varchar(255) default NULL, `file3` varchar(255) default NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8; -- -- Dumping data for table `myProductRightData2` -- INSERT INTO `myProductRightData2` VALUES('February 22, 2011', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test.pdf', 'test', 'test', 'test', 'test1.jpg', 'test2.jpg'); And this is the form code: <?php $error = NULL; $myDate = NULL; $myNameAndProduct = NULL; $myPhone = NULL; $myEmail = NULL; $myWebsite = NULL; $myProductUse = NULL; $myProductProblemSolver = NULL; $myProductUnique = NULL; $myWhyBetter = NULL; $myProductAppeal = NULL; $myProductSelling = NULL; $myProductResearch = NULL; $file1 = NULL; $myProductStory = NULL; $mySpokesperson = NULL; $myProductReviewed = NULL; $myProductDecision = NULL; $file2 = NULL; $file3 = NULL; if(isset($_POST['submit'])) { if ((($_FILES["file1"]["type"] == "image/gif") || ($_FILES["file1"]["type"] == "image/jpg") || ($_FILES["file1"]["type"] == "image/jpeg") || ($_FILES["file1"]["type"] == "image/pjpeg")) && ($_FILES["file1"]["size"] < 20000)) { if ($_FILES["file1"]["error"] > 0) { echo "Return Code: " . $_FILES["file1"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file1"]["name"] . "<br />"; echo "Type: " . $_FILES["file1"]["type"] . "<br />"; echo "Size: " . ($_FILES["file1"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file1"]["tmp_name"] . "<br />"; if (file_exists("upload/" . $_FILES["file1"]["name"])) { echo $_FILES["file1"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file1"]["tmp_name"], "upload/" . $_FILES["file1"]["name"]); echo "Stored in: " . "uploads/" . $_FILES["file1"]["name"]; } } } else { echo "Invalid file"; } $myDate = $_POST['myDate']; $myNameAndProduct = $_POST['myNameAndProduct']; $myPhone = $_POST['myPhone']; $myEmail = $_POST['myEmail']; $myWebsite = $_POST['myWebsite']; $myProductUse = $_POST['myProductUse']; $myProductProblemSolver = $_POST['myProductProblemSolver']; $myProductUnique = $_POST['myProductUnique']; $myWhyBetter = $_POST['myWhyBetter']; $myProductAppeal = $_POST['myProductAppeal']; $myProductSelling = $_POST['myProductSelling']; $myProductResearch = $_POST['myProductResearch']; $file1 = $_POST['file1']; $myProductStory = $_POST['myProductStory']; $mySpokesperson = $_POST['mySpokesperson']; $myProductReviewed = $_POST['myProductReviewed']; $myProductDecision = $_POST['myProductDecision']; $file2 = $_POST['file2']; $file3 = $_POST['file3']; if(empty($myNameAndProduct)) { $error .= '-- Enter your Name and Product. <br />'; } if(empty($myPhone)) { $error .= '-- Enter your Phone Number. <br />'; } if(empty($myEmail)) { $error .= '-- Enter your Email. <br />'; } if($error == NULL) { $sql = sprintf("INSERT INTO myProductRightData2(myDate,myNameAndProduct,myPhone,myEmail,myWebsite,myProductUse,myProductProblemSolver,myProductUnique,myWhyBetter,myProductAppeal,myProductSelling,myProductResearch,file1,myProductStory,mySpokesperson,myProductReviewed,myProductDecision,file2,file3) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')", mysql_real_escape_string($myDate), mysql_real_escape_string($myNameAndProduct), mysql_real_escape_string($myPhone), mysql_real_escape_string($myEmail), mysql_real_escape_string($myWebsite), mysql_real_escape_string($myProductUse), mysql_real_escape_string($myProductProblemSolver), mysql_real_escape_string($myProductUnique), mysql_real_escape_string($myWhyBetter), mysql_real_escape_string($myProductAppeal), mysql_real_escape_string($myProductSelling), mysql_real_escape_string($myProductResearch), mysql_real_escape_string($file1), mysql_real_escape_string($myProductStory), mysql_real_escape_string($mySpokesperson), mysql_real_escape_string($myProductReviewed), mysql_real_escape_string($myProductDecision), mysql_real_escape_string($file2), mysql_real_escape_string($file3)); if(mysql_query($sql)) { $error .= 'Thank you for submitting your product. We will be in contact with you soon.'; mail( "[email protected]", "Product Submission", "Date Sent: $myDate\n Name and Product: $myNameAndProduct\n Phone: $myPhone\n Email: $myEmail\n Website: $myWebsite\n Product Use: $myProductUse\n Problem Product Solves: $myProductProblemSolver\n Product Uniqueness: $myProductUnique\n Product Better Because: $myWhyBetter\n Product Appeal: $myProductAppeal\n Product Selling Places: $myProductSelling\n Product Research: $myProductResearch\n Research Documentation: $file1\n Product Story: $myProductStory\n Product Spokesperson: $mySpokesperson\n Product Reviewed: $myProductReviewed\n Product Decision Maker: $myProductDecision\n Samples: $file2\n $file3", "From: $Email" ); } else { $error .= 'There was an error in our database, please try again!'; } } } echo '<span class="textError">' . $error . '</span>'; ?> <form name="myform" action="" method="post" enctype="multipart/form-data"> <input type="hidden" name="myDate" size="45" maxlength="50" value="<?php echo date("F j, Y"); ?>" /> What is the name of your company and product? <textarea name="myNameAndProduct" cols="72" rows="1"><?php echo $myNameAndProduct; ?></textarea> What is your phone number? <textarea name="myPhone" cols="72" rows="1"><?php echo $myPhone; ?></textarea> What is your email? <textarea name="myEmail" cols="72" rows="1"><?php echo $myEmail; ?></textarea> website we can visit for more information? <textarea name="myWebsite" cols="72" rows="1"><?php echo $myWebsite; ?></textarea> What is this product used for? <textarea name="myProductUse" cols="72" rows="3"><?php echo $myProductUse; ?></textarea> Does this product solve a problem? If so, how? <textarea name="myProductProblemSolver" cols="72" rows="3"><?php echo $myProductProblemSolver; ?></textarea> Does your product have unique or cutting-edge features, ingredients or benefits? <textarea name="myProductUnique" cols="72" rows="3"><?php echo $myProductUnique; ?></textarea> What makes it better than similar products? <textarea name="myWhyBetter" cols="72" rows="3"><?php echo $myWhyBetter; ?></textarea> Does this product have mass appeal? Who is the target audience? <textarea name="myProductAppeal" cols="72" rows="3"><?php echo $myProductAppeal; ?></textarea> Is this product currently selling elsewhere? If so, where and what is the retail price? <textarea name="myProductSelling" cols="72" rows="3"><?php echo $myProductSelling; ?></textarea> Do you have independent research to prove claims about this product? Please provide copies. <textarea name="myProductResearch" cols="72" rows="3"><?php echo $myProductResearch; ?></textarea> Upload independent research copies (Word or PDF files): <input type="file" name="file1" id="file1" /> How and why did your company start? Does that "story" make this product more interesting? <textarea name="myProductStory" cols="72" rows="3"><?php echo $myProductStory; ?></textarea> Do you have a passionate spokesperson that would be comfortable presenting on TV? <textarea name="mySpokesperson" cols="72" rows="3"><?php echo $mySpokesperson; ?></textarea> Has this product ever been submitted to QVC for review? If yes, please provide the details. <textarea name="myProductReviewed" cols="72" rows="3"><?php echo $myProductReviewed; ?></textarea> Who from your company will make the decision to proceed if QVC is presents the right opportunity? <textarea name="myProductDecision" cols="72" rows="3"><?php echo $myProductDecision; ?></textarea> Upload two samples of your product for review (.JPG or PDF files): <input type="file" name="file2" id="file2" /> <input type="file" name="file3" id="file3" /> <input type="submit" name="submit" value="Submit New Product" class="submitButtonProduct" /><br /> </form> It's essentially the same feedback for I always use and it works fine, but I have been trying to integrate the upload portion into it from a tutorial I found on W3C: http://www.w3schools.com/PHP/php_file_upload.asp Any ideas on how to make this work? Quote Link to comment https://forums.phpfreaks.com/topic/230954-how-do-i-allow-a-user-to-upload-and-send-images-with-a-feedback-form/ Share on other sites More sharing options...
Eyewash01 Posted March 17, 2011 Share Posted March 17, 2011 Where are you struggling with the W3 tutorial, cause there's not much to it really. You just need to add a input of type="file" to your user form, and then when they submit the form, the details for it will be in the $_FILES array. In it's simplest form, you could simply add this to your processing page: <?php copy($_FILES['fmFile']['tmp_name'],"_assets/".$_FILES['fmFile']['name']); // fmFile being the name of your form field, and the second function parameter being the directory you want to store the file. ?> Quote Link to comment https://forums.phpfreaks.com/topic/230954-how-do-i-allow-a-user-to-upload-and-send-images-with-a-feedback-form/#findComment-1188949 Share on other sites More sharing options...
spacepoet Posted March 18, 2011 Author Share Posted March 18, 2011 Hi: Thanks for the reply, but not quite following. I have 3 INPUT fields already: <input type="file" name="file1" /> <input type="file" name="file2" /> <input type="file" name="file3" /> I tried to modify your code like this: ... mysql_real_escape_string($file1), //OTHER DATA mysql_real_escape_string($myProductDecision), mysql_real_escape_string($file2), mysql_real_escape_string($file3)); copy($_FILES['file1']['tmp_name'],"/uploads/".$_FILES['file1']['uploads']); // fmFile being the name of your form field, and the second function parameter being the directory you want to store the file. if(mysql_query($sql)) { ... But I get the error: Warning: copy(/uploads/) [function.copy]: failed to open stream: Is a directory in /html/Submit.Product2.php on line 169 I have "Write" permissions added to the folder, so I don't think it's that .. Maybe my Database field type is wrong ??: `file1` varchar(255) default NULL, `file2` varchar(255) default NULL, `file3` varchar(255) default NULL, ??? I thought - having done this with ASP - that I should be storing the file names in the Database as a string/text, and store the files in another folder "uploads" What am I missing ?? Quote Link to comment https://forums.phpfreaks.com/topic/230954-how-do-i-allow-a-user-to-upload-and-send-images-with-a-feedback-form/#findComment-1188978 Share on other sites More sharing options...
.josh Posted March 18, 2011 Share Posted March 18, 2011 I think you need to go back and read that w3c tutorial more carefully...what you are doing is nothing like what is in that tutorial. Quote Link to comment https://forums.phpfreaks.com/topic/230954-how-do-i-allow-a-user-to-upload-and-send-images-with-a-feedback-form/#findComment-1188980 Share on other sites More sharing options...
spacepoet Posted March 18, 2011 Author Share Posted March 18, 2011 Yes, I'm rather confused by this one .. lol .. Quote Link to comment https://forums.phpfreaks.com/topic/230954-how-do-i-allow-a-user-to-upload-and-send-images-with-a-feedback-form/#findComment-1188986 Share on other sites More sharing options...
.josh Posted March 18, 2011 Share Posted March 18, 2011 Your main issue is your call to copy(). $_FILES['file1']['uploads'] does not exist. Go back and look at the tutorial, what they use in the copy() Quote Link to comment https://forums.phpfreaks.com/topic/230954-how-do-i-allow-a-user-to-upload-and-send-images-with-a-feedback-form/#findComment-1188987 Share on other sites More sharing options...
spacepoet Posted March 18, 2011 Author Share Posted March 18, 2011 I'm getting confused with what I originally posted, and the new code .. But, I think I need to find a better upload tutorial/script, anyway. The one from WC3 works sometimes, but it gives errors that should not be errors (file already exists when it is clear not on the server already, invalid file type when it is clearly a .gif, etc.) .. Is there a "better" one you can recommend ? Quote Link to comment https://forums.phpfreaks.com/topic/230954-how-do-i-allow-a-user-to-upload-and-send-images-with-a-feedback-form/#findComment-1189001 Share on other sites More sharing options...
Eyewash01 Posted March 18, 2011 Share Posted March 18, 2011 I have 3 INPUT fields already: <input type="file" name="file1" /> <input type="file" name="file2" /> <input type="file" name="file3" /> I tried to modify your code like this: ... mysql_real_escape_string($file1), //OTHER DATA mysql_real_escape_string($myProductDecision), mysql_real_escape_string($file2), mysql_real_escape_string($file3)); copy($_FILES['file1']['tmp_name'],"/uploads/".$_FILES['file1']['uploads']); // fmFile being the name of your form field, and the second function parameter being the directory you want to store the file. if(mysql_query($sql)) { ... But I get the error: Warning: copy(/uploads/) [function.copy]: failed to open stream: Is a directory in /html/Submit.Product2.php on line 169 Firstly, you have referenced $_FILES['file1']['uploads'] when that should be the file name, i.e $_FILES['file1']['name'], or whatever you want to call the file. Secondly you can't refer to them as simply $file1 and $file2, you need to use the $_FILES[] array (although I may be wrong if register globals is on, however if it is then you are going to have a million problems there anyway). Work through the W3 tutorial and maybe some others - they do work! Quote Link to comment https://forums.phpfreaks.com/topic/230954-how-do-i-allow-a-user-to-upload-and-send-images-with-a-feedback-form/#findComment-1189031 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.