I have this code. I put everything within <?php ?>. Because I thought that would help.
I have the code commented for easier read.
Can someone please show me where I went wrong, or what I forgot?
<?php
?>
<html>
<head>
<style>
body {
background-color:#000000;
color:#E27907;
font-family:Verdana,Arial;
font-size:10pt;
letter-spacing:2;
}
.thumbNormal {
border:4px solid #000000;
}
.thumbSelected {
border:4px solid #ff0000;
}
.prevImage {
border: 8px solid #ccc;
width: 200px;
height: 100px;
}
</style>
<script language=javascript>
var lastID = 0;
function SelectImg(id) {
if (lastID > 0) {
document.getElementById(lastID).className = "thumbNormal";
}
document.getElementById(id).className = "thumbSelected";
document.getElementById(0).src = document.getElementById(id).src;
lastID = id;
}
function LoadTrigger() {
SelectImg(1);
}
window.onload = LoadTrigger;
</script>
</head>
<body>
<?php
echo "<table border=0 width='1000'>";
echo "<tr>";
// *** This section supplies the ID and Image Name
echo "<td valign='top' width='45%' height='200'>Select Directory and Image Name<p>";
include("dd.php");
echo "<td valign='top' width='5%' height='200'> </td>";
// *** This section gets the Current Directory and Displays a Preview Image
// This Part WORKS
echo "<td valign='top' width='55%' height='200'>Current Image to Replace.<p>";
$id=$_POST['cat'];
$name=$_POST['subcat'];
$sql = "SELECT directory FROM directory WHERE DID = '$id'";
$result = mysql_query($sql) or die('A error occured: ' . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
$path = $row['directory'];
print "<img class='thumbNormal' src='http://mysite.com/images/$path/$name' width=200 onclick='SelectImg($id)'>";
print "<br>";
print $path."/".$name;
}
// *** This section selects a New Image from PC and displays a Preview Image
// This Part WORKS
echo "<td width=15> </td>";
echo "<td valign=top>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td valign=top></td>";
echo "</tr>";
echo "<tr>";
echo "<td valign='top' width='45%' height='200'>Preview New Image.<p>";
include('part1.php');
echo "</td>";
echo "<td valign='top' width='5%' height='200'> </td>";
echo "<td valign='top' width='45%' height='200'>Upload New Image.<p>";
// *** This Section Uploads and Renames the New Image
//define a maxim size for the uploaded images in Kb
define ("MAX_SIZE","100");
//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
//This variable is used as a flag. The value is initialized with 0 (meaning no error found)
//and it will be changed to 1 if an errro occures.
//If the error occures the file will not be uploaded.
$errors=0;
//checks if the form has been submitted
if(isset($_POST['Submit']))
{
//reads the name of the file the user submitted for uploading
$image=$_FILES['image']['name'];
//if it is not empty
if ($image)
{
//get the original name of the file from the clients machine
$filename = stripslashes($_FILES['image']['name']);
//get the extension of the file in a lower case format
$extension = getExtension($filename);
$extension = strtolower($extension);
//if it is not a known extension, we will suppose it is an error and will not upload the file,
//otherwise we will do more tests
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
//print error message
echo '<h1>Unknown extension!</h1>';
$errors=1;
}
else
{
//get the size of the image in bytes
//$_FILES['image']['tmp_name'] is the temporary filename of the file
//in which the uploaded file was stored on the server
$size=filesize($_FILES['image']['tmp_name']);
//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*1024)
{
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
}
// *** THIS STARTS MY PROBLEM AREA **
// *** THE $path and $name SHOULD BE PASSED TO HERE, NUT THEY DO NOT HAPPEN
// *** Can Someone let me know What is wrong?
$upload="http://mysite.com/images/".$path;
//we will give it the same name as the old file
$image_name=$name;
//the new name will be containing the full path where will be stored (images folder)
$newname=$upload . "/" . $image_name;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied)
{
echo '<h1>Copy unsuccessfull!</h1>';
echo $copied;
$errors=1;
}}}}
//If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors)
{
echo "<h1>File Uploaded Successfully! Try again!</h1>";
echo "File uploaded as: $newname";
}
// next comes the form, you must set the enctype to "multipart/frm-data" and use an input type "file"
echo "<form name='newad' method='post' enctype='multipart/form-data' action=''>";
echo "<table>";
echo "<tr><td><input type='file' name='image'></td></tr>";
echo '<tr><td><input name="Submit" type="submit" value="Upload image"></td></tr>';
echo "</table>";
echo "</form>";
echo "</table>";
?>
</body>
</html>