Jump to content

Image Loader


twilitegxa

Recommended Posts

I have this code, but when I try to test it to upload an image into the table, it just says File not uploaded. Can anyone help me figure out why? Here is my code.

 

<?php

//connect to database
$conn = mysql_connect("localhost", "root", "")
or die(mysql_error());
mysql_select_db("smrpg",$conn) or die(mysql_error());

  /*
    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 only 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");
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));

$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. Image must be a jpg, gif, or png.";
}
}

?>

 

And here's the form to submit the code:

 

<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>

Link to comment
https://forums.phpfreaks.com/topic/125247-image-loader/
Share on other sites

where is it defining the name of the input from form

 

it seems it should be in this i could be way off but isnt $file supposed to be $imageFile based on the name in the form

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

return $file;
}

Link to comment
https://forums.phpfreaks.com/topic/125247-image-loader/#findComment-647456
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.