spikypunker Posted January 11, 2008 Share Posted January 11, 2008 Hi, I have been trying to construct a simple image uploader for a news item entry. The user selects a news item from the list, then on the same page they select a file to upload. I've had two problems wtih this so far and no one else to ask for help ( lol Firstly this is how I've changed the basic script to allow each image to be saved with the ID of the news item as the filename. I.e News item 5 would have newsno = 5 so the image would be saved as 5.gif. I've not worried about verification at this stage, which i obviously will after getting the basics to work but security wise this site cannot be accessed by the public so there should be no risk. addimage.php <body> <form method="post" action="selectimage.php"> <span class="style1">First select the News Item you wish you add an image to...</span><br /> <br /> <? mysql_connect("db1", "stanga_1", "*********"); @mysql_select_db("stanga_1") or die ("unable to connect"); $query = "SELECT * FROM news"; $result = mysql_query($query); $num = mysql_num_rows($result); mysql_close(); $i = 0; while ($i < $num) { $newsno = mysql_result($result,$i,"newsno"); $date = mysql_result($result,$i,"date"); $head = mysql_result($result,$i,"head"); $body = mysql_result($result,$i,"body"); ?> <input name="item" type="radio" id="item" value="<? echo $newsno; ?>"> <? echo " <span class='style2'>$head</span><br>"; $i++; } ?> <br /><Br /><br /> <span class="style1">Now Select the Image you wish to upload</span>...<br /><br /> <input name="uploadFile" type="file" /><br /><br /> <input type="submit" value="Add Image"/> </form> </body> selectimage.php <? $id = $_POST['item']; echo ($id); $target_path = "uploads/"; $target_path = $target_path . basename( '$id'.gif); [color=red]THIS IS THE BIT NOT TOO SURE ABOUT![/color] if(move_uploaded_file($_FILES['uploadedFile']['tmp_name'], $target_path)) { echo "The file ". basename('$id'.gif). [color=red]THIS IS THE BIT NOT TOO SURE ABOUT![/color] " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; } ?> Secondly, This is the original basic work-through which simply saves the file with the original name, not using the news ID, THIS ISNT WORKING EITHER!!!! Which is my main concern really, i can't actually upload a file at all! selectimage.php <? $id = $_POST['item']; echo ($id); $target_path = "uploads/"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedFile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/85499-solved-php-file-uploader/ Share on other sites More sharing options...
priti Posted January 11, 2008 Share Posted January 11, 2008 kindly note The code in second code snippet $id = $_POST['item']; echo ($id);//you have fetched the id $target_path = "uploads/"; $target_path = $target_path . basename( '$id'.gif); echo $target_path and see what it prints does it print uploads/$id.gif or uploads/1.gif????? Quote Link to comment https://forums.phpfreaks.com/topic/85499-solved-php-file-uploader/#findComment-436362 Share on other sites More sharing options...
spikypunker Posted January 11, 2008 Author Share Posted January 11, 2008 Hi thanks for reply, Yeah for some reasons it's printing uploads/$id.gif I dont know why?? Can anyone help? More important is the fact that it's not uploading anything at all on the normal upload form?? I've checked with the hosting provider and they say they have no restrictions on php upload forms apart from a 6mb upload limit, the test file i am using is about 2kb! Quote Link to comment https://forums.phpfreaks.com/topic/85499-solved-php-file-uploader/#findComment-436537 Share on other sites More sharing options...
spikypunker Posted January 11, 2008 Author Share Posted January 11, 2008 I just tried these: $target_path = $target_path . basename( $id".gif"); And got this: Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in /mnt/vol1/home/s/t/stanga/public_html/selectimage.php on line 27 When i try this: $target_path = $target_path . basename( $id '.gif'); I get this: Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in /mnt/vol1/home/s/t/stanga/public_html/selectimage.php on line 27 Quote Link to comment https://forums.phpfreaks.com/topic/85499-solved-php-file-uploader/#findComment-436546 Share on other sites More sharing options...
spikypunker Posted January 11, 2008 Author Share Posted January 11, 2008 Ah, just tried this $target_path = $target_path . basename( $id); And that worked, so it must be the end bit thats not working, ie trying to get it to add.gif at the end Quote Link to comment https://forums.phpfreaks.com/topic/85499-solved-php-file-uploader/#findComment-436551 Share on other sites More sharing options...
spikypunker Posted January 11, 2008 Author Share Posted January 11, 2008 Ok i feel pretty stupid on that bit, i simply did this $id = $id . ".gif"; Which is solving the problem of naming the file after the ID of the news item... BUT it's still not actually upl;oading anything????? Quote Link to comment https://forums.phpfreaks.com/topic/85499-solved-php-file-uploader/#findComment-436558 Share on other sites More sharing options...
adam291086 Posted January 11, 2008 Share Posted January 11, 2008 before you do anything with the uploaded file try echoing out the file while in tempy store echo $_FILES["file"]["tmp_name"]; Quote Link to comment https://forums.phpfreaks.com/topic/85499-solved-php-file-uploader/#findComment-436560 Share on other sites More sharing options...
spikypunker Posted January 11, 2008 Author Share Posted January 11, 2008 Hi right, cheers for that, tried that and i'm getting nothing. Nothing is printed on screen. Quote Link to comment https://forums.phpfreaks.com/topic/85499-solved-php-file-uploader/#findComment-436562 Share on other sites More sharing options...
adam291086 Posted January 11, 2008 Share Posted January 11, 2008 you need to put enctype=multipart/form-data in your form settings <form method="post" action="selectimage.php" enctype=multipart/form-data> Now what do you get? Quote Link to comment https://forums.phpfreaks.com/topic/85499-solved-php-file-uploader/#findComment-436566 Share on other sites More sharing options...
spikypunker Posted January 11, 2008 Author Share Posted January 11, 2008 Tried that, still no joy, same as before. My charset is charset=UTF-8" Does that need to be changed at all? Quote Link to comment https://forums.phpfreaks.com/topic/85499-solved-php-file-uploader/#findComment-436571 Share on other sites More sharing options...
spikypunker Posted January 14, 2008 Author Share Posted January 14, 2008 Morning! Anyone got anymore on this?? Really need help! Quote Link to comment https://forums.phpfreaks.com/topic/85499-solved-php-file-uploader/#findComment-438689 Share on other sites More sharing options...
adam291086 Posted January 14, 2008 Share Posted January 14, 2008 Post all of your current code. Quote Link to comment https://forums.phpfreaks.com/topic/85499-solved-php-file-uploader/#findComment-438691 Share on other sites More sharing options...
spikypunker Posted January 14, 2008 Author Share Posted January 14, 2008 Hi, cheers for helping. Right here it is to date. addimage.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Add Image</title> <style type="text/css"> <!-- .style2 {font-size: 12px} .style1 {font-size: 14px} a { font-family: Verdana, Arial, Helvetica, sans-serif; color: #000000; } a:visited { color: #000000; text-decoration: none; } a:hover { color: #000000; text-decoration: underline; } a:active { color: #000000; text-decoration: none; } a:link { text-decoration: none; } --> </style> </head> <body> <form enctype=multipart/form-data method="post" action="selectimage.php" > <? mysql_connect("db1", "stanga_1", "pencil"); @mysql_select_db("stanga_1") or die ("unable to connect"); $query = "SELECT * FROM news"; $result = mysql_query($query); $num = mysql_num_rows($result); mysql_close(); $i = 0; while ($i < $num) { $newsno = mysql_result($result,$i,"newsno"); $date = mysql_result($result,$i,"date"); $head = mysql_result($result,$i,"head"); $body = mysql_result($result,$i,"body"); ?> <input name="item" type="radio" id="item" value="<? echo $newsno; ?>"> <? echo " <span class='style2'>$head</span><br>"; $i++; } ?> <br /><Br /><br /> <span class="style1">Now Select the Image you wish to upload</span>...<br /><br /> <input name="uploadFile" type="file" /><br /><br /> <input type="submit" value="Add Image"/> </form> </body> </html> [\code] selectimage.php [code] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Delete</title> <style type="text/css"> <!-- .style1 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; } --> </style> </head> <body> <span class="style1"> <? $id = $_POST['item']; echo $_FILES["file"]["tmp_name"]; $id = $id . ".gif"; $target_path = "uploads/"; $target_path = $target_path . basename( $id); if(move_uploaded_file($_FILES['uploadedFile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; } ?> </body> </html> [\code] At the moment it's passing the variable fine, creating the right file path ie "uploads/3.gif" but not uploading the file at all? I've contacted the hosters and they have nothing blocking php uploads providing the file is no bigger than 6 mb. Thanks for your time! [/code] Quote Link to comment https://forums.phpfreaks.com/topic/85499-solved-php-file-uploader/#findComment-438695 Share on other sites More sharing options...
adam291086 Posted January 14, 2008 Share Posted January 14, 2008 Is the file being echoed out echo $_FILES["file"]["tmp_name"]; Quote Link to comment https://forums.phpfreaks.com/topic/85499-solved-php-file-uploader/#findComment-438702 Share on other sites More sharing options...
spikypunker Posted January 14, 2008 Author Share Posted January 14, 2008 Nope, plus it's changing pages instantly, so there doesn't seem to be any uploading taking place at all. As soon as you click upload it jumps straight to the next page. Quote Link to comment https://forums.phpfreaks.com/topic/85499-solved-php-file-uploader/#findComment-438704 Share on other sites More sharing options...
adam291086 Posted January 14, 2008 Share Posted January 14, 2008 i think <input name="uploadFile" type="file" /><br /><br /> should be <input type="file" name="uploadFile" /><br /><br /> Quote Link to comment https://forums.phpfreaks.com/topic/85499-solved-php-file-uploader/#findComment-438712 Share on other sites More sharing options...
spikypunker Posted January 14, 2008 Author Share Posted January 14, 2008 Hi, thanks for that but still no joy, same as before, nothing being echoed out... Do you have an example that you know definately works so i could test to see if it's the server? Quote Link to comment https://forums.phpfreaks.com/topic/85499-solved-php-file-uploader/#findComment-438742 Share on other sites More sharing options...
adam291086 Posted January 14, 2008 Share Posted January 14, 2008 <?php $path = dirname(__FILE__); $destination= $path; if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; } else { move_uploaded_file ($_FILES["file"]["tmp_name"] , $destination. $_FILES["file"]["name"]); echo "Upload Successful"; } Quote Link to comment https://forums.phpfreaks.com/topic/85499-solved-php-file-uploader/#findComment-438745 Share on other sites More sharing options...
spikypunker Posted January 14, 2008 Author Share Posted January 14, 2008 This is saying "Upload Succesfull" but i'm not seeing it on the server? Quote Link to comment https://forums.phpfreaks.com/topic/85499-solved-php-file-uploader/#findComment-438773 Share on other sites More sharing options...
adam291086 Posted January 14, 2008 Share Posted January 14, 2008 use this form as well <html> <body><form action="uploader.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form></body> </html> also echo out $destination and see where the file is supposed to be going. Also echo $_FILES["file"]["tmp_name"] Quote Link to comment https://forums.phpfreaks.com/topic/85499-solved-php-file-uploader/#findComment-438778 Share on other sites More sharing options...
spikypunker Posted January 14, 2008 Author Share Posted January 14, 2008 hi, ok i tried that new form, with the echos you asked for. If i try: echo "$_FILES["file"]["tmp_name"]"; I get: Parse error: parse error, unexpected '\"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in /mnt/vol1/home/s/t/stanga/public_html/uploader.php on line 22 And when i try: echo " $destination"; I get: mnt/vol1/home/s/t/stanga/public_html (which is root folder) Quote Link to comment https://forums.phpfreaks.com/topic/85499-solved-php-file-uploader/#findComment-438844 Share on other sites More sharing options...
adam291086 Posted January 14, 2008 Share Posted January 14, 2008 so the destination is correct for now. What was on line 22. Post all the uploader.php script with the echos Quote Link to comment https://forums.phpfreaks.com/topic/85499-solved-php-file-uploader/#findComment-438849 Share on other sites More sharing options...
spikypunker Posted January 14, 2008 Author Share Posted January 14, 2008 Line 22 is the echo "$_FILES["file"]["tmp_name"]"; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Delete</title> <style type="text/css"> <!-- .style1 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; } --> </style> </head> <body> <span class="style1"> <?php $path = dirname(__FILE__); $destination= $path; echo "$_FILES["file"]["tmp_name"]"; if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; } else { move_uploaded_file ($_FILES["file"]["tmp_name"] , $destination. $_FILES["file"]["name"]); echo "Upload Successful"; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/85499-solved-php-file-uploader/#findComment-438855 Share on other sites More sharing options...
adam291086 Posted January 14, 2008 Share Posted January 14, 2008 its beacsuse you are trying to treat a variable as a string with " " echo "$_FILES["file"]["tmp_name"]"; should be echo $_FILES["file"]["tmp_name"]; Quote Link to comment https://forums.phpfreaks.com/topic/85499-solved-php-file-uploader/#findComment-438857 Share on other sites More sharing options...
spikypunker Posted January 14, 2008 Author Share Posted January 14, 2008 Ah i see, ok right, it's now echoed the temp name: /tmp/php1DtmGL Is what it displayed. Quote Link to comment https://forums.phpfreaks.com/topic/85499-solved-php-file-uploader/#findComment-438864 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.