Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php file upload guide

Tags:

php

How to upload file in php and send filename to database?

like image 666
ktm Avatar asked Sep 12 '10 21:09

ktm


People also ask

How does PHP file upload work?

A PHP script can be used with a HTML form to allow users to upload files to the server. Initially files are uploaded into a temporary directory and then relocated to a target destination by a PHP script. The user opens the page containing a HTML form featuring a text files, a browse button and a submit button.

What is Tmp_name in PHP file upload?

tmp_name is the temporary name of the uploaded file which is generated automatically by php, and stored on the temporary folder on the server. name is the original name of the file which is store on the local machine.

How can get upload file size in PHP?

The filesize() function in PHP is an inbuilt function which is used to return the size of a specified file. The filesize() function accepts the filename as a parameter and returns the size of a file in bytes on success and False on failure.


1 Answers

<?php
 if (isset($_FILES['file'])) 
 {
    $file = $_FILES['file'];

    // File Properties
    $file_name = $file['name'];
    $file_tmp = $file['tmp_name'];
    $file_size = $file['size'];
    $file_error = $file['error'];

    // Work out the file extension
    $file_ext = explode('.', $file_name);
    $file_ext = strtolower(end($file_ext));

    $allowed = array('png', 'jpg');

    //filename
    $id = 'uploads/Test';

    if (!file_exists($id)) 
    {
        mkdir($id, 0777, true);
    }

    if (in_array($file_ext, $allowed)) {
        if ($file_error === 0) {
            if ($file_size <= 2097152) {

                $file_name_new = uniqid('', true) . '.' . $file_ext;
                $file_destination = $id .'/'. $file_name_new;

                if (move_uploaded_file($file_tmp, $file_destination)) {
                    echo $file_destination;
                }
            }
        }
    }
}

    ?>
like image 75
Berk Balik Avatar answered Sep 19 '22 21:09

Berk Balik