PHP Forums Archive

Upload images and store names in a database

Tags: image upload pics database

francescoflore posted on 2009-06-28 19:31:48 #

Hi,

I am using the upload multiple files script. It works fine for uploading the pics into the forlder but I am having problems in inserting the link into the appropriate database fields. There are also other kind of fields in the form which, instead, get uploaded and inserted into the database.

The script gives no error. I am probably missing something in the script that relates to the uploading name of the pic. There a field for each pic and one for their name. Such as: Image_1, Image_2 then Image_1_name, Image_2_ame.

Sorry I now that this might sound silly to you but I have just started with php.

Please any help to undertand this.

Thank you F.

Comments / discussions

Olaf posted on 2009-06-28 20:05:11 #

For the beginning, the script doesn't store the file data in the database, just the name...
If you try the examples from the upload class you should be able to go the first steps.

Post here, if you have problems with the code, Please don't get me wrong, but I can't point a newbie to something else than the php manual (this is the way how I learned PHP)

francescoflore posted on 2009-06-28 20:14:58 #

Hi Olaf,

I am aware that the script doesn't store the file in the database, but just the name. I tought I explained that. The fact is that it doesn't. Anyway thank you for your kind help. Will look php manual. F

Olaf posted on 2009-06-28 20:40:09 #

I guess you're missing something during the database transaction.
check this post about simple debugging.

halles posted on 2009-07-01 01:56:18 #

Hei.. here is a php-script that i use, it works pretty fine...
You simply load a file up to a dir on your server and put the record in your Sql table.

script:
//******************************************
<?php
include ("upload_class.php");
//include('../config.php');
$db_prefix='fs_";
error_reporting(E_ALL);
ini_set("memory_limit", "184M");

class Foto_upload extends file_upload {

var $x_size;
var $y_size;
var $x_max_size = 520;
var $y_max_size = 320;
var $x_max_thumb_size = 100;
var $y_max_thumb_size = 80;
var $thumb_folder;
var $foto_folder;
var $larger_dim;
var $larger_curr_value;
var $larger_dim_value;
var $larger_dim_thumb_value;

var $use_image_magick = false; // switch between true and false
// I suggest to use ImageMagick on Linux/UNIX systems, it works on windows too, but it's hard to configurate
// check your existing configuration by your web hosting provider

function process_image($landscape_only = false, $create_thumb = true, $delete_tmp_file = false, $compression = 80) {
$filename = $this->upload_dir.$this->file_copy;
$this->check_dir($this->thumb_folder); // run these checks to create not existing directories
$this->check_dir($this->foto_folder); // the upload dir is created during the file upload (if not already exists)
$thumb = $this->thumb_folder.$this->file_copy;
$foto = $this->foto_folder.$this->file_copy;
if ($landscape_only) {
$this->get_img_size($filename);
if ($this->y_size > $this->x_size) {
$this->img_rotate($filename, $compression);
}
}
$this->check_dimensions($filename); // check which size is longer then the max value
if ($this->larger_curr_value > $this->larger_dim_value) {
$this->thumbs($filename, $foto, $this->larger_dim_value, $compression);
} else {
copy($filename, $foto);
}
if ($create_thumb) {
if ($this->larger_curr_value > $this->larger_dim_thumb_value) {
$this->thumbs($filename, $thumb, $this->larger_dim_thumb_value, $compression); // finally resize the image
} else {
copy($filename, $thumb);
}
}
if ($delete_tmp_file) $this->del_temp_file($filename); // note if you delete the tmp file the check if a file exists will not work
}
function get_img_size($file) {
$img_size = getimagesize($file);
$this->x_size = $img_size[1];
$this->y_size = $img_size[1];
}
function check_dimensions($filename) {
$this->get_img_size($filename);
$x_check = $this->x_size - $this->x_max_size;
$y_check = $this->y_size - $this->y_max_size;
if ($x_check < $y_check) {
$this->larger_dim = "y";
$this->larger_curr_value = $this->y_size;
$this->larger_dim_value = $this->y_max_size;
$this->larger_dim_thumb_value = $this->y_max_thumb_size;
} else {
$this->larger_dim = "x";
$this->larger_curr_value = $this->x_size;
$this->larger_dim_value = $this->x_max_size;
$this->larger_dim_thumb_value = $this->x_max_thumb_size;
}
}
function img_rotate($wr_file, $comp) {
$new_x = $this->y_size;
$new_y = $this->x_size;
if ($this->use_image_magick) {
exec(sprintf("mogrify -rotate 90 -quality %d %s", $comp, $wr_file));
} else {
$src_img = imagecreatefromjpeg($wr_file);
$rot_img = imagerotate($src_img, 90, 0);
$new_img = imagecreatetruecolor($new_x, $new_y);
imageantialias($new_img,TRUE);
imagecopyresampled($new_img, $rot_img, 0, 0, 0, 0, $new_x, $new_y, $new_x, $new_y);
imagejpeg($new_img, $this->upload_dir.$this->file_copy, $comp);
}
}
function thumbs($file_name_src, $file_name_dest, $target_size, $quality = 80) {
//print_r(func_get_args());
$size = getimagesize($file_name_src);
if ($this->larger_dim == "x") {
$w = number_format($target_size, 0, ',', '');
$h = number_format(($size[1]/$size[0])*$target_size,0,',','');
} else {
$h = number_format($target_size, 0, ',', '');
$w = number_format(($size[0]/$size[1])*$target_size,0,',','');
}
if ($this->use_image_magick) {
exec(sprintf("convert %s -resize %dx%d -quality %d %s", $file_name_src, $w, $h, $quality, $file_name_dest));
} else {
$dest = imagecreatetruecolor($w, $h);
//imageantialias($dest, false);
$src = imagecreatefromjpeg($file_name_src);
imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
imagejpeg($dest, $file_name_dest, $quality);
}
}
}

$max_size = 2048*1048; // the max. size for uploading (~1MB)
define("MAX_SIZE", $max_size);
$foto_upload = new Foto_upload;

$foto_upload->upload_dir = $_SERVER['DOCUMENT_ROOT']."/files/photo/"; // "files" is the folder for the uploaded files (you have to create these folder)
$foto_upload->foto_folder = $_SERVER['DOCUMENT_ROOT']."/files/photo/";
$foto_upload->thumb_folder = $_SERVER['DOCUMENT_ROOT']."/files/thumb/";
$foto_upload->extensions = array(".jpg",".gif"); // specify the allowed extension(s) here
$foto_upload->language = "no";
$foto_upload->x_max_size = 520;
$foto_upload->y_max_size = 320;
$foto_upload->x_max_thumb_size = 100;
$foto_upload->y_max_thumb_size = 100;

if (isset($_POST['Submit']) && $_POST['Submit'] == "Upload") {
$foto_upload->the_temp_file = $_FILES['upload']['tmp_name'];
$foto_upload->the_file = $_FILES['upload']['name'];
$foto_upload->http_error = $_FILES['upload']['error'];
$foto_upload->replace = (isset($_POST['replace'])) ? $_POST['replace'] : "n"; // because only a checked checkboxes is true
$foto_upload->do_filename_check = "n";
if ($foto_upload->upload()) {
$foto_upload->process_image(false, true, false, 80);
$foto_upload->message[] = "Processed foto: ".$foto_upload->file_copy."!"; // "file_copy is the name of the foto"
$katalog="/files/photo/";
$thumbs="/files/thumb/";
//$pro_id=123;

$x=$foto_upload->x_max_size ;
$y=$foto_upload->y_max_size ;

$xt=$foto_upload->x_max_thumb_size;
$yt=$foto_upload->y_max_thumb_size;
$proid= $_POST['id'];
$query = sprintf("SELECT th_id, filename,beskrivelse, thumbs, proid, ok FROM fs_thumb WHERE proid= $id ");

// Perform Query

$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {

}

// the code to create the test table
mysql_query("
CREATE TABLE IF NOT EXISTS fs_foto (
foto_id INT NOT NULL AUTO_INCREMENT,
filename VARCHAR( 100 ) NOT NULL,
katalog VARCHAR( 200 ) NOT NULL,
proid INT NOT NULL,
x INT NOT NULL,
y INT NOT NULL,
ok INT NULL,
PRIMARY KEY (foto_id))") or die(mysql_error());

$ok="1"; //$ok;

mysql_query(sprintf("INSERT INTO ".$db_prefix."foto SET filename = '%s', katalog = '%s', proid = '%s', x = '%s', y = '%s',ok ='%s'", $foto_upload->file_copy, $katalog,$proid, $x, $y, $ok));

}
}
$error = $foto_upload->show_error_string();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Photo-upload form</title>

<style type="text/css">
<!--
body {
text-align:center;
background-image: url();
}
label {
margin:0;
float:left;
display:block;
width:120px;
}
#main {
width:350px;
margin:0 auto;
padding:20px 0;
text-align:left;
}
-->
</style>
<link rel="nofollow" href="../includes/main.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.style17 {
font-size: 16px;
color: #FFFFFF;
}
.style19 {
color: #FF3300;
font-weight: bold;
}
.style20 {
color: #6633FF;
font-style: italic;
}
.style21 {color: #000066}
.style22 {color: #FF0000}
-->
</style>
</head>
<body>
<table width="380" border="3" cellpadding="10" cellspacing="3" bordercolor="#CCCCCC" bgcolor="#FFFFFF">
<tr>
<th width="350" nowrap bgcolor="#076BA7" scope="col"><span class="style6 style17">Opplast bilde </span></th>
</tr>
<tr>
<td><div id="main">
<h2><span class="style21"><span class="style22">Mrk: </span>Objektene må deaktiveres før en legger inn nye bilder</span></h2>
<h2>
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_size; ?>">
</h2>
<h2></h2>
<form action="mainfoto.php" method="post" enctype="multipart/form-data">
<div>
<label for="upload">Velg et foto . . </label>
<p>
<input name="upload" type="file" id="upload" value="" size="35">
</p>
<p>
<?
$db=mysql_select_db($db_name,$conn) or die("I Couldn't select your database");

$result=mysql_query("SELECT * FROM ".$db_prefix."prosjekt",$conn);

echo "<select name='id'>\n"; //optionally add MULTIPLE to allow sending to multiple addresses
echo "<option value=''>Velg prosjekt</option>\n";
while ($rows = mysql_fetch_array($result)){
if ($rows['aktiv']!== '1') {
echo "<option value='".$rows['id']."'>".$rows['project_navn']."</option>\n";
}//end while
}
echo "</select>";
?>
</p>
</div>
<table width="300" border="0" cellspacing="0" cellpadding="4">
<tr>
<td>Erstatte gammelt bilde
<input name="replace" type="checkbox" value="y" checked>

<input type="submit" name="Submit" id="Submit" value="Upload"> </td>
</tr>
</table>
<div> </div>
</form>
<p><?php echo $error; ?></p>
<h5><span class="style19">MRK!</span> Skal du leggetil/endre bildet, må Objektet
( <span class="style20">deaktiveres</span> ) først !</h5>
</div></td>
</tr>
<tr>
<td bgcolor="#076BA7"><div align="center"><font color="#0066CC" size="3">
<script type="text/JavaScript">
<!--
function MM_goToURL() { //v3.0
var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
//-->
</script>

<input name="button" type="button" onClick="MM_goToURL('parent','index.php?side=main');return document.MM_returnValue" value="Back">
</font></div></td>
</tr>
</table>
</body>
</html>
//*******************************************