How To Upload Picture in PHP, Upload Picture, Insert Picture In Database
How to insert picture in database? I’ve that question before, and my answers is, it almost ridiculous to insert a bunch of picture file in database when you can just upload the picture and store the picture path in the database.
Yes, just store the picture path. So, all you need to do is to create a column in database in varchar(50) to store the path.
Now i will show how to upload picture.
1. Create a form to upload the picture, like this : upload_form.php
-----------
<form action="upload_proc.php" method="post" enctype="multipart/form-data">
<table align="center" border="1" cellspacing="0" width="80%">
<tr>
<td>Upload</td>
<td><input type="file" size="40" name="pict" /></td>
</tr>
<tr><td>Rename Picture File</td><td><input type="text" name="rename" size="40" /></td></tr>
<tr><td colspan="2"><input type="submit" name="submit" value="Submit" /></td></tr>
</table>
</form>
-----------
IMPORTANT : Make sure you put enctype="multipart/form-data" in <form> tag, IF you want to upload a file.
Now, in upload_proc.php :
//-----------UPLOAD FUNCTION-------
<?
//function to check valid picture
function image_valid($type){
$file_types = array(
'image/pjpeg' => 'jpg',
'image/jpeg' => 'jpg',
'image/jpeg' => 'jpeg',
'image/gif' => 'gif',
'image/X-PNG' => 'png',
'image/PNG' => 'png',
'image/png' => 'png',
'image/x-png' => 'png',
'image/JPG' => 'jpg',
'image/GIF' => 'gif',
'image/bmp' => 'bmp',
'image/bmp' => 'BMP');
if(!array_key_exists($type, $file_types)){
return FALSE;
}
else{
return $file_types[$type];
}
}
function image_process($file,$id){
$MAX_SIZE = 200000000000; //Only 2MB maximum picture’s size can be uploaded
$upload_dir = "img/"; //Uploaded picture will be put here
$result = "TRUE";
$ext = image_valid($file['type']); //go to image_valid() function
if ($file['size'] <= $MAX_SIZE && $ext && $file['error']==0){ //if all legal
if($id){
$file['name'] = $id.".".$ext; //rename the picture, IF you want it~.
}
$temp_name = $file['tmp_name'];
$file_name = $file['name'];
$file_name = str_replace("\\","",$file_name);
$file_name = str_replace("'","",$file_name);
$file_path = $upload_dir.$file_name; //picture path store in here
$result = move_uploaded_file($temp_name, $file_path) or die("Problems with upload");
if($result){
$result = $file_path;
}else{
$result = FALSE;
}
return $result;
}
else{ //if any error, show it here~
$uploadErrors = array(
UPLOAD_ERR_OK => 'There is no error, the file uploaded with success.',
UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.',
UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded.',
UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder.',
UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.',
UPLOAD_ERR_EXTENSION => 'File upload stopped by extension.');
echo "<p align='center'><b style='color:#FF0000'>ERROR : ".$uploadErrors[$file['error']]."</b></p>";
$result = FALSE;
}
return $result;
}
?>
//-----------
$rename = $_POST['rename'];
$result = image_process($_FILES['pict'],$rename);
$result = image_process($_FILES['pict'],$rename);
if($result){
echo "<img src='$result' width='200' height='200'>";
}else{
echo "<p align=center><a href='Javascript:history.go(-1)'>Try upload again</a></p>";
}
-----------
$rename is used for rename the file. If You don’t wanna used it, then remove it.
MAKE SURE!
- test this code in localhost!!
- create a folder name “img” to put the uploaded file.
