3

how to write code to upload image and save path into mysql db? i have tried but none are working.

1

1 Answer 1

2

One way is to upload image and store it in a folder on server, and save name to mysql database. Here's an example ::

First we'll create a form to upload ::

//file.html

Upload your file to the database...
<form action="upload.php" method="post" enctype="multipart/form-data" name="uploadform">
        <input type="hidden" name="MAX_FILE_SIZE" value="350000">
        <input name="picture" type="file" id="picture" size="50">
    <input name="upload" type="submit" id="upload" value="Upload Picture!">
</form>

Then we create upload.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title> Upload Image </title>
<?php 
// if something was posted, start the process... 
if(isset($_POST['upload'])) 
{ 

// define the posted file into variables 
$name = $_FILES['picture']['name']; 
$tmp_name = $_FILES['picture']['tmp_name']; 
$type = $_FILES['picture']['type']; 
$size = $_FILES['picture']['size']; 

// if your server has magic quotes turned off, add slashes manually 
if(!get_magic_quotes_gpc()){ 
$name = addslashes($name); 
} 

// open up the file and extract the data/content from it 
$extract = fopen($tmp_name, 'r'); 
$content = fread($extract, $size); 
$content = addslashes($content); 
fclose($extract);  

// connect to the database 
include "connect.php"; 

// the query that will add this to the database 
$addfile = "INSERT INTO files (name, size, type, content ) VALUES ('$name', '$size', '$type', '$content')"; 
mysql_query($addfile) or die(mysql_error()); 
     if(!empty($_FILES)) 
    { 
        $target = "upload/"; 
        $target = $target . basename( $_FILES['picture']['name']) ; 
        $ok=1; 
        $picture_size = $_FILES['picture']['size']; 
        $picture_type=$_FILES['picture']['type']; 
        //This is our size condition 
        if ($picture_size > 5000000) 
           { 
           echo "Your file is too large.<br>"; 
           $ok=0; 
           } 


        //This is our limit file type condition 
        if ($picture_type =="text/php") 
        { 
            echo "No PHP files<br>"; 
            $ok=0; 
        } 

        //Here we check that $ok was not set to 0 by an error 
        if ($ok==0) 
        { 
            Echo "Sorry your file was not uploaded"; 
        } 

        //If everything is ok we try to upload it 
        else 
        { 
            if(move_uploaded_file($_FILES['picture']['tmp_name'], $target)) 
            { 
                echo "The file ". basename( $_FILES['picture']['name']). " has been uploaded <br/>"; 
            } 
                        else 
                        { 
                            echo "Sorry, there was a problem uploading your file."; 
                        } 
                    } 
                } 


mysql_close();  

echo "Successfully uploaded your picture!";       
}else{die("No uploaded file present"); 
}   

?>
</head>

<body>

 <div align="center"> 
 <img src="upload/<?php echo $name; ?>" 
    <br /> 
    <a href="form.html">upload more images</a> 
</div> 

</body>
</html>

//getpicture.php

**//Finally the connect.php**
Sign up to request clarification or add additional context in comments.

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.