Image Insertion / Retrevial

Hi All,

I am a newbie and am in the process of trying to put together a user landing page for a site I am working on. The problem I am having is inserting an image link into the database table and then retrieving that image for display for the appropriate user. Can someone review the below information and indciate what the problem may be?

The table structure is:

Id, User_name, image

The code I am working with is below:

[php]<?php

/************* MYSQL DATABASE SETTINGS *****************

  1. Specify Database name in $dbname
  2. MySQL host (localhost or remotehost)
  3. MySQL user name with ALL previleges assigned.
  4. MySQL password

Note: If you use cpanel, the name will be like account_database
*************************************************************/

define (“DB_HOST”, “"); // set database host
define (“DB_USER”, "
”); // set database user
define (“DB_PASS”,“"); // set database password
define (“DB_NAME”,"
”); // set database name

$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(“Couldn’t make connection.”);
$db = mysql_select_db(DB_NAME, $link) or die(“Couldn’t select database”);

//Define user session
$_SESSION [‘user_name’]="*****";
$user_name=$_SESSION[‘user_name’];

//define a maxim size for the uploaded images in Kb
define (“MAX_SIZE”,“100”);

//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return “”; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}

//This variable is used as a flag. The value is initialized with 0 (meaning no error found)
//and it will be changed to 1 if an error occurs.
//If the error occurs the file will not be uploaded.
$errors=0;
//checks if the form has been submitted
if(isset($_POST[‘Submit’]))
{
//reads the name of the file the user submitted for uploading
$image=$_FILES[‘image’][‘name’];
//if it is not empty
if ($image)
{
//get the original name of the file from the clients machine
$filename = stripslashes($_FILES[‘image’][‘name’]);
//get the extension of the file in a lower case format
$extension = getExtension($filename);
$extension = strtolower($extension);
//if it is not a known extension, we will suppose it is an error and will not upload the file,
//otherwise we will do more tests
if (($extension != “jpg”) && ($extension != “jpeg”) && ($extension != “png”) && ($extension != “gif”))
{
//print error message
echo ‘

Unknown extension!

’;
$errors=1;
}
else
{
//get the size of the image in bytes
//$_FILES[‘image’][‘tmp_name’] is the temporary filename of the file
//in which the uploaded file was stored on the server
$size=filesize($_FILES[‘image’][‘tmp_name’]);

//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*1024)
{
echo ‘

You have exceeded the size limit!

’;
$errors=1;
}

//we will give an unique name, for example the time in unix time format
$image_name=time().’.’.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname=“images/”.$image_name;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES[‘image’][‘tmp_name’], $newname);
if (!$copied)
{
echo ‘

Copy unsuccessful!

’;
$errors=1;
}}}}

//If no errors registered, print the success message
if(isset($_POST[‘Submit’]) && !$errors)
{
$query = mysql_query(“UPDATE users SET image=’$newname’ WHERE id=’$_SESSION[user_id]’”);

die(“Your profile image has been uploaded Home”);
//echo “

File Uploaded Successfully!

”;
}

?>

[/php]

No one has any suggestions???

On a quick glance I can’t see anything wrong with the code.

My first thought is:
Does PHP have write access to the folder you’re writing to, and does the folder ‘images’ exist?

It would be useful if we knew what exactly is not working with it - can you post the appropriate lines from your PHP error log if you have access to it?

Sponsor our Newsletter | Privacy Policy | Terms of Service