How to Insert images as file instead of inserting them in Database as BLOB

257 Views Asked by At

I am new to php. i want to upload images from html form into file. i searched on net but unable to find anything helpful. Please tell me how to uplaod images in file rather than database. i know the method of uploading images in database, and its very easy. Is there any way like this of uploading images in file. sorry for my bad english.

$_FILES['image']['tmp_name'];
$original_image=file_get_contents ($_FILES['image']['tmp_name']);
$name= $_FILES['image']['name'];
2

There are 2 best solutions below

3
On BEST ANSWER

Take a look at move_uploaded_file.

This is an example from the link:

<?php
$uploads_dir = '/uploads';
foreach ($_FILES["image"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["image"]["tmp_name"][$key];
        $name = $_FILES["image"]["name"][$key];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
    }
}
?>

Shorter example without support for multiple files/error checking:

<?php
    $tmp_name = $_FILES["image"]["tmp_name"];
    $name = $_FILES["image"]["name"];
    move_uploaded_file($tmp_name, "uploads/$name");
?>

Even shorter as a one-liner:

<?php
    move_uploaded_file($_FILES["image"]["tmp_name"], "uploads/" . $_FILES["image"]["name"]);
?>
2
On

do you mean you want to store your image to your server.

just use these two functions

<?php
    copy($_FILES['image']['tmp_name'], $imgPath); //copy your image to a specific path on server

    move_uploaded_file($_FILES['image']['tmp_name'], $imgPath); //copy your image to a specific path on server and delete uploaded image in temp folder
?>