Jump to content


Image Upload Feature Help Required


  • You cannot reply to this topic
No replies to this topic

#1 unstopabl3

    Young Padawan

  • Members
  • Pip
  • 48 posts

Posted 06 October 2005 - 04:03 PM

Hi i am using a script allow to upload images/icons when your adding tutorials. Everytime a tutorial is added/updated you have to upload an image/icon which is then saved with a random name in a folder named uploads. For example i'm writing a tutorial name "Photoshop" and i uploaded image ps.gif it will be given a random name like this one tuto_icon_1.gif and assigned to this tutorial "Photoshop" Lets say i uploaded wrong image by mistake and want to upload the right one. Ok so i go to the tutorial "Photoshop" press upload button and upload a new image. Now the new image name is tuto_icon_2.gif and is assigned to this tutorial "Photoshop" The problem is that once the new icon was uploaded which is tuto_icon_2.gif the previously uploaded image tuto_icon_1.gif still remains in the directory uploads so now i have 2 images. One which i need and the other one is garbage/trash. Now this cycle goes on whenver i need to edit the tutorial "Photoshop" i have to upload an image and a new random image is created so my Upload directory is full of junk image files i dont need like
tuto_icon_1.gif
tuto_icon_2.gif
tuto_icon_3.gif
tuto_icon_4.gif

My question is what or how can i stop that from happening ? What i want to do is once an image is uploaded and assigned to my tutorial "Photoshop" i wont have to reupload a new image everytime i make changes to that tutorial, and when i do upload a new image it deletes/owerites the previously assigned image from the folder. This way my folder will only have the image i require and use. Heres my whole code

Or even easier if you can tell me how can i make a button or a text option which once clicked on deletes the assigned image/icon to a specific tutorial
For example lets say i want to delete the icon for "Photoshop" tutorial i simply click on "Delete Icon"

Heres the whole code
include('connect.php');

switch($do)
{
case 1:

$a = mysql_query("select * from `tut_categories` order by `id` asc") or die(mysql_error());

echo "<table cellpadding='1' cellspacing='1'>";
echo "<tr><td colspan='100%'>";
if(mysql_num_rows($a) > 0)
{
echo "<input type='button' width='200px' value='Tutorials' style='width: 200px; text-align: center' onclick='location=\"?do=4\";'>";
}
else
{
echo "<input type='button' width='200px' disabled value='You must have atleast 1 category' style='width: 200px; text-align: center'>";
}

echo "</td></tr>";
echo "<tr><td><b>Icon</b></td><td><b>Category</b></td><td><b>Description</b></td><td><b>Icon</b></td><td><b>Option</b></td></tr>";
while($cat = mysql_fetch_array($a))
{
echo "<tr><td>".(($cat['icon'] != 'none' && $cat['icon'] != '') ? "<img src='uploads/{$cat[icon]}' border='0'>" : "Not Available")."</td><td><form action='?do=2&id=$cat[id]' method='post' enctype='multipart/form-data'><input type='text' name='name' value='$cat[name]'></td><td><textarea name='desc' cols='50' rows='4'>$cat[desc]</textarea></td><td><input type='file' name='icon'></td><td><input type='submit' value='Edit'></form> <input type='button' onclick='if(confirm(\"Delete this category?\")) { location=\"?do=3&id=$cat[id]\" }' value='Delete'></td></tr>";
}

echo "<tr><td colspan='100%'>".line()."</td></tr>";
echo "<tr><td colspan='100%'>";

if(mysql_num_rows($a) > 0)
{
echo "<input type='button' width='200px' value='Tutorials' style='width: 200px; text-align: center' onclick='location=\"?do=4\";'>";
}
else
{
echo "<input type='button' width='200px' disabled value='You must have atleast 1 category' style='width: 200px; text-align: center'>";
}

echo "</td></tr>";
echo "<tr><td colspan='100%'><b>Add</b></td></tr>";
echo "<tr><td><b>Icon</b></td><td><b>Category</b></td><td><b>Description</b></td><td><b>Icon</b></td><td><b>Option</b></td></tr>";

echo "<tr><td>&nbsp;</td><td><form action='?do=2' method='post' enctype='multipart/form-data'><input type='text' name='name' value=''></td><td><textarea name='desc' cols='50' rows='4'></textarea></td><td><input type='file' name='icon'></td><td><input type='submit' value='Add'></form></td></tr>";

echo "</table>";
break;
case 2:

if(!$id)
{
$iname = 'none';

if($_FILES["icon"]["name"] != "" && $hl != 'y')
{
$types = array( "jpg", "jpeg", "gif", "bmp" );
$n = 1;
$ex = false;
while(true)
{
foreach($types as $type)
{
if(file_exists("uploads/tut_icon_".$n.".".$type))
{
$ex = true;
$n++;
break;
}
}

if($ex == true)
{
$ex = false;
}
else
{
break;
}
}

$ext = explode(".", $_FILES["icon"]["name"]);

$iname = "tut_icon_{$n}.".$ext[count($ext)-1];

if(move_uploaded_file($_FILES["icon"]["tmp_name"], "uploads/".$iname))
{
$icon = true; 
}
else
{
$icon = false;
}

// End upload
}
else if($hl == 'y')
{
$iname = $hotlink;
}

mysql_query("insert into `tut_categories` (`id`,`name`,`desc`,`icon`) values ('','$name','$desc', '$iname')");

if($icon && $_FILES["icon"]["name"] != "")
{
die("<script> location='index.php?do=1'; </script>");
}
else
{
echo "Added the row, but the upload of the icon failed!<br />";
echo "<b>Uploads</b> permissions are ".(is_writable("uploads/") ? "allowed" : "rejected")."<br />";
echo "<b>Following information to debug file:<br /><pre>";
print_r($_FILES["icon"]);
echo "</pre>";
}
}
else
{
$icon = false;

if($_FILES["icon"]["name"] != "" && $hl != 'y')
{
$a = mysql_query("select * from `tut_categories` where `id`='$id'");
$cat = mysql_fetch_array($a);

if($cat['icon'] == '' || $cat['icon'] == 'none')
{
$types = array( "jpg", "jpeg", "gif", "bmp" );
$n = 1;
$ex = false;
while(true)
{
foreach($types as $type)
{
if(file_exists("uploads/tut_icon_".$n.".".$type))
{
$ex = true;
$n++;
break;
}
}

if($ex == true)
{
$ex = false;
}
else
{
break;
}
}

$ext = explode(".", $_FILES["icon"]["name"]);

$cat['icon'] = "tut_icon_{$n}.".$ext[count($ext)-1];
}

if(move_uploaded_file($_FILES["icon"]["tmp_name"], "uploads/".$cat["icon"]))
{
$icon = true; 
}
else
{
$icon = false;
}
}
else if($hl == 'y')
{
$iname = $hotlink;
$icon = "XD";
}

if($icon == true)
{
mysql_query("update `tut_categories` set `icon`='{$cat['icon']}' where `id`='$id'");
}
else if($icon == "XD")
{
mysql_query("update `tut_categories` set `icon`='{$iname}' where `id`='$id'");
}

mysql_query("update `tut_categories` set `name`='$name', `desc`='$desc' where `id`='$id'");

if(($icon && $_FILES["icon"]["name"] != "") || $_FILES["icon"]["name"] == "")
{
die("<script> location='index.php?do=1'; </script>");
}
else
{
echo "Edited the row, but the upload of the icon failed!<br />";
echo "<b>Uploads</b> permissions are ".(is_writable("uploads/") ? "allowed" : "rejected")."<br />";
echo "<b>Following information to debug file:<br /><pre>";
print_r($_FILES["icon"]);
echo "</pre>";
}
}
break;
case 3:

mysql_query("delete from `tut_categories` where `id`='$id'");

mysql_query("update `tutorials` set `catid`='0' where `catid`='$id'");

if(!@header("Location: index.php?do=1"))
{
die("<script> location='index.php?do=1'; </script>");
}

break;
// End tutorial categories
case 4:
echo "<table cellpadding='1' cellspacing='1'>";

echo "<tr><td colspan='100%'><form action='?do=4' method='post'><select name='cat'>";

$a = mysql_query("select * from `tut_categories` order by `id` asc") or die(mysql_error());
while($c = mysql_fetch_array($a))
{
echo "<option value='$c[id]' ".($c['id'] == $cat ? "selected" : "").">$c[name]</option>\n";
}

echo "</select> <input type='submit' value='Go &raquo;'></form></td></tr>";

if($cat)
{
echo "<tr><td><b>Icon</b></td><td><b>Name</b></td><td><b>Writer</b></td><td><b>Added</b></td><td><b>Options</b></td></tr>";

echo "<tr><td colspan='100%'>"; 
echo "<input type='button' width='200px' value='Add' style='width: 200px; text-align: center' onclick='location=\"?do=5&cat=$cat\";'>";
echo "</td></tr>";

$x = mysql_query("select * from `tutorials` where `catid`='{$cat}' ORDER BY `id` DESC ") or die(mysql_error());

while($tut = mysql_fetch_array($x))
{
if($tut['icon'] == '' || $tut['icon'] == 'none')
{
$icon = 'Not Available';
}
else
{
$icon = '<img src="uploads/'.$tut['icon'].'" border="0">';
}

echo "<tr><td>{$icon}</td><td>".$tut['name']."</td><td>".$tut['writer']."</td><td>".gmdate("D, d M Y H:i:s T", $tut['added'])."</td><td><a href='?do=5&id=".$tut['id']."'>Modify<br /><a href='?do=6&id=".$tut['id']."'>Delete</a></td></tr>";
} 

echo "<tr><td colspan='100%'>"; 
echo "<input type='button' width='200px' value='Add' style='width: 200px; text-align: center' onclick='location=\"?do=5&cat=$cat\";'>";
echo "</td></tr>";
}

echo "</table>";

break;
case 5:

if($id)
{
$d = mysql_query("select * from `tutorials` where `id`='$id'") or die(mysql_error());
$row = mysql_fetch_array($d);
}

?>

<td><b>Name:</b></td>
<td><input type="text" name="name" value="<?= $row['name']; ?>"></td>
</tr>
<tr>
<td><b>Description:</b></td>
<td><textarea cols="40" rows="4" name="desc"><?= $row['desc']; ?></textarea></td>
</tr> 
<tr>
<td><b>Icon:</b><? if($row) { ?><br />Re-Upload To Edit<? } ?></td>
<td><input type="file" name="icon"></td>
</tr> 
<tr>
<td><b>Writer:</b></td>
<td><input type="text" name="writer" value="<?= $row['writer']; ?>"></td>
</tr>
<tr>
<td><b>Site:</b></td>
<td><input type="text" name="site" value="<?= $row['site']; ?>"></td>
</tr>
<tr>
<td><b>Tutorial:</b></td>
<td><textarea cols='80' rows='15' name='tutorial'><?= $row['text']; ?></textarea></td>
</tr> 
<tr>
<td>&nbsp;</td>
<td>
<input type="submit" value="<? if($row) { echo "Update"; } else { echo "Add"; } ?>
</td>
</tr>
</table>
</form>
<?php
break;
case 7:

$file = $_FILES;
$icon = $file['icon'];

if(strlen($icon['name']) > 0)
{
// Upload. Gah
$iname = 'none';

if($icon["name"] != "")
{
$types = array( "jpg", "jpeg", "gif", "bmp" );
$n = 1;
$ex = false;
while(true)
{
foreach($types as $type)
{
if(file_exists("uploads/tuto_icon_".$n.".".$type))
{
$ex = true;
$n++;
break;
}
}

if($ex == true)
{
$ex = false;
}
else
{
break;
}
}

$ext = explode(".", $icon["name"]);

$iname = "tuto_icon_{$n}.".$ext[count($ext)-1];

if(move_uploaded_file($icon["tmp_name"], "uploads/".$iname))
{
$icon = true; 
}
else
{
$icon = false;
}

// End upload
}
}

if($id)
{
mysql_query("update `tutorials` set `name`='{$name}', `writer`='{$writer}', `site`='{$site}', `desc`='{$desc}', `text`='{$tutorial}', `icon`='{$iname}' where `id`='$id'") or die(mysql_error());
}
else
{
mysql_query("insert into `tutorials` values ('', '{$name}', '{$iname}', '{$desc}', UNIX_TIMESTAMP(), '{$writer}', '{$site}', '{$tutorial}', '{$cat}')") or die(mysql_error());
}


if(!@header("Location: index.php?do=4&cat=".$cat))
{
die("<script> location='index.php?do=4&cat=".$cat."'; </script>");
}
break;
case 6:

$a = mysql_query("select * from `tutorials` where `id`='$id'");
$tut = mysql_fetch_array($a);

if(mysql_num_rows($a) == 0)
{
die("This tutorial doesn't exist");
}

mysql_query("delete from `tutorials` where `id`='{$tut['id']}'");

@unlink("uploads/".$tut['icon']);

if(!@header("Location: index.php?do=4&cat=".$tut['catid']))
{
die("<script> location='index.php?do=4&cat=".$tut['catid']."'; </script>");
}

break;
// End tutorials
default:

echo "<ul>";

if($user[2][2] == 1 || $user[2][3] == 1)
{
echo '<li><a href="?do=1">Tutorials</a></li>';
}

echo "</ul>";
}

?>
Let me know if something else is required





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users