1

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'];
1ry
  • 77
  • 10
Aftab Aamir
  • 175
  • 1
  • 4
  • 12

2 Answers2

0

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"]);
?>
Hein Andre Grønnestad
  • 6,885
  • 2
  • 31
  • 43
  • can you please tell me a simple way of doing it. Storing in database is very easy, and in 3 steps its done, but storing in file is way too long :( – Aftab Aamir Jul 23 '13 at 09:28
  • @AftabAamir This IS an easy way and also the correct way of doing it. I've provided some simplified/shorter code snippets. The last two are functionally identical, they're just written in different ways. Shorter is not always better. The shortest one is the hardest to read. – Hein Andre Grønnestad Jul 23 '13 at 09:40
  • 'Shorter example without support for multiple files/error' is easy – Aftab Aamir Jul 23 '13 at 10:17
0

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
?>
ntp
  • 11
  • 1
  • @ntp Why are you using `copy` together with move_uploaded_file? – Hein Andre Grønnestad Jul 23 '13 at 09:43
  • sorry about that. I mean we can use one of two functions to copy the temporary uploaded file to server path. I wanna show this: first php uploads file to a temporary folder, then we can process this file in any way we want – ntp Aug 02 '13 at 02:20