Advertisement

05.07.2008 at 04:21AM PDT, ID: 23382333 | Points: 500
[x]
Attachment Details

uploading files for a cms

Asked by dolorsSDG in PHP and Databases, PHP Scripting Language

Tags: , ,

I've been following some tutorials at:

first: http://www.php-mysql-tutorial.com/image-gallery/
second: http://www.php-mysql-tutorial.com/cms-php-mysql.php

I had no problem in followint them and both worked alright.

Now i'm trying to join things learnt and use the page made from the cms tutorial: http://www.solucionsdigitals.com/webs/esport7/image-gallery/cms-add.php to upload images as well.

I created the fields at the database, but I do something wrong as the files does not get uploaded.


here is the code I'm using:
Start 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:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
<?php
require_once 'library/functions.php';
?>
<html>
<head>
<title>Afegir un nou Article</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
 
</head>
 
<body>
<?php
if(isset($_POST['save']))
{
	$title   = $_POST['title'];
	$detalls = $_POST['detalls'];
	$content = $_POST['content'];
	
	$imgName   = $_FILES['fleImage']['name'];
	$tmpName   = $_FILES['fleImage']['tmp_name'];
	
	// we need to rename the image name just to avoid
	// duplicate file names
	// first get the file extension
	$ext = strrchr($imgName, ".");
	
	// then create a new random name
	$newName = md5(rand() * time()) . $ext;
 
    // the album image will be saved here
    $imgPath = NEWS_IMG_DIR . $newName;
	
	// resize all album image
	$result = createThumbnail($tmpName, $imgPath, NEWS_WIDTH);
	
	if (!$result) {
		echo "Error uploading file";
		exit;
	}
	
	
	if(!get_magic_quotes_gpc())
	{
		$title   = addslashes($title);
		$detalls = addslashes($detalls);
		$content = addslashes($content);
		
	}
	include 'library/config.php';
	include 'library/opendb.php';
	
	$query = "INSERT INTO news (title, detalls, content, image, date) VALUES ('$title', '$detalls', '$content', $newName, NOW())";
	mysql_query($query) or die('Error, ha fallat la consulta');
	include 'library/closedb.php';
	
	echo "<h4>Ja s'ha guardar l'article '$title'</h4>";
	}
?>
<form method="post">
  <table width="700" border="0" cellpadding="2" cellspacing="1" class="box" align="center">
    <tr> 
      <td width="100">T&iacute;tol</td>
      <td><input name="title" type="text" class="box" id="title"></td>
    </tr>
    <tr> 
      <td width="100">Breu presentaci&oacute;</td>
      <td><textarea name="detalls" cols="50" rows="3" class="box" id="detalls"></textarea></td>
    </tr>
    <tr> 
      <td width="100">Continguts</td>
      <td><textarea name="content" cols="50" rows="10" class="box" id="content"></textarea></td>
    </tr>
    <tr> 
            <td>Imatge</th>
            <td> <input name="fleImage" type="file" class="box" id="fleImage"></td>
        </tr>
    <tr> 
      <td colspan="2"  align="right"><input name="save" type="submit" class="box" id="save" value="- Guardar article -"><input name="btnCancel" type="button" id="btnCancel" value="- Cancel·la -" onClick="window.history.back();"></td>
    </tr>
  </table>
</form>
</body>
</html>
 
 
 
library/functions.php:
 
 
 
<?php
/*
	Upload an image and create the thumbnail. The thumbnail is stored 
	under the thumbnail sub-directory of $uploadDir.
	
	Return the uploaded image name and the thumbnail also.
*/
function uploadImage($inputName, $uploadDir)
{
	$image     = $_FILES[$inputName];
	$imagePath = '';
	$thumbnailPath = '';
	
	// if a file is given
	if (trim($image['tmp_name']) != '') {
		$ext = substr(strrchr($image['name'], "."), 1); 
 
		// generate a random new file name to avoid name conflict
		// then save the image under the new file name
		$imagePath = md5(rand() * time()) . ".$ext";
		$result    = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);
			
		if ($result) {
			// create thumbnail
			$thumbnailPath =  md5(rand() * time()) . ".$ext";
			$result = createThumbnail($uploadDir . $imagePath, $uploadDir . 'thumbnail/' . $thumbnailPath, THUMBNAIL_WIDTH);
			
			// create thumbnail failed, delete the image
			if (!$result) {
				unlink($uploadDir . $imagePath);
				$imagePath = $thumbnailPath = '';
			} else {
				$thumbnailPath = $result;
			}	
		} else {
			// the image cannot be uploaded
			$imagePath = $thumbnailPath = '';
		}
		
	}
 
	
	return array('image' => $imagePath, 'thumbnail' => $thumbnailPath);
}
 
 
 
createNewsthumb
 
/*
	Create a thumbnail of $srcFile and save it to $destFile.
	The thumbnail will be $width pixels.
*/
function createThumbnail($srcFile, $destFile, $width, $quality = 75)
{
	$thumbnail = '';
	
	if (file_exists($srcFile)  && isset($destFile))
	{
		$size        = getimagesize($srcFile);
		$w           = number_format($width, 0, ',', '');
		$h           = number_format(($size[1] / $size[0]) * $width, 0, ',', '');
		
		$thumbnail =  copyImage($srcFile, $destFile, $w, $h, $quality);
	}
	
	// return the thumbnail file name on sucess or blank on fail
	return basename($thumbnail);
}
 
/*
	Copy an image to a destination file. The destination
	image size will be $w X $h pixels
*/
function copyImage($srcFile, $destFile, $w, $h, $quality = 75)
{
    $tmpSrc     = pathinfo(strtolower($srcFile));
    $tmpDest    = pathinfo(strtolower($destFile));
    $size       = getimagesize($srcFile);
 
    if ($tmpDest['extension'] == "gif" || $tmpDest['extension'] == "jpg")
    {
       $destFile  = substr_replace($destFile, 'jpg', -3);
       $dest      = imagecreatetruecolor($w, $h);
       //imageantialias($dest, TRUE);
    } elseif ($tmpDest['extension'] == "png") {
       $dest = imagecreatetruecolor($w, $h);
       //imageantialias($dest, TRUE);
    } else {
      return false;
    }
 
    switch($size[2])
    {
       case 1:       //GIF
           $src = imagecreatefromgif($srcFile);
           break;
       case 2:       //JPEG
           $src = imagecreatefromjpeg($srcFile);
           break;
       case 3:       //PNG
           $src = imagecreatefrompng($srcFile);
           break;
       default:
           return false;
           break;
    }
 
    imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
 
    switch($size[2])
    {
       case 1:
       case 2:
           imagejpeg($dest,$destFile, $quality);
           break;
       case 3:
           imagepng($dest,$destFile);
    }
    return $destFile;
 
}
 
/*
	Check if the user is logged in or not
*/
function checkLogin()
{
	if (!isset($_SESSION['isLogin']) || $_SESSION['isLogin'] == false) {
		header('Location: login.php');
		exit;
	}
}
 
/*
	Create the link for moving from one page to another
*/
function getPagingLink($totalResults, $pageNumber, $itemPerPage = 10, $strGet = '')
{
	$pagingLink    = '';
	$totalPages    = ceil($totalResults / $itemPerPage);
	
	// how many link pages to show
	$numLinks      = 10;
 
	// create the paging links only if we have more than one page of results
	if ($totalPages > 1) {
		$self = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ;
 
		// print 'previous' link only if we're not
		// on page one
		if ($pageNumber > 1) {
			$page = $pageNumber - 1;
			if ($page > 1) {
				$prev = " <a href=\"$self?pageNum=$page&$strGet\">[Prev]</a> ";
			} else {
				$prev = " <a href=\"$self?$strGet\">[Prev]</a> ";
			}	
				
			$first = " <a href=\"$self?$strGet\">[First]</a> ";
		} else {
			$prev  = ''; // we're on page one, don't show 'previous' link
			$first = ''; // nor 'first page' link
		}
	
		// print 'next' link only if we're not
		// on the last page
		if ($pageNumber < $totalPages) {
			$page = $pageNumber + 1;
			$next = " <a href=\"$self?pageNum=$page&$strGet\">[Next]</a> ";
			$last = " <a href=\"$self?pageNum=$totalPages&$strGet\">[Last]</a> ";
		} else {
			$next = ''; // we're on the last page, don't show 'next' link
			$last = ''; // nor 'last page' link
		}
 
		$start = $pageNumber - ($pageNumber % $numLinks) + 1;
		$end   = $start + $numLinks - 1;		
		
		$end   = min($totalPages, $end);
		
		$pagingLink = array();
		for($page = $start; $page <= $end; $page++)	{
			if ($page == $pageNumber) {
				$pagingLink[] = " $page ";   // no need to create a link to current page
			} else {
				if ($page == 1) {
					$pagingLink[] = " <a href=\"$self?$strGet\">$page</a> ";
				} else {	
					$pagingLink[] = " <a href=\"$self?pageNum=$page&$strGet\">$page</a> ";
				}	
			}
	
		}
		
		$pagingLink = implode(' | ', $pagingLink);
		
		// return the page navigation link
		$pagingLink = $first . $prev . $pagingLink . $next . $last;
	}
	
	return $pagingLink;
}
 
/*
	Display the breadcrumb navigation on top of the gallery page
*/
function showBreadcrumb()
{
	if (isset($_GET['album'])) { 
		$album = $_GET['album'];
		$sql  = "SELECT al_name
                 FROM tbl_album
		         WHERE al_id = $album";
				 
		$result = mysql_query($sql) or die('Error, get album name failed. ' . mysql_error());
		$row = mysql_fetch_assoc($result);
		echo ' &gt; <a href="index.php?page=list-image&album=' . $album . '">' . $row['al_name'] . '</a>';
 
		if (isset($_GET['image'])) {
			$image = $_GET['image'];
			$sql  = "SELECT im_title
					 FROM tbl_image
					 WHERE im_id = $image";
					 
			$result = mysql_query($sql) or die('Error, get image name failed. ' . mysql_error());
			$row = mysql_fetch_assoc($result);
			
			echo ' &gt; <a href="index.php?page=image-detail&album=' . $album . '&image=' . $image . '">' . $row['im_title'] . '</a>';
		}
	}
}
 
?>
[+][-]05.07.2008 at 04:26AM PDT, ID: 21515158

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.07.2008 at 04:55AM PDT, ID: 21515336

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.08.2008 at 12:40AM PDT, ID: 21522966

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.

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