RSA Posted July 18, 2012 Share Posted July 18, 2012 new in php world and I'am following w3schools lessons but there is an error in w3shools php lessons( in_array) I'm trying to follow and do these steps to upload a file http://www.w3schools.com/php/php_file_upload.asp this code gives me an error in this line&& in_array($extension, $allowedExts)) Link to comment Share on other sites More sharing options...
birbal Posted July 18, 2012 Share Posted July 18, 2012 what error is it showing? Link to comment Share on other sites More sharing options...
thescientist Posted July 18, 2012 Share Posted July 18, 2012 are you sure $extension and $allowedExts are what you expect them to be? please provide the code, and debugging you attempted, along with the actual error you are getting. Link to comment Share on other sites More sharing options...
RSA Posted July 18, 2012 Author Share Posted July 18, 2012 The error Parse error: syntax error, unexpected T_BOOLEAN_AND in C:\AppServ\www\kids\upload_file.php on line 20 Link to comment Share on other sites More sharing options...
RSA Posted July 18, 2012 Author Share Posted July 18, 2012 Code<?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 "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"]; } }else { echo "Invalid file"; }?> Link to comment Share on other sites More sharing options...
justsomeguy Posted July 18, 2012 Share Posted July 18, 2012 The error is in these lines: if ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg"))&& ($_FILES["file"]["size"] < 20000))&& in_array($extension, $allowedExts)) The extra paren at the end of the fourth line closes the if statement, so the next line is not seen as part of the if statement. The if statement has one set of parentheses surrounding all of the conditions, and parens can be used to group conditions. It would be easier to read like this: if ( ( $_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/pjpeg" ) && $_FILES["file"]["size"] < 20000 && in_array($extension, $allowedExts)) I removed some of the parens, you don't need them around a single condition. 1 Link to comment Share on other sites More sharing options...
RSA Posted July 18, 2012 Author Share Posted July 18, 2012 Thanks all and thanks justsomeguy It works now I just copied the code from the website and u see the extra parentheses I'm disappointed now how can I follow these tutorials now ------------------- The result is Strict Standards: Only variables should be passed by reference in C:\AppServ\www\kids\upload_file.php on line 13 Upload: goog.jpg Type: image/jpeg Size: 12.015625 Kb Stored in: C:\WINDOWS\Temp\php82.tmp Warning: move_uploaded_file(upload/goog.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\AppServ\www\kids\upload_file.php on line 41 Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\WINDOWS\Temp\php82.tmp' to 'upload/goog.jpg' in C:\AppServ\www\kids\upload_file.php on line 41 Stored in: upload/goog.jpg Link to comment Share on other sites More sharing options...
justsomeguy Posted July 18, 2012 Share Posted July 18, 2012 Some of the tutorials may contain errors, but we'll be here to help you out. The error for move_uploaded_file may mean that the upload directory that you're trying to move the file to doesn't exist. It won't create a directory that doesn't exist automatically. The warning about strict standards is because of this line: $extension = end(explode(".", $_FILES["file"]["name"])); This version will just hide that error message: $extension = @end(explode(".", $_FILES["file"]["name"])); and this will fix it: $chunks = explode(".", $_FILES["file"]["name"]);$extension = end($chunks); The error is because the end function requires an actual variable, not the return value from another function. 1 Link to comment Share on other sites More sharing options...
RSA Posted July 18, 2012 Author Share Posted July 18, 2012 Now no errors thanks again u r marvelous :)The result isUpload: goog.jpgType: image/jpegSize: 12.015625 KbStored in: C:\WINDOWS\Temp\php8D.tmpStored in: upload/goog.jpg Link to comment Share on other sites More sharing options...
boen_robot Posted July 18, 2012 Share Posted July 18, 2012 Just to let everyone know, I used the "Report error" link at the bottom of the page, linking to this page for the fix.If you encounter an error that you know how to fix, you can use that to notify the W3Schools staff about it. If you don't know how to fix it, we can do what we just did for this error. Link to comment Share on other sites More sharing options...
salman.pirani Posted July 26, 2012 Share Posted July 26, 2012 am getting error in this code plz help me error : Parse error: parse error in C:\wamp\www\upload\upload_file.php on line 45 code: <?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...
thescientist Posted July 26, 2012 Share Posted July 26, 2012 can you put your code in code tags and indent properly please? indicating which line in your code is line 45 would be appreciated as well. Link to comment Share on other sites More sharing options...
justsomeguy Posted July 26, 2012 Share Posted July 26, 2012 That code only has 43 lines. Link to comment Share on other sites More sharing options...
thescientist Posted July 26, 2012 Share Posted July 26, 2012 Link to comment Share on other sites More sharing options...
salman.pirani Posted July 28, 2012 Share Posted July 28, 2012 this is the code____________<?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...
salman.pirani Posted July 28, 2012 Share Posted July 28, 2012 error Parse error: parse error in C:\wamp\www\forum\upload_file.php on line 17 Link to comment Share on other sites More sharing options...
boen_robot Posted July 28, 2012 Share Posted July 28, 2012 How many times do we have to tell you? You're using "(" for the if/else bodies, when you should instead be using "{", and that is causing the error.Try this: <?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";}?> Look closely at the stuff before and after "else", and see what you're using instead. Link to comment Share on other sites More sharing options...
salman.pirani Posted July 29, 2012 Share Posted July 29, 2012 oh now i got this ...but its still not giving the resultits generation this error___________________________________Upload: Chrysanthemum1.jpgType: image/jpegSize: 12.080078125 KbTemp file: C:\wamp\tmp\php5CAC.tmpWarning: move_uploaded_file(upload/Chrysanthemum1.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\wamp\www\forum\upload_file.php on line 36Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\wamp\tmp\php5CAC.tmp' to 'upload/Chrysanthemum1.jpg' in C:\wamp\www\forum\upload_file.php on line 36Stored in: upload/Chrysanthemum1.jpg Link to comment Share on other sites More sharing options...
salman.pirani Posted July 30, 2012 Share Posted July 30, 2012 plz reply Link to comment Share on other sites More sharing options...
kanchatchai Posted July 30, 2012 Share Posted July 30, 2012 (edited) http://w3schools.invisionzone.com/index.php?showtopic=44522 Edited July 30, 2012 by kanchatchai Link to comment Share on other sites More sharing options...
salman.pirani Posted July 31, 2012 Share Posted July 31, 2012 oh thanks for help now its uploading the file to the server but if its not uploading a zip file how can i make the changes in the code so that i can upload a zip or rar file to server i have tried some changes but its not working. thanks Link to comment Share on other sites More sharing options...
justsomeguy Posted July 31, 2012 Share Posted July 31, 2012 This is the part that checks for file types and extensions: $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)) { Personally, I would remove the mime type checks and only check the extension. 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