will anyone please help me re-write this upload?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

will anyone please help me re-write this upload?

Post by mikewooten »

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 ... d3_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: Select all

<?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";
	}
?>



?>
Last edited by mikewooten on Mon May 17, 2004 5:23 pm, edited 1 time in total.
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

Hopefullly this will help you

Code: Select all

//upload
@copy($_FILES['fileupload']['tmp_name'], "folder/location/where/files/are/saved/".$_FILES['file']['name'] or die("Could not upload");

//upload form
/*
<form method=post action="page.php" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="filesize...30000000">
<input type="file" name="fileupload">
<submit>
</form>
*/
mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

Post by mikewooten »

where would i put that? would i put it right before the upload form?
thanks
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

towards the top of the page
this will upload the file and put it into the location you want it to go

Code: Select all

if(isset($_GET['submit']))
{
@copy($_FILES['txtItemImage']['tmp_name'], "folder/location/where/files/are/saved/".$_FILES['file']['name'] or die("Could not upload");
}
mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

Post by mikewooten »

would it be right before if(isset($_FILES['txtItemImage'])) at the top of the page? would there be an else than goes with it or just leave it as an if statement?
thanks
mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

?

Post by mikewooten »

??
zenabi
Forum Commoner
Posts: 84
Joined: Mon Sep 08, 2003 5:26 am
Location: UK

Post by zenabi »

First, try checking to see that you can physically upload files to your server.

Follow the example found at http://uk.php.net/features.file-upload
mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

Post by mikewooten »

how would you change the permissions for the upload_tmp_dir in an .htaccess file? i emailed my hosting company and they said that i couldn't change the php.ini file because it was an server wide issue and that i had to create a .htaccess file and save it in the folder.
thanks

i tried the file upload on that link that you told me about and it gave me this error:
Warning: move_uploaded_file(/home/username/public_html/wootenmusic7/p_imgs/car49.jpg): failed to open stream: No such file or directory in /home2/username/public_html/wootenmusic7/admintest/upload_test.php on line 27

Warning: move_uploaded_file(): Unable to move '/tmp/phpsvFcfG' to '/home/username/public_html/wootenmusic7/p_imgs/car49.jpg' in /home2/username/public_html/wootenmusic7/admintest/upload_test.php on line 27

it seems like i can't upload images until i give permissions with the .htaccess file. how do you do that so that the file upload works?
thanks

the file upload script that i got from that link is:

Code: Select all

<?php




<?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" />
</head>

<body>

<form enctype="multipart/form-data" action="<? $_SERVER['PHP_SELF']; ?>" method="post">
 <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
 Send this file: <input name="userfile" type="file" />
 <input type="submit" value="Send File" />
</form>
<?
$_FILES['userfile']['name'];
$_FILES['userfile']['type'];
$_FILES['userfile']['size'];
$_FILES['userfile']['tmp_name'];
$_FILES['userfile']['error'];

$uploaddir = '/home/username/public_html/wootenmusic7/p_imgs/';
$uploadfile = $uploaddir . $_FILES['userfile']['name'];

print "<pre>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
   print "File is valid, and was successfully uploaded. ";
   print "Here's some more debugging info:\n";
   print_r($_FILES);
} else {
   print "Possible file upload attack!  Here's some debugging info:\n";
   print_r($_FILES);
}
print "</pre>";




?>


</body>
</html>





?>
mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

Post by mikewooten »

i tried using the code you gave me and it came up with those errors posted above,
also
i was tried to see if @copy would work instead of move_uploaded_file, but it still came up with the error message saying that 'upload failed'.

also i tried to upload an .htaccess file into that folder where my files are so that i could give a path to where the images are being uploaded using upload_tmp_dir, and that didn't help either. i'm still getting the same error

i used this inside of my .htaccess file:
php_value upload_tmp_dir /home/username/public_html/uploadtest/images/

what can i do for this to work? does anyone know? can someone help me with this?
thanks
mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

?

Post by mikewooten »

??
Post Reply