Hi
Im a newbie to php so bear with me. I have a script that will upload images to an ‘images’ folder(I got it form a book I just bought). This works OK, but I need to add a ref to the image to a mysql database too. Can annyone advise me on this?
My code so far:
[php]<DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://w3.org/TR//xhtml1-transitional.dtd”>
<fieldset><legend>Select an image file</legend>
<p><b>File:</b> <input type="file" name="upload" /></p>
</fieldset>
<input type="submit" name="submit" value="Submit" />
</div>
</form>
<?php
//Check if the form has been submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
//check for an uploaded file
if (isset($_FILES['upload']))
{
//validate the type, jpg or png
$allowed = array ('image/pjpeg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png');
if (in_array($_FILES['upload']['type'], $allowed))
{
//move the file over
if (move_uploaded_file($_FILES['upload']['tmp_name'], "../uploads/{$_FILES['upload']['name']}"))
{
echo 'The file has been uploaded
'; }//end of move if } else { echo 'Uploaded file must be an image. File not uploaded.
'; } }//end of isset if
//check for error
if ($_FILES['upload']['error'] > 0)
{
echo '<p class="error">The file could not be uploaded because:<strong>';
//print a message based upon the error
switch ($_FILES['upload']['error'])
{
case 1:
print 'The file exceeds the max_upload_size in php.ini.';
break;
case 2:
print 'The file exceeds the MAX_FILE_SIZE setting in the html form';
break;
case 3:
print 'The file has only been partially uploaded';
break;
case 4:
print 'No file was uploaded';
break;
case 6:
print 'No temporary folder was available';
break;
case 7:
print 'Unable to write to the hard disk';
break;
case 8:
print 'File upload stopped';
break;
default:
print'A system error occured';
break;
}//end of switch
print '<?strong></p>';
}//end of error if
//delete the file if it still exists
if (file_exists ($_FILES['upload']['tmp_name']) && is_file ($_FILES['upload']['tmp_name']))
{
unlink ($_FILES['upload']['tmp_name']);
}
}
?>[/php]
I’m sure this seems simple to most of you, but its giving me headaches!
Thanks…