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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.