1

I am currently working on a website that needs to upload the images of different products by its users. I am implementing it by using MySql database via php.

My code for a basic form for taking input from users is:

<form enctype="multipart/form-data" action="testimage1.php" method="post" name="changer">
<input name="MAX_FILE_SIZE" value="102400" type="hidden">
<input name="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">
</form>

My database table is:

 mysql> CREATE TABLE tbl_images (
 > id tinyint(3) unsigned NOT NULL auto_increment,
 > image blob NOT NULL,
 > PRIMARY KEY (id)
 > );

testimage1.php has the following code:-

 $username = "root";
 $password = "";
 $host = "localhost";
 $database = "thinstrokes";


 $link = mysql_connect($host, $username, $password);
 if (!$link) {
 die('Could not connect: ' . mysql_error());
 }

// Select your database
mysql_select_db ($database);

    if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 

  // Temporary file name stored on the server
  $tmpName  = $_FILES['image']['tmp_name'];  

  // Read the file 
  $fp      = fopen($tmpName, 'r');
  $data = fread($fp, filesize($tmpName));
  $data = addslashes($data);
  fclose($fp);


  // Create the query and insert
  // into our database.
  $query = "INSERT INTO tbl_images ";
  $query .= "(image) VALUES ('$data')";
  $results = mysql_query($query, $link) or die(mysql_error());

  // Print results
  print "Thank you, your file has been uploaded.";

  }
  else {
  print "No image selected/uploaded";
  }

On submitting the form I am getting an error: No image selected/uploaded

I am not getting the error... and I've already asked for this before as:

But until now I am not successful in storing the image in the database.

7
  • 1
    "on submitting the form I am getting error" -- "I am not getting the error" -- so, are you seeing an error when you submit the form or not? If you are seeing the "No image selected/uploaded" error, I would recommending var_dump($_FILES) to see if it contains the data you are expecting it to contain. Commented May 31, 2012 at 16:31
  • What's the purpose of using fopen?? Commented May 31, 2012 at 16:31
  • You script is working properly, check my answer Commented May 31, 2012 at 16:39
  • it is not working in my laptop,and i am using xampp-win32-1.7.3..and using mysql of xampp Commented May 31, 2012 at 16:43
  • @greggory.hz sry i am getting the error and that error is.."No image selected/uploaded".. Commented May 31, 2012 at 16:46

2 Answers 2

1

Your script is working just fine, This is what I test:

<?

if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 

  // Temporary file name stored on the server
  $tmpName  = $_FILES['image']['tmp_name'];  

  // Read the file 
  $fp      = fopen($tmpName, 'r');
  $data = fread($fp, filesize($tmpName));
  $data = addslashes($data);
  fclose($fp);


  // Create the query and insert

  // Print results
  print "Thank you, your file has been uploaded.";

  }
  else {
  print "No image selected/uploaded";
  }

?>

<form enctype="multipart/form-data" action="" method="post" name="changer">
<input name="MAX_FILE_SIZE" value="102400" type="hidden">
<input name="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">
</form>

And it works just fine, if you want to see it in action I can send you the link.

It must be something else that is ruining your code (*note that I removed the DB queries to avoid getting mysql errors but the script was working even with them there.

Sign up to request clarification or add additional context in comments.

2 Comments

ya i get it...it is only working for images less than 20kb..and i want to upload image of bigger size..?
do you want me to send a new piece of code that can handle uploading the files etc??
0

//Here is the solution for your problem

<form enctype="multipart/form-data" action="" method="post" name="changer">
    <input name="MAX_FILE_SIZE" value="102400" type="hidden">
    <input name="image" accept="image/jpeg" type="file">
    <input value="Submit" type="submit">
</form>
<?php

    // connection to database
    include 'includes/connection.php';
?>

<?php

    if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 
        // Temporary file name stored on the server
        $tmpName  = $_FILES['image']['tmp_name'];  
        // Read the file 
        $fp      = fopen($tmpName, 'r');
        $data = fread($fp, filesize($tmpName));
        $data = addslashes($data);
        fclose($fp);
        $result = mysql_query("INSERT INTO image (image)VALUES ( '$data')", $connection);
        if(!$result)
        {
            die("Database query failed: ". mysql_error());
        }
        // Print results

        print "Thank you, your file has been uploaded.";
    }
    else 
    {
        print "No image selected/uploaded";
    }
?>

<?php
    //close connection
    include 'includes/close.php';
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.