Advertisement

05.15.2008 at 03:23AM PDT, ID: 23404473 | Points: 500
[x]
Attachment Details

getting new file name to database when uploading image

Asked by dolorsSDG in PHP and Databases, PHP Scripting Language, MySQL Server

Tags:

I've used the following code I got from your site (from an answer) to upload and resized images and it worked great! However I need to connect the file name to a database. I'm not very goog at php and cannot do it.

I have a table called news with a field name image and a default value of prova.jpg. I also have the page connected to thedatabase, of course. I would need to get the article to which attach the file (for exmple a list of the news titles (field called title) and then change the field of that row image to its new value.

How can I do that o where can I find an example to try to do it.

Thanks a lot in advanceStart Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
<?php
 
if(isset($_POST['submit']))
 
{		
 
//make sure this directory is writable!
 
$path_thumbs = "/var/www/vhost/solucionsdigitals.com/home/html/webs/esport7/admine7/images/news/";		
 
//the new width of the resized image, in pixels.
 
$img_thumb_width = 200; // 
 
$extlimit = "yes"; //Limit allowed extensions? (no for all extensions allowed)
 
//List of allowed extensions if extlimit = yes
 
$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");		
 
//the image -> variables
 
$file_type = $_FILES['vImage']['type'];
 
$file_name = $_FILES['vImage']['name'];
 
$file_size = $_FILES['vImage']['size'];
 
$file_tmp = $_FILES['vImage']['tmp_name'];
 
//check if you have selected a file.
 
if(!is_uploaded_file($file_tmp)){
 
echo "Cal escollir una imatge. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
 
exit(); //exit the script and don't process the rest of it!
 
}
 
//check the file's extension
 
$ext = strrchr($file_name,'.');
 
$ext = strtolower($ext);
 
// the file extension is not allowed!
 
if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
 
echo "No s'accepten aquestes imatge. Nom&eacute;s jpg o gif.  <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
 
exit();
 
}
 
//so, whats the file's extension?
 
$getExt = explode ('.', $file_name);
 
$file_ext = $getExt[count($getExt)-1];
 
//create a random file name
 
$rand_name = md5(time());
 
$rand_name= rand(0,999999999);
 
//the new width variable
 
$ThumbWidth = $img_thumb_width;
/////////////////////////////////
 
// CREATE THE THUMBNAIL //
 
////////////////////////////////
//keep image type
 
if($file_size){
 
if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
 
$new_img = imagecreatefromjpeg($file_tmp);
 
}elseif($file_type == "image/x-png" || $file_type == "image/png"){
 
$new_img = imagecreatefrompng($file_tmp);
 
}elseif($file_type == "image/gif"){
 
$new_img = imagecreatefromgif($file_tmp);
 
}
 
//list the width and height and keep the height ratio.
 
list($width, $height) = getimagesize($file_tmp);
 
//calculate the image ratio
 
$imgratio=$width/$height;
 
if ($imgratio>1){
 
$newwidth = $ThumbWidth;
 
$newheight = $ThumbWidth/$imgratio;
 
}else{
 
$newheight = $ThumbWidth;
 
$newwidth = $ThumbWidth*$imgratio;
 
}
 
//function for resize image.
 
if (function_exists(imagecreatetruecolor)){
 
$resized_img = imagecreatetruecolor($newwidth,$newheight);
 
}else{
 
die("No s'ha pogut penjar. Massa gran");
 
}
 
//the resizing is going on here!
 
imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
 
//finally, save the image
 
ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
 
ImageDestroy ($resized_img);
 
ImageDestroy ($new_img);
 
}
 
echo "Ja hi ha la imatge al servidor.";
 
exit();
 
//success message, redirect to main page.		
 
$msg = urlencode("$title was uploaded! <a href=\"Resize.php\">Upload More?</a>");
 
header("Location: Resize.php?msg=$msg");
 
exit();
 
 
}else{
//if there is a message, display it
 
if(isset($_GET['msg']))
 
{
 
//but decode it first!
 
echo "<p>".urldecode($_GET['msg'])."</p>";
 
}
 
//the upload form
 
echo "
 
<form action=\"$_SERVER[PHP_SELF]\" method=\"post\"enctype=\"multipart/form-data\">\n
 
<p>File:<input type=\"file\" name=\"vImage\" /></p>\n
 
<p><input type=\"submit\" name=\"submit\" value=\"Submit\" /></p>";
 
}
?>
[+][-]05.15.2008 at 04:28AM PDT, ID: 21572375

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.15.2008 at 08:04AM PDT, ID: 21574410

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.17.2008 at 12:54AM PDT, ID: 21588333

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628