close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

Problem uploading images

Thread began 3/14/2010 7:38 pm by bruce379400795 | Last modified 4/07/2010 7:35 am by bruce379400795 | 1039 views | 2 replies

bruce379400795

Problem uploading images

I know this is not using DFP, but I figured I could get a good response on here. I am building a form to upload an image to a folder (c:\wamp\www\theworkoutstore\images\). I also want to store the filename of the image into my MySQL database like so: images/filename. I cannot get the filename into my database column by trying to use a hidden field and putting the filename into it to submit to the database. I figured using <?php $file ?> in the hidden field would do it, but I also need "images/" to be in front of the filename. As of right now the only thing that is inserted into my database from the hidden field is "insert". I'm lost. Any suggestions? Here is my code:

<?php require_once('Connections/connect.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$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"] == "uploadImage")) {
$insertSQL = sprintf("INSERT INTO workout (Name, Image) VALUES (%s, %s)",
GetSQLValueString($_POST['Workout_Name'], "text"),
GetSQLValueString($_POST['Image'], "text"));

mysql_select_db($database_connect, $connect);
$Result1 = mysql_query($insertSQL, $connect) or die(mysql_error());
}

// define a constant for the maximum upload size
define ('MAX_FILE_SIZE', 102400);

if (array_key_exists('upload', $_POST)) {
// define constant for upload folder
define('UPLOAD_DIR', 'C:/wamp/www/theworkoutstore/images/');
// move the file to the upload folder and rename it
// replace any spaces in original filename with underscores
// at the same time, assign to a simpler variable
$file = str_replace(' ', '_', $_FILES['image']['name']);
// convert the maximum size to KB
$max = number_format(MAX_FILE_SIZE/1024, 1).'KB';
// create an array of permitted MIME types
$permitted = array('image/gif','image/jpeg','image/pjpeg','image/png');
// begin by assuming the file is unacceptable
$sizeOK = false;
$typeOK = false;
// check that file is within the permitted size
if ($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
$sizeOK = true;
}

// check that file is of a permitted MIME type
foreach ($permitted as $type) {
if ($type == $_FILES['image']['type']) {
$typeOK = true;
break;
}
}

if ($sizeOK && $typeOK) {
switch($_FILES['image']['error']) {
case 0:
// make sure file of same name does not already exist
if (!file_exists(UPLOAD_DIR.$file)) {
// move the file to the upload folder and rename it
$success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$file);
}
else {
// get the date and time
ini_set('date.timezone', 'America/Virginia');
$now = date('Y-m-d-His');
$success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$now.$file);
}
if ($success) {
$result = "$file uploaded successfully";
}
else {
$result = "Error uploading $file. Please try again.";
}
break;
case 3:
$result = "Error uploading $file. Please try again.";
default:
$result = "System error uploading $file. Contact webmaster.";
}
}
elseif ($_FILES['image']['error'] == 4) {
$result = 'No file selected';
}
else {
$result = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png.";
}
}
?>
<!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>Administrator Section - Insert</title>
<style type="text/css">
<!--
body,td,th {
font-family: Calibri;
font-size: 11pt;
color: #000000;
}
.style1 {
color: #FFFFFF;
font-weight: bold;
font-size: 16pt;
}
.style2 {color: #FFFFFF}
-->
</style></head>

<body>
<table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="800"><img src="images/banner.png" width="1000" height="200" /></td>
</tr>
<tr>
<td bgcolor="#000033"><div align="center"><span class="style1">ADMINISTRATOR SECTION</span></div></td>
</tr>
<tr>
<td><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="20%" valign="top" bgcolor="#000033">&nbsp;</td>
<td width="80%" valign="top"><br />
<table width="60%" border="1" align="center" cellpadding="5" cellspacing="0" bordercolor="#000000">
<tr>
<td bgcolor="#CCCCCC">
<?php
// if the form has been submitted, display result
if (isset($result)) {
echo "<p><strong>$result</strong></p>";
}
?>
<form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="uploadImage" id="uploadImage">
<p>
<label for="Workout_Name">Workout Name:</label>
<input type="text" name="Workout_Name" id="Workout_Name" />
<label for="image"><br />
<br />
Upload Image:</label>
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
<input type="file" name="image" id="image" />
<input name="Image" type="hidden" id="Image" value="<?php echo $file ?>" />
</p>
<p align="center">
<input type="submit" name="upload" id="upload" value="Upload" />
</p>
<input type="hidden" name="MM_insert" value="uploadImage" />
</form>
</td>
</tr>
</table>
<br /></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...