Results 1 to 9 of 9
Thread: PHP image uploader for portfolio display
-
PHP image uploader for portfolio display
9 Jul 2012 @ 11.59 Hi all
Need help with an image uploader to display portfolio images.
Here is what is happening at the moment:
What I want is for the images to be displayed on the portfolio page. I am using an uploads folder & want the image path to be stored in the db instead of storing the actual image in the db.
The php script above the html declaration on uploader.php:
The form on the uploader.php page to upload the images from the admin section:PHP Code://Start the session so we have access to stored data
session_start();
// Define variables
$image_title = "image_title";
$image_path = "image_path";
$username = "******";
$password = "************";
$localhost = "localhost";
$database = "*************";
// Check if the value has been inserted
if(isset($_POST['image_path']))
$_SESSION['image_path'] = $_POST['image_path'];
if(isset($_POST['image_title']))
$_SESSION['image_title'] = $_POST['image_title'];
// Connect to database
mysql_connect($localhost,$username,$password);
mysql_select_db($database) or die( "Unable to select database");
mysql_close();
?>
What happens after is it doesn't go through at all or go into the uploads folder I created, neither does it show on the admin page where I will be able to delete the posts I have made.I get this messageHTML Code:<form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="form1" id="form1"> <label for="image_title"></label> <input type="text" name="image_title" id="image_title" /><br /><br /> <label for="image_path"></label> <input type="file" name="image_path" id="image_path" /> <input type="submit" name="upload" id="upload" value="Submit" /> <input type="hidden" name="MM_insert" value="form1" /> </form>
Can someone help me to see the error here. Still new in php ThanksCode:Notice: Undefined index: image_path in C:\wamp\www\ca-design\admin\uploader.php on line 42 Call Stack # Time Memory Function Location 1 0.0008 400784 {main}( ) ..\uploader.php:0 Column 'image_path' cannot be null
I also have an upload-file.php script that I tried using. When I start the upload process it says that I have an undefined index on line 23 (Notice: Undefined index: uploadfile in C:\wamp\www\ca-design\admin\upload-file.php on line 23). Why could it be telling me this?
The upload-file.php script:
This upload-file.php script I put here & took away the <?php echo $editFormAction; ?> part :PHP Code:<?php
// Start the session
session_start();
// Define variables
$image_title = "image_title";
$image_path = "image_path";
$username = "*****";
$password = "**********";
$localhost = "localhost";
$database = "**************";
// Check if the value has been inserted
//if(isset($_POST['image_title']))
//$_SESSION['image_title'] = $_POST['image_title'];
//if(isset($_POST['image_path']))
//$_SESSION['image_path'] = $_POST['image_path'];
// Connect to database
mysql_connect($localhost,$username,$password);
mysql_select_db($database) or die( "Unable to select database");
$uploaddir = './uploads/';
$file = $uploaddir . basename($_FILES['uploadfile']['name']);
$max_file_size = 20000000 ;
if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) {
echo "success";
// Create a query to insert data
$query = mysql_query ("INSERT INTO images ('image_title','image_path') VALUES ('$image_title','$image_path')");
} else {
echo "Uploading of files failed";
}
mysql_close();
?>
Cant see the error Please assist thanksHTML Code:<form action="upload-file.php" method="POST" enctype="multipart/form-data" name="form1" id="form1"> <label for="image_title"></label> <input type="text" name="image_title" id="image_title" /><br /><br /> <label for="image_path"></label> <input type="file" name="image_path" id="image_path" /> <input type="submit" name="upload" id="upload" value="Submit" /> <input type="hidden" name="MM_insert" value="form1" /> </form>
Last edited by Frinkky; 9 Jul 2012 at @ 13.13. Reason: Removed formatting, added php and html tags- PLEASE USE THESE IN FUTURE
-
9 Jul 2012 @ 13.24 On the second one, you should either replace $_FILES[uploadfile] with $_FILES[image_path] (or change the file input to name="uploadfile").
It doesn't look like you pasted all of the first script as currently it just defines server/database, a couple of if statements and finally connection to the database - there is nothing there to upload files from the form. The error states line 42 - there aren't 42 lines (at least) in what you pasted - please include it all
p.s. Please refrain from adding custom formatting to your posts and ensure you wrap php in tags, html in tags and arbitrary code (css/js etc) in tags - thanks.
-
9 Jul 2012 @ 13.57 Thanks will post the rest of the code now
-
9 Jul 2012 @ 14.08 The uploader.php script
<?php require_once('../Connections/connection.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO images (image_title, image_path) VALUES (%s, %s)",
GetSQLValueString($_POST['image_title'], "text"),
GetSQLValueString($_POST['image_path'], "text"));
mysql_select_db($database_connection, $connection);
$Result1 = mysql_query($insertSQL, $connection) or die(mysql_error());
$insertGoTo = "admin_index.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
<?php
//Start the session so we have access to stored data
session_start();
// Define variables
$image_title = "image_title";
$image_path = "image_path";
$username = "******";
$password = "**********";
$localhost = "localhost";
$database = "************";
// Check if the value has been inserted
if(isset($_POST['image_path']))
$_SESSION['image_path'] = $_POST['image_path'];
if(isset($_POST['image_title']))
$_SESSION['image_title'] = $_POST['image_title'];
$query = mysql_query ("INSERT INTO images('image_path','image_title') VALUES ('$image_path','$image_title')");
// Connect to database
mysql_connect($localhost,$username,$password);
mysql_select_db($database) or die( "Unable to select database");
mysql_close();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CA DESIGN - Uploader</title>
<link href="admin_styles.css" rel="stylesheet" type="text/css" />
<link rel="Shortcut Icon" href="favicon.ico">
</style>
<style type="text/css">
a:link {
color: #b9b9ba;
text-decoration: none;
}
a:visited {
text-decoration: none;
color: #b9b9ba;
}
a:hover {
text-decoration: none;
color: #333;
}
a:active {
text-decoration: none;
}
a {
font-family: "AvantGarde Bk BT";
font-weight: bold;
}
body,td,th {
color: #272727;
}
</style>
</head>
<body><div id="container">
<div id="wrapper">
<?php require_once("includes/inc_header_container.php"); ?>
<div id="content">
<div id="uploader_1">
<h4>Corporate Identity work</h4>
<form action="upload-file.php" method="POST" enctype="multipart/form-data" name="form1" id="form1">
<label for="image_title"></label>
<input type="text" name="image_title" id="image_title" /><br /><br />
<label for="image_path"></label>
<input type="file" name="image_path" id="image_path" />
<input type="submit" name="upload" id="upload" value="Submit" />
</form>
</div>
<div id="uploader_2">
<h4>Print design</h4>
</div>
<div id="uploader_3">
<h4>Web design</h4>
</div>
</div>
<?php require_once("includes/inc_footer_container.php"); ?>
</div><!-- Wrapper -->
</div><!-- Container -->
</body>
</html>
__________________________________________________ ____________________
The portfolio.php script where I display my work
<?php
// Start the session
session_start();
// Define variables
$temp = "temp";
?>
<?php require_once('Connections/connection.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_connection, $connconnection);
$query_getImages = "SELECT image_title, image_path FROM images ORDER BY id DESC";
$getImages = mysql_query($query_getImages, $connection) or die(mysql_error());
$row_getImages = mysql_fetch_assoc($getImages);
$totalRows_getImages = mysql_num_rows($getImages);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CA DESIGN - Portfolio</title>
<meta name="keywords" content="ca-design, cape town web designer, cape town graphic designer, web designer in cape town south africa, graphic designer in cape town south africa, freelance graphic designer, corporate identity design, layout design, creative brochure design, website design, web banners, poster design, logo design, letterheads, business cards, stationery, print design, graphic design, newsletter design, web design, info graphics, graphic design in cape town, design cape town, cape town web design, south african graphic designers, freelance web designer, graphic designer portfolio, cape town design, graphic designers, freelance graphic designer" />
<meta name="description" content="Digital Design Portfolio of Crispian Anyster - CA DESIGN is a small design firm specialising in graphic design & web design services offered to small & growing businesses " />
<link href="public.css" rel="stylesheet" type="text/css" />
<link rel="Shortcut Icon" href="favicon.ico">
<script type="text/javascript" src="js/mootools.js"></script>
<script type="text/javascript" src="js/slimbox.js"></script>
<link rel="stylesheet" href="js/slimbox.css" type="text/css" media="screen" />
<style type="text/css">
a:link {
color: #b9b9ba;
text-decoration: none;
}
a:visited {
text-decoration: none;
color: #b9b9ba;
}
a:hover {
text-decoration: none;
color: #333;
}
a:active {
text-decoration: none;
}
a {
font-family: "AvantGarde Bk BT";
font-weight: bold;
}
body,td,th {
color: #C8C8C8;
}
</style>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-32617666-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body><div id="container">
<div id="wrapper">
<?php require_once("includes/inc_header_container.php"); ?>
<div id="portfolio">
<?php echo '<h3 style="color: #222120" >CORPORATE IDENTITY DESIGN</h3>'; ?>
<?php do { ?>
<table width="200">
<tr>
<th scope="col"><a href="<?php echo $row_getImages['image_path']; ?>"lightbox-portfolio"
title="<?php echo $row_getImages['image_title']; ?>"><?php echo "<img src= '$temp' />";'width="200" height="200" border="2"'; ?></a></th>
</tr>
</table>
<?php } while ($row_getImages = mysql_fetch_assoc($getImages)); ?>
<?php echo '<h3 style="color: #222120" >PRINT DESIGN</h3>'; ?>
<?php echo '<h3 style="color: #222120" >WEB BANNER DESIGN</h3>'; ?>
<?php echo '<h3 style="color: #222120">PERSONAL ARTWORK</h3>'; ?>
</div>
<div id="backtotop"><a href="#"><img src="b2t-button.png" width="24" height="25" /></a></div>
<?php require_once("includes/inc_footer_container.php"); ?>
</div><!-- Wrapper -->
</div><!-- Container -->
</body>
</html>
<?php
mysql_free_result($getImages);
?>
__________________________________________________ _____________________
The upload-file.php script from the form on uploader.php
<?php
// Start the session
session_start();
// Define variables
$image_title = "image_title";
$image_path = "image_path";
$username = "******";
$password = "**********";
$localhost = "localhost";
$database = "*************";
// Check if the value has been inserted
if(isset($_POST['image_title']))
$_SESSION['image_title'] = $_POST['image_title'];
if(isset($_POST['image_path']))
$_SESSION['image_path'] = $_POST['image_path'];
// Connect to database
mysql_connect($localhost,$username,$password);
mysql_select_db($database) or die( "Unable to select database");
$uploaddir = './uploads/';
$file = $uploaddir . basename($_FILES['uploadfile']['name']);
$max_file_size = 20000000 ;
if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) {
echo "success";
// Create a query to insert data
$query = mysql_query ("INSERT INTO images ('image_title','image_path') VALUES ('$image_title','$image_path')");
} else {
echo "Uploading of files failed";
}
mysql_close();
?>Last edited by Crispian; 9 Jul 2012 at @ 14.36. Reason: add code
-
9 Jul 2012 @ 14.25 Okay so I changed the uploadfile to image_path on upload-file.php & this is the error I received:
[FONT=Times New Roman]Warning: move_uploaded_file(./uploads/bottle&label_design.jpg) [[/FONT]function.move-uploaded-file[FONT=Times New Roman]]: failed to open stream: No such file or directory in C:\wamp\www\ca-design\admin\upload-file.php on line [/FONT]25
I am not sure as to what this means but it seems that it is not connecting to the databse to store the file_path or store the image in the designated folder.
-
12 Jul 2012 @ 08.08 Hi can anyone please help me with this?
-
12 Jul 2012 @ 14.59 Looks like it's not finding the file. Check you have the right directory. It's possible that the "./uploads/" is not where you think it is!
Try to simplify your script by hard-coding a file location for testing. Simplifying will help you find out what's causing the problem.
-
18 Jul 2012 @ 17.53 I've managed to upload images into the uploads directory. Changed some things in upload-file.php. But the problem lies with the information of the image not going into the database. I don't see the information of the image in the DB which was uploaded. Any suggestions as to why this is happening?
Also I am not get anything displayed on the admin page of what I just uploaded and that is a result of the information not going through to the database?
These uploads will be displayed on my portfolio page in the end of it all.
My code for uploader.php
My code on upload-file.php
<?phpsession_start();
// set variables
$file = 'file';
$image_path = 'image_path';
$title = 'title';
$size = 'size';
$type = 'type';
$username = 'username';
$password = 'password';
$localhost = 'localhost';
$database = 'database';
// connect to the database
mysql_connect($localhost, $username, $password);
mysql_select_db($database) or die( "Unable to select database");
$query = mysql_query("INSERT INTO images ('title','image_path','size','type') VALUES ('$title','$image_path','$size','$type','$file')") ;
mysql_close();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CA DESIGN - Uploader</title>
<link href="admin_styles.css" rel="stylesheet" type="text/css" />
<link rel="Shortcut Icon" href="favicon.ico">
</style>
<style type="text/css">
a:link {
color: #b9b9ba;
text-decoration: none;
}
a:visited {
text-decoration: none;
color: #b9b9ba;
}
a:hover {
text-decoration: none;
color: #333;
}
a:active {
text-decoration: none;
}
a {
font-family: "AvantGarde Bk BT";
font-weight: bold;
}
body,td,th {
color: #272727;
}
</style>
</head>
<body><div id="container">
<div id="wrapper">
<?php require_once("includes/inc_header_container.php"); ?>
<div id="content">
<div id="uploader_1">
<h4>Corporate Identity work</h4>
<form action="../upload-file.php" method="POST" enctype="multipart/form-data" name="form1" id="form1">
<input name="title" type="text" value="" />
<input type="hidden" name="MAX_FILE_SIZE" value="20000000" />
<input type="file" name="userfile" id="file" />
<input type="submit" name="upload" id="upload" value="Submit" />
<input type="reset" name="reset" id="reset" value="Reset" />
</form>
</div>
<div id="uploader_2">
<h4>Print design</h4>
</div>
<div id="uploader_3">
<h4>Web design</h4>
</div>
</div>
<?php require_once("includes/inc_footer_container.php"); ?>
</div><!-- Wrapper -->
</div><!-- Container -->
</body>
</html>
<?php
// Start the session
session_start();
// Define variables
$title = "title";
$image_path = "image_path";
$size = 'size';
$type = 'type';
$username = "username";
$password = "password";
$localhost = "localhost";
$database = "database";
// Check if the value has been inserted
//if(isset($_POST['title']))
//$_SESSION['title'] = $_POST['title'];
//if(isset($_POST['image_path']))
//$_SESSION['image_path'] = $_POST['image_path'];
// Connect to database
mysql_connect($localhost,$username,$password);
mysql_select_db($database) or die( "Unable to select database");
$uploaddir = './uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
$max_file_size = 20000000 ;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File upload successful";
// Create a query to insert data
$query = mysql_query ("INSERT INTO images ('title','image_path','size','type') VALUES ('$title','$image_path','$size','$type')");
} else {
echo "Uploading of files failed";
}
mysql_close();
?>
-



LinkBack URL
About LinkBacks












Displaying Search Results
Agree with you.... Generally, we don't have control over display...