I need some help with this code. I am trying to upload an image from a form. I want the image to be stored into a file system and the rest of the form data and the image path to be inserted into a MySQL database.
as of now i am getting the following echo. "No File Selected" even though i have selected a file to upload.
I am really new to PHP so any help would be greatly appreciated.
Form Code:
<form enctype="multipart/form-data" name="addimage" action="imageupload.php" method="post">
<tr>
<td>
Select Category:
<br />
<?php
include 'Images_db_connect.php';
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql = mysql_query("SELECT id,category_name FROM categories ORDER BY category_name");
$row = mysql_fetch_array($sql);
?>
<select name="category_name">
<?php do{ ?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['category_name']; ?> </option>
<?php } while($row = mysql_fetch_array($sql));?>
</select>
<br />
<br />
Image Name:
<br />
<input type="text" name="image name" size="30">
<br />
<br />
Image Caption:
<br />
<textarea name="caption" rows="3" cols="30"></textarea>
<br />
<br />
Select File:
<br />
<input type="file" name="file" size="30">
<br />
<br />
<input type="submit" name="upload" value="Upload Image">
</td>
</tr>
</form>
PHP Code:
<?php
include 'Images_db_connect.php';
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
//define a maxim size for the uploaded images in Kb
define ("MAX_SIZE","75");
if(isset($_FILES['file']['name']) && $_FILES['file']['name']<>""){
$typ = $_FILES['file']['type'];
if($typ == "image/gif" || $typ == "image/png" || $typ == "image/jpeg" || $typ == "image/pgif" || $typ == "image/ppng" || $typ == "image/pjpeg"){
$uploaddir = "images/";
$uploadimages = $uploaddir.$_FILES['file']['name'];
if(move_uploaded_file($_FILES['file']['tmp_name'], $uploadimages)){
$imagename = 'image name';
$caption = 'caption';
$category = 'category_name';
$sql = "INSERT into photos (image_name, caption, image_path, category) VALUES ('$imagename', '$caption', '$uploadimages', '$category')";
mysql_query($sql) or die(mysql_error());
echo "File successfully copied";
}
else{
echo "Copy unsuccessful";
}
}
else{
echo "Incorrect file type";
}
}
else{
echo "No file selected";
}
?>