Upload image to mysql - Can someone help me please!

images php coding issues or problems here.

Moderators: macek, egami, gesf

melud
New php-forum User
New php-forum User
Posts: 4
Joined: Fri Feb 14, 2003 3:56 pm
Location: Buckinghamshire
Contact:

Upload image to mysql - Can someone help me please!

Postby melud » Fri Feb 14, 2003 4:02 pm

Can someone help please. I have followed a script (see below)from a book to allow users to upload images to a mysql database. I keep getting the following error.

Warning: open_basedir restriction in effect. File is in wrong directory in /home/myusername/public_html/test_handle.php on line 48

Warning: fopen("/tmp/phpJziNzi","r") - Operation not permitted in /home/*********/public_html/test_handle.php on line 48
File open failed!

Here is my script


include('***********');

$UPLOAD_TYPES['GIF'] = 1;
$UPLOAD_TYPES['JPG'] = 1;
$UPLOAD_TYPES['JPEG'] = 1;
$UPLOAD_SIZES['max'] = 40000;
$UPLOAD_SIZES['min'] = 0;

//Verify the file size and type

$intResult = verify_uploaded_file($HTTP_POST_FILES['upload_file']['name'],
$HTTP_POST_FILES['upload_file']['size']);

//The file does not meet criteria
if ($intResult != 1)
{
$msg_base = $HTTP_POST_FILES['upload_file']['name'] . ' is unacceptable.

';

//DIE WITH ERROR MESSAGE

if ($intResult == -1){

die ($msg_base . 'Reason: File size out of allowed range. ');

}
elseif ($intResult == -2){
die ($msg_base . 'Reason: File type not allowed. ');

}
}


//The file met criteria - verify and put in database
if (! is_uploaded_file($HTTP_POST_FILES['upload_file']['tmp_name']))
{
die("you didn't upload a file!");
}
else
{

//open file and store in $file_contents
$file = fopen($HTTP_POST_FILES['upload_file']['tmp_name'], 'r') or die("File open failed!");
$file_contents = fread($file,
filesize($HTTP_POST_FILES['upload_file']['tmp_name']))
or die("Can't read!");
fclose($file);

// Escape strange characters
$file_contents = AddSlahes($file_contents);
//put file in database
mysql_connect(MYSQL_HOST,MYSQL_USER, MYSQL_PASS) or die ("Unable to connect");

mysql_select_db(MYSQL_DBASE) or die ("Unable to select DB.");

mysql_query($SQL = "INSERT INTO $TableName SET graphic ='$file_contents'")
or die("MySQL Query Error: " . mysql_error() . "

"
. "The SQL was: $SQL

");
mysql_close();


echo $HTTP_POST_FILES['upload_file']['name'] . ' was uploaded successfully.';
}

function verify_uploaded_file($strName, $intSize)
{
if ($intSize < $GLOBALS['UPLOAD_SIZES']['min'] || $intSize > $GLOBALS['UPLOAD_SIZES']['max'])
{
return -1;
}

$arrSegments = split('[.]', $strName);
$strExtension = $arrSegments[count($arrSegments) -1];

if ($GLOBALS['UPLOAD_TYPES'][strtoupper($strExtension)] != 1){
return -2;
}
return 1;
}
?>

User avatar
Oleg Butuzov
Last Samuray
Last Samuray
Posts: 831
Joined: Sun Jun 02, 2002 3:09 am

Postby Oleg Butuzov » Tue Feb 18, 2003 6:39 am

<?
$f=fopen("image.jpg","rb");
$upload=fread($f,filesize("image.jpg"));
fclose($f);
$upload=addslashes($upload);
mysql_query("INSERT INTO `table` (`collum`) VALUES ('$upload')");
?>

you can modify this script for your needs...

melud
New php-forum User
New php-forum User
Posts: 4
Joined: Fri Feb 14, 2003 3:56 pm
Location: Buckinghamshire
Contact:

Thanks

Postby melud » Tue Feb 18, 2003 10:48 am

Thank you I will try this week

User avatar
Oleg Butuzov
Last Samuray
Last Samuray
Posts: 831
Joined: Sun Jun 02, 2002 3:09 am

Postby Oleg Butuzov » Tue Feb 18, 2003 10:04 pm

i forgot say, field for image must be BLOB-type =)


Return to “PHP coding => Images”

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest