salman.pirani Posted July 24, 2012 Share Posted July 24, 2012 i was trying to have a code from which i can upload the files on server via php and i got this code below from w3school but its not working can u help me with thisthanks_________________________________________________________ <?php$allowedExts = array("jpg", "jpeg", "gif", "png");$extension = end(explode(".", $_FILES["file"]["name"]));if ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg"))&& ($_FILES["file"]["size"] < 20000))&& in_array($extension, $allowedExts)){if ($_FILES["file"]["error"] > 0){echo "Return Code: " . $_FILES["file"]["error"] . "<br />";}else{echo "Upload: " . $_FILES["file"]["name"] . "<br />";echo "Type: " . $_FILES["file"]["type"] . "<br />";echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";if (file_exists("upload/" . $_FILES["file"]["name"])){echo $_FILES["file"]["name"] . " already exists. ";}else{move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);echo "Stored in: " . "upload/" . $_FILES["file"]["name"];}}}else{echo "Invalid file";}?> Link to comment Share on other sites More sharing options...
Ingolme Posted July 24, 2012 Share Posted July 24, 2012 By "not working" what do you mean? Link to comment Share on other sites More sharing options...
salman.pirani Posted July 24, 2012 Author Share Posted July 24, 2012 its giving error in line number 8 Link to comment Share on other sites More sharing options...
justsomeguy Posted July 24, 2012 Share Posted July 24, 2012 That error is discussed here: http://w3schools.invisionzone.com/index.php?showtopic=44375 Link to comment Share on other sites More sharing options...
dsonesuk Posted July 24, 2012 Share Posted July 24, 2012 You have to many closing parenthesis for if condition Link to comment Share on other sites More sharing options...
salman.pirani Posted July 24, 2012 Author Share Posted July 24, 2012 as per the guidlines gven in that post i have make these changes in code but still its not working_____________________________________________________________________________ <?php$allowedExts = array("jpg", "jpeg", "gif", "png");$chunks = explode(".", $_FILES["file"]["name"]);$extension = end($chunks);if (($_FILES["file"]["type"] == "image/gif"|| $_FILES["file"]["type"] == "image/jpeg"|| $_FILES["file"]["type"] == "image/pjpeg")&& $_FILES["file"]["size"] < 20000&& in_array($extension, $allowedExts))if ($_FILES["file"]["error"] > 0)(echo "Return Code: " . $_FILES["file"]["error"] . "<br />";)else(echo "Upload: " . $_FILES["file"]["name"] . "<br />";echo "Type: " . $_FILES["file"]["type"] . "<br />";echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";)if (file_exists("upload/" . $_FILES["file"]["name"]))(echo $_FILES["file"]["name"] . " already exists. ";)else(move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);echo "Stored in: " . "upload/" . $_FILES["file"]["name"]))else(echo "Invalid file"?> Link to comment Share on other sites More sharing options...
justsomeguy Posted July 24, 2012 Share Posted July 24, 2012 What exactly is happening? We need more information other than "it doesn't work". Link to comment Share on other sites More sharing options...
salman.pirani Posted July 24, 2012 Author Share Posted July 24, 2012 Parse error: parse error in C:\wamp\www\forum\upload_file.php on line 17 its giving this error Link to comment Share on other sites More sharing options...
Ingolme Posted July 24, 2012 Share Posted July 24, 2012 The problem is here:if ($_FILES["file"]["error"] > 0)(echo "Return Code: " . $_FILES["file"]["error"] . "<br />" You have parenthesis instead of curly brackets. Link to comment Share on other sites More sharing options...
salman.pirani Posted July 26, 2012 Author Share Posted July 26, 2012 its still giving this error Parse error: parse error in C:\wamp\www\upload\upload_file.php on line 25 Link to comment Share on other sites More sharing options...
salman.pirani Posted July 26, 2012 Author Share Posted July 26, 2012 below is the code which am executing but its not giving me result, kindly run this whole code at your hand and if it get the result out so kindly share me the whole code instead of sharing changes in one line many thanks ____________________________________________________________________________________________________<?php$allowedExts = array("jpg", "jpeg", "gif", "png");$chunks = explode(".", $_FILES["file"]["name"]);$extension = end($chunks);if (($_FILES["file"]["type"] == "image/gif"|| $_FILES["file"]["type"] == "image/jpeg"|| $_FILES["file"]["type"] == "image/pjpeg")&& $_FILES["file"]["size"] < 20000&& in_array($extension, $allowedExts))if ($_FILES["file"]["error"] > 0)(echo "Return Code: " . $_FILES["file"]["error"] . "<br />";)else(echo "Upload: " . $_FILES["file"]["name"] . "<br />";echo "Type: " . $_FILES["file"]["type"] . "<br />";echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";)if (file_exists("upload/" . $_FILES["file"]["name"]))(echo $_FILES["file"]["name"] . " already exists. ";)else(move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);echo "Stored in: " . "upload/" . $_FILES["file"]["name"]))else(echo "Invalid file"?> Link to comment Share on other sites More sharing options...
birbal Posted July 26, 2012 Share Posted July 26, 2012 you are still using first parentheses instead of curly braces. when you declare codeblocks it is surrounded by curly braces if ($_FILES["file"]["error"] > 0){echo "Return Code: " . $_FILES["file"]["error"] . "<br />";} and it is not always possible for us to run and test every codes posted and updated here. you should run that code yourself and specify the errors,what was you expecting and what is happening instead. Link to comment Share on other sites More sharing options...
kanchatchai Posted July 26, 2012 Share Posted July 26, 2012 Do you have upload folder and chmod it to allow upload file? Link to comment Share on other sites More sharing options...
salman.pirani Posted July 26, 2012 Author Share Posted July 26, 2012 actually i was trying to say that every time i make changes i never work any doing change in few line again and again making me confuse so could you let me know the finall whole code that works so i will be very thank full to you. Link to comment Share on other sites More sharing options...
birbal Posted July 26, 2012 Share Posted July 26, 2012 you have the braces missmatch. use proper indention and use a decent text editor. this kind of error is easy to spot from there. $allowedExts = array("jpg", "jpeg", "gif", "png");$chunks = explode(".", $_FILES["file"]["name"]);$extension = end($chunks);if (($_FILES["file"]["type"] == "image/gif"|| $_FILES["file"]["type"] == "image/jpeg"|| $_FILES["file"]["type"] == "image/pjpeg")&& $_FILES["file"]["size"] < 20000&& in_array($extension, $allowedExts)){if ($_FILES["file"]["error"] > 0){echo "Return Code: " . $_FILES["file"]["error"] . "<br />";}else{echo "Upload: " . $_FILES["file"]["name"] . "<br />";echo "Type: " . $_FILES["file"]["type"] . "<br />";echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";}if (file_exists("upload/" . $_FILES["file"]["name"])){echo $_FILES["file"]["name"] . " already exists. ";}else{move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);echo "Stored in: " . "upload/" . $_FILES["file"]["name"];}}else{echo "Invalid file";} Link to comment Share on other sites More sharing options...
salman.pirani Posted July 31, 2012 Author Share Posted July 31, 2012 is this the correct code Link to comment Share on other sites More sharing options...
birbal Posted July 31, 2012 Share Posted July 31, 2012 i fixed only the parse error.you still need to check and run it from your side for any kind of runtime error or others. Link to comment Share on other sites More sharing options...
birbal Posted July 31, 2012 Share Posted July 31, 2012 i fixed only the parse error.you still need to check and run it from your side for any kind of runtime error or others. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now