Here is the form

<form action="" method="POST" enctype="multipart/form-data">
            <input type="file" name="file"/>
            <br />
            <input type="submit"/>
        </form>

and Here is the script

<?php
$hostname = "hostname";
$db_user = "username"; 
$db_password = "passwrd";
$database = "db";
$db_table = "dbtable";
$db = mysql_connect($hostname, $db_user, $db_password); 
mysql_select_db($database,$db);
$uploadDir = 'listings/';
if(isset($_POST['submit']))
{
$fileName = $_FILES['Photo']['name'];
$tmpName  = $_FILES['Photo']['tmp_name'];
$fileSize = $_FILES['Photo']['size'];
$fileType = $_FILES['Photo']['type'];
$filePath = $uploadDir . $fileName;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
    $filePath = addslashes($filePath);
}
$query = "UPDATE $db_table SET `image_name` = '$fileName' WHERE `id` = 1";
mysql_query($query) or die('Error, query failed'); 
}
?>

Ok, you get a blank page because the submit input field lacks of the name attribute: since you're testing for $_POST['submit'] you have to add this attribute; second problem: the name attribute of the file input field is file instead of Photo as expected in your $_FILES['Photo'].

So, in order to work change the form to:

<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="Photo"/>
    <br />
    <input type="submit" name="submit" />
</form>

Ok, I did that but now I get these set of errors.

"Warning: move_uploaded_file(listings/no_photo.gif): failed to open stream: No such file or directory in C:\Program Files (x86)\EasyPHP-12.1\www\html files\upload_image.php on line 17

Warning: move_uploaded_file(): Unable to move 'C:\Program Files (x86)\EasyPHP-12.1\tmp\php9A88.tmp' to 'listings/no_photo.gif' in C:\Program Files (x86)\EasyPHP-12.1\www\html files\upload_image.php on line 17
Error uploading file"

Does the script automatically create the folder for the image or will it just upload file path to database? to me it seems as if the script will do both..

No, you have to create the directory in the server, and make sure it is writable by the script: this means writable by the user which runs the server, for Apache usually this is www-data. But you can check it by running phpinfo().

If you are using a remote server you can create the directory through ftp or through mkdir() function. In the first case the directory will be owned by the ftp user, in the second case by the Apache user.

This translates into two different permissions settings for the directory: 777 or 755. If you want to use mkdir then run:

$dir = './listings/';
if( ! file_exists($dir))
{
    mkdir($dir, 0755);
}

@cereal, When I run phpinfo(); what do I need to look for? I'm not using a remote server. It's local through a program called EasyPHP.

Ok, it's easier: create the folder listings and make sure is not read-only, if I remember good, you can check it by right clicking and selecting properties. After that it should work fine.

Usually in remote servers configurations, you should check for User/Group in the Configuration > apache2handler section.

@cereal, I fixed the issue. it was the foward slash in the variable "$uploadDir". it is stored into the database! Now..how to I show some of the content and the image onto a webpage..in a list preferably? Sorry for your trouble, I know you probably get tired of me asking so many questions..

@cereal, Don't I have to execute the image to output as an image a certain way? I mean I understand how to output regular data text onto a web page. That's not hard at all..I was just curious about displaying the image correctly as an image.

You can do what LastMitch suggested previously. In this case, Apache will handle the mime of the file and take care to send the appropriate headers to the browser.

Sometimes, however, you may want to hide the source directory and disallow direct access; in these cases you can create a script that displays the image:

<?php

$path = './hidden/path/';
$name = pathinfo(trim($_GET['name'],'/'), PATHINFO_BASENAME);

if( ! file_exists($path.$name))
{
    die('Not found');
}

$file = file_get_contents($path.$name);

# set mime type for jpeg files
header("Content-Type: image/jpeg");
echo $file;

?>

And use it like this:

<img src="/script/index.php?name=image001.jpg" />

More can be done to block the access to the source directory and to hide the script name by using mod_rewrite in .htaccess files, but this is off topic.

Anyway it's not a good solution unless you really need it, because it will force the server to include the PHP engine in the request of a static file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.