Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Upload file to server using PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 511
    Comment on it

    We can use php and html to upload the file to the server.

    Initially the file are added to the temporary folder and then are transfer to the server by using the script.

    phpinfo.php define the temporary file  that is used for the upload.

    The file is uploaded as upload_tmp_dir.

    The maximum upload size is stated upload_max_filesize.

    To upload a file

    1. Open page that contain the html, a browse button and a submit button.

    2. The user click on the browse and then select the file.

    3. Then the user click on the submit button.

    4. The selected file is sent to the temporary folder on the server.

    5. PHP script check the folder and transfer it to the real directory .

    6. The php script confirm the upload user.

     

    To create an html upload form.

     

    <?php
       if(isset($_FILES['image'])){
          $errors= array();
          $file_name = $_FILES['image']['name'];
          $file_size =$_FILES['image']['size'];
          $file_tmp =$_FILES['image']['tmp_name'];
          $file_type=$_FILES['image']['type'];
          $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
          
          $expensions= array("jpeg","jpg","png");
          
          if(in_array($file_ext,$expensions)=== false){
             $errors[]="extension not allowed, please choose a JPEG or PNG file.";
          }
          
          if($file_size > 2097152){
             $errors[]='File size must be excately 2 MB';
          }
          
          if(empty($errors)==true){
             move_uploaded_file($file_tmp,"images/".$file_name);
             echo "Success";
          }else{
             print_r($errors);
          }
       }
    ?>
    <html>
       <body>
          
          <form action="" method="POST" enctype="multipart/form-data">
             <input type="file" name="image" />
             <input type="submit"/>
          </form>
          
       </body>
    </html>

    To create upload script

    We use a global variable $_FILES.

    ex

    <?php
       if(isset($_FILES['image'])){
          $errors= array();
          $file_name = $_FILES['image']['name'];
          $file_size = $_FILES['image']['size'];
          $file_tmp = $_FILES['image']['tmp_name'];
          $file_type = $_FILES['image']['type'];
          $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
          
          $expensions= array("jpeg","jpg","png");
          
          if(in_array($file_ext,$expensions)=== false){
             $errors[]="extension not allowed, please choose a JPEG or PNG file.";
          }
          
          if($file_size > 2097152) {
             $errors[]='File size must be excately 2 MB';
          }
          
          if(empty($errors)==true) {
             move_uploaded_file($file_tmp,"images/".$file_name);
             echo "Success";
          }else{
             print_r($errors);
          }
       }
    ?>
    <html>
       <body>
          
          <form action = "" method = "POST" enctype = "multipart/form-data">
             <input type = "file" name = "image" />
             <input type = "submit"/>
    			
             <ul>
                <li>Sent file: <?php echo $_FILES['image']['name'];  ?>
                <li>File size: <?php echo $_FILES['image']['size'];  ?>
                <li>File type: <?php echo $_FILES['image']['type'] ?>
             </ul>
    			
          </form>
          
       </body>
    </html>

     

 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: