Reputation: 71
I'm trying to upload image to mysql table. When image selected it's working fine. But without selecting image it's display error message. How can i fix it?
Warning: file_get_contents(): Filename cannot be empty in C:\wamp\www\firstdialnew\firstdial\adhandler.php on line 23
$stype=$_POST['stype'];
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
if($stype="")
{
echo "Please fill all the details";
}else{
$sql=mysql_query("INSERT INTO ads
(sid,stype,image,image_name)
VALUES(NULL,'$stype','$image','$image_name')");
echo "Details Successfully send";
}
Upvotes: 0
Views: 113
Reputation: 1
try this :
$stype=$_POST['stype'];
$image_name ='';
$image='';
if(!empty($_FILES['image'])) {
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
}
if($stype="")
{
echo "Please fill all the details";
}else{
$sql=mysql_query("INSERT INTO ads
(sid,stype,image,image_name)
VALUES(NULL,'$stype','$image','$image_name')");
echo "Details Successfully send";
}
Upvotes: 0
Reputation: 3520
You are not checking for $_FILES
try this
$stype=$_POST['stype'];
if(isset($_FILES['image']) ) {
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
}else{
$image = '';
$image_name = '';
}
I will suggest you to upload file in a folder and store the file name in mysql table.
Here is how you can do that
$file_path = "uploads/";
if(isset($_FILES['image']) ) {
$file_name = basename( $_FILES['uploaded_file']['name']);
$file_path = $file_path . $file_name;
//You should validate the image before upload
move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path);
}else{
$image = '';
$image_name = '';
}
//now store file name in db
$sql=mysql_query("INSERT INTO ads
(sid,stype,image,image_name)
VALUES(NULL,'$stype','$file_name','$image_name')");
Upvotes: 1
Reputation: 2438
Follow my experience, you should save image into folder (ex : public/upload/images/image_name.png) AND save image path into database. That is simple and database's size is not large
Upvotes: 1
Reputation: 158
Did you add :
<form method="POST" enctype="multipart/form-data">
Into your form (enctype="multipart/form-data")
Upvotes: 0
Reputation: 8809
you need to check/validate that image is upload or not try something like that
if (!empty($_FILES['image']['name'])) {
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
}else{
$image = '';
$image_name = '';
}
Upvotes: 3