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')
Don't you want a filename here?
move_uploaded_file($imageTemp, $_SERVER['DOCUMENT_ROOT'] . '/images/students')
@Shivamyadav but you didn't try adding it?
move_uploaded_file($imageTemp, $_SERVER['DOCUMENT_ROOT'] . '/images/students/'. $imageName);
@Shivamyadav then try debugging your code. var_dump($someVariable);exit;
This is a simple version of dd()
@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
@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 its telling me now this bool(false) that means file is not uploading..🥺
@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 which debugger sir, can u suggest me a any browser debugger?
@Shivamyadav this var_dump($someVariable);exit;
You can move it to your own dd() function if you want
@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)
}
@Shivamyadav and you checked the other variables? And checked that /images exist? And $_SERVER['DOCUMENT_ROOT'] is the root of your project?
Also if you get false,, then try
var_dump($_FILES["image"]["error"]);exit;
This should give you an integer
@Sinnbeck yea sir i have doubled checked it these above mentioned things are fine.
@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 error is zero and move uploaded is true now.. 😁
bool(true) int(0)
@Shivamyadav sweet! Then it works now. Maybe it failed due to the temporary variable.
@Sinnbeck temporary variable means which variable to point?
@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
Please sign in or create an account to participate in this conversation.