Jump to content

File Upload.


190319m9
 Share

Recommended Posts

<?php        if (((($_FILES["file"]["type"] == "image/gif")        || ($_FILES["file"]["type"] == "image/jpeg")        || ($_FILES["file"]["type"] == "image/pjpeg")        || ($_FILES["file"]["type"] == "image/png"))        && ($_FILES[["file"]["size"] < 20*1024*1024))            {            if ($_FILES["file"]["error"] > 0)                {                echo "Error : " . $_FILES["file"]["error"] . "<br />";                }            else                 {                echo "Upload : " . $_FILES["file"]["name"] . "<br />";                echo "Size : " (.$_FILES["file"]["size"] / 1024) . " Kb <br />";                                if (file_exists("d:/hosting/sau100rabh/m_nine/toy_group/upload/" . $_FILES["file"]["name"]))                    {                    echo $_FILES["file"]["name"] . " already exists."                    }                else                     {                    move_uploaded_file($_FILES["file"]["tmp_name"], "d:/hosting/sau100rabh/m_nine/toy_group/upload/" . $_FILES["file"]["name"]);                    echo "File upload process completed."                    }                }             }        else            {            echo "File format not supported or file upload size limit exceeded.";            }     ?> 

Q1. What is the error in above code meant to handle file uploaded from using an HTML form?Q2. Can I detect if a wrong file format or a larger file is being uploaded before the upload is started? I wish to avoid Javascript or server side scripting. Some method in PHP would be of better utility.

Link to comment
Share on other sites

A1. What does the error say when you try to run it?A2. NB: PHP is a server-side scripting language. The following extract of the code defines what file types are allowed and what size is permitted:

if (((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg")|| ($_FILES["file"]["type"] == "image/png"))&& ($_FILES[["file"]["size"] < 20*1024*1024))

Link to comment
Share on other sites

The page does not runs any scripts and shows a completely blank page. Probably a minor syntax error.And yes that code describes file formats and upload size limit, but it verifies it with the uploaded only after the file upload is complete on execution of action attribute of the form tag on the file upload page.

Link to comment
Share on other sites

Do you have error reporting enabled?Oh I see, before the upload starts. You can check the extension by parsing the value property of the input in JavaScript, but you can't check the size. Remember though that JS can always be disabled or edited so you should keep the server-side validation.

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
 Share

×
×
  • Create New...