简体   繁体   中英

uploading image to mysql using php

I am new to php and trying to upload an image file in mysql database using php.I tried various tutorial but it didnot work for me. Code Snippet:-

<?php 
//connect to database. Username and password need to be changed 
mysql_connect("localhost", "root", ""); 

//Select database, database_name needs to be changed 
mysql_select_db("yelldb"); 

if (!$_POST['uploaded']){ 
//If nothing has been uploaded display the form 
?> 

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"  
ENCTYPE="multipart/form-data"> 
Upload:<br><br> 
<input type="file" name="image"><br><br> 
<input type="hidden" name="uploaded" value="1"> 
<input type="submit" value="Upload"> 
</form> 

<?php 
}else{ 
//if the form hasn't been submitted then: 

//from here onwards, we are copying the file to the directory you made earlier, so it can then be moved  
//into the database. The image is named after the persons IP address until it gets moved into the database 

//get users IP 
$ip=$_SERVER['REMOTE_ADDR']; 


//don't continue if an image hasn't been uploaded 
if (!empty($image)){ 
//copy the image to directory 

copy($image, "./temporary/".$ip.""); 

//open the copied image, ready to encode into text to go into the database 
$filename1 = "./temporary/".$_SERVER['REMOTE_ADDR']; 
$fp1 = fopen($filename1, "r"); 

//record the image contents into a variable 
$contents1 = fread($fp1, filesize($filename1)); 

//close the file 
fclose($fp1); 

//encode the image into text 
$encoded = chunk_split(base64_encode($contents1));  

//insert information into the database 
mysql_query("INSERT INTO servicelist (ImgData)"."VALUES ('$encoded')"); 

//delete the temporary file we made 
//unlink($filename1); 
}
}
?>

We don't save out the whole image in our database usually. We go through inserting the permanent of picture in our database. Use this php function

move_uploaded_file(file,newloc) 

This will move from your temporary directory to permanent directory. Then, get path from there and insert that to the database.

Typically, you wouldn't save an entire image into an SQL database. Instead, you store the on disk path or some other 'pointer' to the actual file.

Change your code to read something like the following:

//don't continue if an image hasn't been uploaded 
if (isset($_POST['image'])){ 
 $image = $_POST['image'];
 //copy the image to directory 
 $path = "/some/path";

 move_uploaded_file($image,$path);

 //store the name and path. PS: you will want to validate your input, and look 
 //at using prepared statements. 
 //Concentating values like this is NOT safe, or ideal

 $location = $path . "/" . $image
 mysql_query("INSERT INTO servicelist (ImgData) VALUES (" . $location . ")"); 
}

If however, you still wish to store the image in the SQL database, look into the blob storage type, not encoded text.

PHP move_uploaded_file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM