Jump to content

error in w3shools php lessons( in_array)


RSA

Recommended Posts

what error is it showing?

Link to comment
Share on other sites

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

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

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.

  • Like 1
Link to comment
Share on other sites

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

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.

  • Like 1
Link to comment
Share on other sites

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

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

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

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

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...