Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shivamyadav's avatar

php how to store a image in the folder?

my image input

 $imageName = isset($_FILES["image"]["name"]) ? $_FILES['image']['name'] : "";
    $imageType = isset($_FILES["image"]["type"]) ? $_FILES['image']['type'] : "";
    $imageSize = isset($_FILES["image"]["size"]) ? $_FILES['image']['size'] : "";
    $imageTemp = isset($_FILES["image"]["temp_name"]) ? $_FILES['image']['temp_name'] : "";

image name is storing in the database but not the file is uploading to the folder :(

file path to store image root/images/students and the images ablove code is in this dir path root/validation/studentValidation/create.php

uploading the image code to the folder

move_uploaded_file($imageTemp, $_SERVER['DOCUMENT_ROOT'] . '/images/students')
0 likes
22 replies
Sinnbeck's avatar

Don't you want a filename here?

move_uploaded_file($imageTemp, $_SERVER['DOCUMENT_ROOT'] . '/images/students')
 
Sinnbeck's avatar

@Shivamyadav but you didn't try adding it?

move_uploaded_file($imageTemp, $_SERVER['DOCUMENT_ROOT'] . '/images/students/'. $imageName);
Sinnbeck's avatar

@Shivamyadav then try debugging your code. var_dump($someVariable);exit;

This is a simple version of dd()

1 like
Shivamyadav's avatar

@Sinnbeck code

 $file = move_uploaded_file($imageTemp, $_SERVER['DOCUMENT_ROOT'] . '/images/' . $imageName);
        echo var_dump($file);
        die();

error Uncaught mysqli_sql_exception: Data too long for column 'image' at row 1

Sinnbeck's avatar

@Shivamyadav that error is related to mysql, but you don't show any mysql code? And the error means that you are trying to put too long a string in the image column. Be sure that it's the filename and not the actual image

Sinnbeck's avatar

@Shivamyadav yes. Then debug it? Use the debugger to check the content of $_FILES. And the path. And so on an so on

And double check that the images folder exists

Sinnbeck's avatar

@Shivamyadav this var_dump($someVariable);exit;

You can move it to your own dd() function if you want

Shivamyadav's avatar

@Sinnbeck here it is

["image"]=>
  array(6) {
    ["name"]=>
    string(15) "shivam-dell.jpg"
    ["full_path"]=>
    string(15) "shivam-dell.jpg"
    ["type"]=>
    string(10) "image/jpeg"
    ["tmp_name"]=>
    string(45) "C:\Users\shiva\AppData\Local\Temp\php22DE.tmp"
    ["error"]=>
    int(0)
    ["size"]=>
    int(5300)
  }
Sinnbeck's avatar

@Shivamyadav and you checked the other variables? And checked that /images exist? And $_SERVER['DOCUMENT_ROOT'] is the root of your project?

Sinnbeck's avatar

Also if you get false,, then try

var_dump($_FILES["image"]["error"]);exit;

This should give you an integer

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@Shivamyadav 0 means no error? (in this case 0 does not mean false. It's the error code. 0 = no error)

Try this

 $file = move_uploaded_file($_FILES["image"]["tmp_name"], $_SERVER['DOCUMENT_ROOT'] . '/images/' . $imageName);
        echo var_dump($file, $_FILES["image"]["error"]);
        die();
Sinnbeck's avatar

@Shivamyadav like this

$file = move_uploaded_file($_FILES["image"]["tmp_name"], $_SERVER['DOCUMENT_ROOT'] . '/images/' . $imageName); //works

$file = move_uploaded_file($imageTemp, $_SERVER['DOCUMENT_ROOT'] . '/images/' . $imageName); //does not work 
1 like

Please or to participate in this conversation.