Jump to content

IMAGE UPLOAD


daebat

Recommended Posts

I'm having some trouble with an image upload form.  I have a few instances of this code that works in other areas of my site so I'm pretty sure there is just something small that I'm missing:

 

The Form:

 

<?
include("../include/session.php");
?>
<html>
<head>
<title>Template Configuration</title>
<link rel="stylesheet" type="text/css" href="../css/backend.css">
</head>
<body>
<?
if($session->logged_in){

$data = mysql_query("SELECT * FROM template WHERE id = '1'") or die(mysql_error());

while($info = mysql_fetch_array( $data ))
{
echo "
<h1 style='text-align:center;'>Template Configuration</h1>
<div id='textedit'>
<form method='post' action='templateprocess.php' enctype='multipart/form-data'>
<table>
<tr><td><h2>Header Image</h2></td></tr>	
 <tr><td colspan='2' style='text-align:center'><img src='../upload/template/".$info['headerimg'] ."'><br /><br /></tr></td>
<tr>
<td>Image Upload:</td>
<td>
<input type='file' name='headerimg'>
</td>
</tr>
</table  
            <input type='hidden' name='id' value='1'>
            <input TYPE='submit' name='upload' title='Add data to the Database' value='Submit'/>
          </form>
          <a href='../main.php'><img src='../images/backButton.jpg'></a>
          </div>
          ";
}
}
else{
echo "[<a href='../main.php'>Please Login</a>]   ";
}
?>	
</body>
</html>

 

The Processor:

 

<?
include("../include/session.php");
?>

<html>
<head>
<title>Template Configuration</title>

<link rel="stylesheet" type="text/css" href="../css/backend.css">

</head>
<body>
<div id='process'>
<?
if($session->logged_in){

$target = "/path/to/folder/upload/template/";

if ($headerimg != ''){
$headerimg = ($_FILES['headerimg']['name']);
foreach($_FILES as $file) {
    move_uploaded_file($file['tmp_name'], $target . $file['name']);
}
mysql_query("UPDATE template SET headerimg ='$headerimg' WHERE id ='1'");
}
?>
<p>Update Successful... <a href="../main.php">click here</a> to return to the administration area.</p>
<?php
}
else{
echo "[<a href='../main.php'>Please Login</a>]   ";
}
?>	</div>
</body>
</html>

 

My database has a table named 'template' with two fields of 'id' and 'headerimg'.  I have inserted into the table (id) '1' and (headerimg) 'header-image.png' and is reading this as a preview above.  I can't, however, get the 'headerimg' field to update and the image never uploads into my template folder.

Link to comment
Share on other sites

sorry I'm not very good with PHP yet.  what's wrong with that code?  I have the same code working in another area of the cms.

 

Here is a working version:

 

$ordering=$_POST['ordering'];
$title=$_POST['title'];
$id=$_GET['id'];


if ($image != ''){
$image = ($_FILES['image']['name']);
foreach($_FILES as $file) {
    move_uploaded_file($file['tmp_name'], $target . $file['name']);
}
mysql_query("UPDATE tablename SET image ='$image' WHERE id ='$id'");
}
if ($thumb != ''){
$thumb = ($_FILES['thumb']['name']);
foreach($_FILES as $file) {
    move_uploaded_file($file['tmp_name'], $target . $file['name']);
}
mysql_query("UPDATE tablename SET thumb ='$thumb' WHERE id ='$id'");
}



mysql_query("UPDATE tablename SET ordering = '$ordering', title = '$title' WHERE id ='$id' ");

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.