Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Discussion on upload File in PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 301
    Comment on it

    Hi Reader's,
    Welcome to FindNerd, today we are going to discuss File upload in PHP.

    When developing a web application in PHP sometimes we need to upload images in our application. Basically, we can say that when a user goes for registration in an application then he upload his image for the profile. After uploading image user can see his profile image after signin.

    So uploading an image is not a difficult task in PHP, if below steps were followed, and this will help to easily upload an image in application.

     

    1- Firstly we have to configure "php.ini" File and we have to verify the file_uploads directive is set On.
     we can see below syntax

     file_uploads = On 

     

    2- After completing the first step we have to create a view for selecting a file to upload.

    we can follow below code:

    <form action="upload.php" method="post" enctype="multipart/form-data">
      Image Path: <input type="file" name="UploadFile">
      <input type="submit" value="Upload Image" name="submit">
    </form>

    In the above code, we are giving an action "upload.php" which will execute on submit button and we will use POST attribute method and has an enctype attribute set to multipart/form-data. Because without these attributes, the file upload will not work.

     

    3- PHP script for uploading

    <?php
        //define here target directory where file will be uploaded.
        $target_dir = "uploads/";
        $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
        $uploadstatus = 1;
        $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
        if(isset($_POST["submit"])) {
          $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
          if($check !== false) {
            ////show success message
            echo "Succes ! Image uploaded and name is - " . $check["mime"] . ".";
            $uploadstatus = 1;
          }else {
            //show error message
            echo "error ! image not uploaded please try again.";
            $uploadstatus = 0;
    
          }
        }
    ?>

    I hope this blog will help you for uploading image in your PHP web application.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: