will anyone please help me re-write this upload?

A

Anonymous

Guest
Posted: Mon May 17, 2004 6:20 pm Post subject: will anyone please help me re-write this upload?

--------------------------------------------------------------------------------

i have this upload code that i have been working with for the longest time and can't seem to get it to work. i have the code below and when i run the page, and try to upload the file, an error comes back saying that the upload failed. i need the upload to work.
for the upload, i would like for the image name to go to the database while the image uploads into a folder on my server. and when the file gets uploaded, i would like that file to be displayed on a webpage that i have.
this is the page i would like the image to show up on:
http://www.wootenmedia.com/wootenmusic7/guitars.php
this is the upload page that i'm working on

http://www.wootenmedia.com/wootenmusic7/admintest/add3_1_3.php

will anyone help me with re-writing this in any kind of way so that this script will run correctly.
any help would be greatly appreciated.
thank you
thanks

here's the code

Code:
<?php 

<? 
header("Cache-Control: no-store, no-cache, must-revalidate"); 
header("Cache-Control: post-check=0, pre-check=0", false); 
header("Pragma: no-cache"); 
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
?> 
<?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">"; ?> 
<!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> 
<title>Untitled Document</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<META HTTP-EQUIV='Pragma' CONTENT='no-cache' /> 
<META HTTP-EQUIV='Cache-Control' CONTENT='no-cache' /> 
</head> 
<body> 
<form action="<? $_SERVER['PHP_SELF']; ?>" method="post" ENCTYPE="multipart/form-data"> 
Choose a product category  <br> 
<select name="txtCatID" id="select"> 
               <option value="0" SELECTED>0</option> 
               <option value="1">1</option> 
               <option value="2">2</option> 
               <option value="3">3</option> 
</select><br>  
Choose product items to add to your product pages  <br> 
   <input type="hidden" name="<?echo '$txtCatID';?>"> 
<select name="txtprodItems" id="select"> 
               <option value="Guitars" SELECTED>Guitars</option> 
               <option value="Drums">Drums</option> 
               <option value="Amps">Amps</option> 
               <option value="Books">Books</option> 
</select><br> 
item name<br> 
<input type="text" name="txtItemName"><br> 
item description<br> 
<input type="text" name="txtItemDesc"><br> 
item price<br> 
<input type="text" name="txtItemPrice"><br> 
<? 
//START DB Stuff Here 
   // specify the directory where the uploaded file should end up 
   $path = "/home/username/public_html/wootenmusic7/p_imgs/"; 
// specify the filetypes allowed 
$allowed = array('image/gif','image/pjpeg','image/jpeg','image/png'); 
// specify the max filesize in bytes 
$max_size = 200000; 
if(isset($_FILES['txtItemImage'])) 
{ 
  if(is_uploaded_file($_FILES['txtItemImage']['tmp_name'])) 
  { 
   if($_FILES['txtItemImage']['size'] < $max_size) 
   { 
    if(in_array($_FILES['txtItemImage']['type'],$allowed)) 
    { 
     if(!file_exists($path . $_FILES['txtItemImage']['name'])) 
     { 
     if(move_uploaded_file($_FILES['ItemImage']['tmp_name'],$path.$_FILES['txtItemImage']['name'])) 
      { 
       $html_output = 'Upload sucessful!<br>'; 
       $html_output .= 'File Name: '.$_FILES['txtItemImage']['name'].'<br>'; 
       $html_output .= 'File Size: '.$_FILES['txtItemImage']['size'].' bytes<br>'; 
       $html_output .= 'File Type: '.$_FILES['txtItemImage']['type'].'<br>'; 
       $image = $_FILES['txtItemImage']['name'] ; 
      }else{ 
       $html_output = 'Upload failed!<br>'; 
       if(!is_writeable($path)) 
       { 
       }else{ 
        $html_output = 'an unknown error ocurred.<br>';        
       } 
      } 
     }else{ 
      $html_output = 'The file already exists<br>'; 
     } 
    }else{ 
     $html_output = 'Wrong file type<br>'; 
    } 
   }else{ 
    $html_output = 'The file is too big<br>'; 
   } 
  } 
}else{ 

  $html_output = '<form method="post" enctype="multipart/form-data" action="'.$_SERVER['PHP_SELF'].'">'; 
  $html_output .= '<input type="file" name="txtItemImage">'; 
  $html_output .= '<input type="submit" name="submit" value="upload"><input type="Reset" />'; 
  $html_output .= '</form>'; 
} 
echo '<html><head><title>Uploader</title></head><body>'; 
echo $html_output; 
echo '</body></html>'; 
   $html_output = $_POST['submit']; 
      if(isset($html_output)){ 
   include("db2.php"); 
   $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName); 
            $txtCatID = $_POST['txtCatID']; 
            $txtprodItems = $_POST['txtprodItems']; 
            $txtItemName = $_POST['txtItemName']; 
            $txtItemDesc = $_POST['txtItemDesc']; 
            $txtItemPrice = $_POST['txtItemPrice'];            
         $txtItemImage = $_FILES['txtItemImage']['name']; 
echo "$theSQL"; 
$theSQL = "insert into items (prodItems, catid, itemName, itemDesc, itemPrice, ItemImage)"; 
$theSQL = $theSQL . " values ('$txtprodItems', '$txtCatID', '$txtItemName', '$txtItemDesc', '$txtItemPrice', '$txtItemImage')"; 
    $result = mysql_query($theSQL); 
   echo "$theSQL"; 
   }else{ 
   echo "did not insert any image into database"; 
   } 
?> 



?>
 
please do not ask other to write the code, that way you won't love and understand this lovely language, please post where the problem or the error occurs... and we'll help you with that.
 
Back
Top