Jump to content

PHP image Upload....HELP!!


Alexhoward

Recommended Posts

Hi Guys,

 

i've going round in circles trying to get an image upload to SQL

 

think i'm on the right track cant but can't get it to work

 

can anyone help me...?

 

<html>
<head>
<title>Upload Image</title>
</head>
<body>
<form action="imageupload.php" method="POST" enctype="multipart/form-data">
Name : <input type="text" name="imageName"> <br/>
Image :<input type="file" name="imageFile">
<input type="submit" value="Upload" name="func">
</form>
</body>
</html>

<?php

include ("config.php");

  /*
    This function take the image from form variables
                            */

function getImageFile($file){
$takeFile = fopen($file, "r");
$file = fread($takeFile, filesize($file));
fclose($takeFile);

return $file;
}

  /*
    We learn image type using this function
    Because we will let onlt gif, jpg and png images can be uploaded
                            */

function getfileType( $name ){
$name = explode(".", $name);
$name = array_reverse($name);
$name = $name[0];
return $name;
}

$allowedImageTypes = array("gif","jpg","png","bmp","jpeg","tiff");
if(empty($_FILES['image_file']['tmp_name'])){
echo "File not uploaded";
}
else {
$fileType = $_FILES['image_file']['name'];

if(in_array(getfileType($fileType), $allowedImageTypes)){
$fileContent = getImageFile($_FILES['imgFile']['tmp_name']);

$uploadedImage = chunk_split(base64_encode($fileContent));

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());

$query = "INSERT INTO images_table VALUES('NULL','$imgName','$uploadedImage')";

$result = mysql_query($query);

if(mysql_affected_rows() > 0){

echo "Image has been inserted succesfully";
}
else {
echo "Image can not be inserted check your submission";
}
}
else {
echo "This is not a true image type";
}
}

?>

Link to comment
https://forums.phpfreaks.com/topic/101913-php-image-uploadhelp/
Share on other sites

<?php

if($_FILES['imageFile']['name'])
{
if(!eregi('(.)+\\.(jpg$|gif$)', $_FILES['imageFile']['name']))	
{
//error.. allow only .jpg and .gif
}
elseif(round(($_FILES['imageFile']['size'] / 1024),2) > 500)
{
//error.. allow only 500kb
}
else
{
if(move_uploaded_file($_FILES['imageFile']['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/folder/image_id.jpg'))
{
unlink($_SERVER['DOCUMENT_ROOT'].'/folder/image_id.jpg');

//blah blah..

chmod($_SERVER['DOCUMENT_ROOT'].'/folder/image_id.jpg', 0644);
}
}
?>

 

its somethin like that i think ..

Link to comment
https://forums.phpfreaks.com/topic/101913-php-image-uploadhelp/#findComment-521627
Share on other sites

index.php

<html>
<head>
<title>Upload Image</title>
</head>
<body>
<form action="imageupload.php" method="POST" enctype="multipart/form-data">
Name : <input type="text" name="imageName"> <br/>
Image :<input type="file" name="imageFile">
<input type="submit" value="Upload" name="func">
</form>
</body>
</html>

 

imageupload.php

<?php

include ("config.php");

  /*
    This function take the image from form variables
                            */

function getImageFile($file){
$takeFile = fopen($file, "r");
$file = fread($takeFile, filesize($file));
fclose($takeFile);

return $file;
}

  /*
    We learn image type using this function
    Because we will let onlt gif, jpg and png images can be uploaded
                            */

function getfileType( $name ){
$name = explode(".", $name);
$name = array_reverse($name);
$name = $name[0];
return $name;
}

$allowedImageTypes = array("gif","jpg","png","bmp","jpeg","tiff");
if(empty($_FILES['image_file']['tmp_name'])){
echo "File not uploaded";
}
else {
$fileType = $_FILES['image_file']['name'];

if(in_array(getfileType($fileType), $allowedImageTypes)){
$fileContent = getImageFile($_FILES['imgFile']['tmp_name']);

$uploadedImage = chunk_split(base64_encode($fileContent));

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());

$query = "INSERT INTO images_table VALUES('NULL','$imgName','$uploadedImage')";

$result = mysql_query($query);

if(mysql_affected_rows() > 0){

echo "Image has been inserted succesfully";
}
else {
echo "Image can not be inserted check your submission";
}
}
else {
echo "This is not a true image type";
}
}

?>

Link to comment
https://forums.phpfreaks.com/topic/101913-php-image-uploadhelp/#findComment-521632
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.