I am getting these errors in PHP:
Warning: file_get_contents(): Filename cannot be empty in C:\xampp\htdocs\myDocs\mainProject\upload_page.php on line 97
and
Warning: getimagesize(): Filename cannot be empty in C:\xampp\htdocs\myDocs\mainProject\upload_page.php on line 99 That is not an image.
This is my code:
<div class = "splash container-fluid">
<form action = "upload_page.php" method = "POST" enctype="multipart/form-data">
File:
<input type= "file" name = "image"> <input type= "submit" value = "Upload">
</form>
</div>
<?php
//connect to database
mysql_connect("localhost", "root", "") or die(my_sql_error());
mysql_select_db("natureall")or die(my_sql_error());
//file properties
$file = $_FILES['image']['tmp_name'];
if (!isset($file))
echo "Please select an image";
else{
addslashes($image = file_get_contents($_FILES['image']['tmp_name']));
$image_name = ($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
}
if($image_size == FALSE)
echo "That is not an image";
else{
if(! $insert = mysql_query("INSERT INTO photos VALUES ('', 'image_name', '', 'image')"))
echo "Problem uploading image";
else{
$lastid = mysql_insert_id();
echo "Image uploaded.<p/>Your image:<p/><img src = get.php?id=$lastid";
}
}
?>
Also the
if (!isset($file))
echo "Please select an image";
part does not generate the echo before the image is selected, it should shouldn't it. How could this be set before the image is browsed for?
Thanks for all your help everyone! I'm getting there. There are new records in the db but the image name is not travelling only the text 'image_name' and the blob is only 5b, so that's not right. I have this db being added to from an android app where the image is stored in a dir, but I haven't worked out how to do it from a webpage yet. Here is my code as it stands now tanks to the all the help, especially @Martin:
<?php
//connect to database
mysql_connect("localhost", "root", "") or die(my_sql_error());
mysql_select_db("natureall")or die(my_sql_error());
//file properties
if($_FILES['image']['error'] == 1){
echo "The uploaded file exceeds the upload_max_filesize directive in php.ini.";
}
elseif ($_FILES['image']['error'] == 2){
echo "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";
}
elseif ($_FILES['image']['error'] == 3){
echo "The uploaded file was only partially uploaded.";
}
elseif ($_FILES['image']['error'] == 4){
echo " No file was uploaded.";
}
elseif ($_FILES['image']['error'] == 5){
echo " Don't know this one.";
}
elseif ($_FILES['image']['error'] == 6){
echo " Missing a temporary folder. Introduced in PHP 5.0.3.";
}
elseif ($_FILES['image']['error'] == 7){
echo " Failed to write file to disk. Introduced in PHP 5.1.0. ";
}
elseif ($_FILES['image']['error'] == 8){
echo " A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help. Introduced in PHP 5.2.0. ";
}
else {
//no error so uploaded something,
// therefore $_FILES['image']['tmp_name'] is a valid file.
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = ($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
}
if($image_size == FALSE){
echo "That is not an image";
}
else{
if(! $insert = mysql_query("INSERT INTO photos VALUES ('', 'image_name', '', 'image')")){
echo "Problem uploading image";
}
else{
$lastid = mysql_insert_id();{
echo "Image uploaded.<p/>Your image:<p/><img src = get.php?id=$lastid";
}
}
}
?>