Jump to content

[SOLVED] Problem with File Upload.


centerwork

Recommended Posts

I have been racking my brain on script for the last two days. The files don't seem to pass through when the form is submitted. It keeps stopping of this portion of the script:

 

if (!is_uploaded_file($file_tmp)) {

              //print error message and file number.

              echo "File $i: Not selected.<br>";

 

Here is the complete php script:

<?php 
//Establishes A Connection With The Database
require ($_SERVER["DOCUMENT_ROOT"] . "/ssl/db_config.php");
$connection = mysql_connect($db_host, $db_user, $db_password, $db_name) or die ("error connecting");
mysql_select_db("ccsbiz"); 

$page = $_GET['p'];
$a = $_GET['a'];
$sel_id = $_GET['id'];
$submit = $_POST['submit'];
//number of files to upload.
$num_files = 2;

if($submit == "Add")
	{
	$status = $_POST['status'];
	$type = $_POST['type'];
	$category = $_POST['category'];
	$name = $_POST['name'];
	$model = $_POST['model'];
	$serial = $_POST['serial'];
	$description = $_POST['description'];
	$manual = $_POST['manual'];
	$price = $_POST['price'];
	$lease = $_POST['lease'];
	$date = time();

	$insert = "INSERT INTO products (id, status, type, category, manual, name, model, description, price, lease) VALUES (null, '$status', '$type', '$category', '$name', '$model', '$description', '$manual', '$price', '$lease')";

	$results = mysql_query($insert) or die (mysql_error());

	if($results == "TRUE")
		{
		echo'<h3>Your new product has been added.</h3>';
		}
	}

if($submit == "Update")
	{
	$sel_id = $_POST['sel_id'];
	$status = $_POST['status'];
	$type = $_POST['type'];
	$category = $_POST['category'];
	$name = $_POST['name'];
	$model = $_POST['model'];
	$serial = $_POST['serial'];
	$description = $_POST['description'];
	$manual = $_POST['manual'];
	$price = $_POST['price'];
	$lease = $_POST['lease'];
	$date = time();

	$table = "products";

	$insert = "UPDATE $table SET $table.status='$status', $table.type='$type', $table.category='$category', $table.name='$name', $table.model='$model', $table.serial='$serial', $table.description='$description', $table.manual='$manual', $table.price='$price', $table.lease='$lease', $table.date='$date' WHERE id = '$sel_id' ";

	$results = mysql_query($insert) or die (mysql_error());

	if($results == "TRUE")
		{
		echo'<h3>Your product has been updated.</h3>';
		}

	//This is were your image is uploaded.
	###########################################
	#----------Upload Multiple Files----------#
	#----------Multi-files Uploader-----------#
	#-------------Multi-Uploader -------------#
	###########################################
	/*=========================================\
	Author      :  Mohammed Ahmed(M@@king)    \\
	Version     :  1.0                        \\
	Date Created:  Aug 20  2005               \\
	----------------------------              \\
	Last Update:   Aug 31 2005                \\
	----------------------------              \\
	Country    :   Palestine                  \\
	City       :   Gaza                       \\
	E-mail     :   m@maaking.com              \\
	MSN        :   m@maaking.com              \\
	AOL-IM     :   maa2pal                    \\
	WWW        :   http://www.maaking.com     \\
	Mobile/SMS :   00972-599-622235           \\
											  \\
	===========================================\
	------------------------------------------*/
	//upload directory.
	//change to fit your need eg. files, upload .... etc.
	$upload_dir = "/home/content/c/c/s/ccsbiz/html/upload_images/";
	//the file size in bytes.
	$size_bytes =5242880; //2097152 bytes = 2mb.
	//Extensions you want files uploaded limited to.
	$limitedext = array(".gif",".jpg",".jpeg",".png",".pdf");


	     //check if the directory exists or not.
   if (!is_dir("$upload_dir")) {
      die ("Error: The directory <b>($upload_dir)</b> doesn't exist");
   }
   //check if the directory is writable.
   if (!is_writeable("$upload_dir")){
      die ("Error: The directory <b>($upload_dir)</b> is NOT writable, Please CHMOD (777)");
   }


//if the form has been submitted, then do the upload process
//infact, if you clicked on (Upload Now!) button.
if ($submit == "Update"){

       echo "<h3>Upload results:</h3>";

       //do a loop for uploading files based on ($num_files) number of files.
       for ($i = 1; $i <= $num_files; $i++) {
           //define variables to hold the values.
           $new_file = $_FILES['file'.$i.''];
           $file_name = $new_file['name'];
           //to remove spaces from file name we have to replace it with "_".
           $file_name = str_replace(' ', '_', $file_name);
           $file_tmp = $new_file['tmp_name'];
           $file_size = $new_file['size'];

           #-----------------------------------------------------------#
           # this code will check if the files was selected or not.    #
           #-----------------------------------------------------------#
           if (!is_uploaded_file($file_tmp)) {
              //print error message and file number.
              echo "File $i: Not selected.<br>";
           }else{
                 #-----------------------------------------------------------#
                 # this code will check file extension                       #
                 #-----------------------------------------------------------#

                 $ext = strrchr($file_name,'.');
                 if (!in_array(strtolower($ext),$limitedext)) {
                    echo "File $i: ($file_name) Wrong file extension. <br>";
                 }else{
                       #-----------------------------------------------------------#
                       # this code will check file size is correct                 #
                       #-----------------------------------------------------------#

                       if ($file_size > $size_bytes){
                           echo "File $i: ($file_name) Faild to upload. File must be <b>". $size_bytes / 1024 ."</b> KB. <br>";
                       }else{
                             #-----------------------------------------------------------#
                             # this code check if file is Already EXISTS.                #
                             #-----------------------------------------------------------#

                             if(file_exists($upload_dir.$file_name)){
                                 echo "File $i: ($file_name) already exists.<br>";
                             }else{
                                   #-----------------------------------------------------------#
                                   # this function will upload the files.   cool          #
                                   #-----------------------------------------------------------#
                                   if (move_uploaded_file($file_tmp,$upload_dir.$file_name)) {
                                       echo "File ".$i.": ".$name_file.":  ($file_name) Uploaded.<br>";
								   if($i==1)
										{
										$table = "products";

										$insert = "UPDATE $table SET $table.image='$file_name' WHERE id = '$sel_id' ";
										$results = mysql_query($insert) or die (mysql_error());
										}
									elseif($i==2)
										{
										$table = "products";

										$insert = "UPDATE $table SET $table.product_info='$file_name' WHERE id = '$sel_id' ";
										$results = mysql_query($insert) or die (mysql_error());
										}
                                   }else{
                                        echo "File $i: Faild to upload.<br>";
                                   }#end of (move_uploaded_file).

                             }#end of (file_exists).

                       }#end of (file_size).

                 }#end of (limitedext).

           }#end of (!is_uploaded_file).

       }#end of (for loop).
       # print back button.
       echo "»<a href=\"$_SERVER[php_SELF]\">back</a>";
////////////////////////////////////////////////////////////////////////////////
//else if the form didn't submitted then show it.
}
}

if($page==1)
	{
	if($a==2)
		{
		$query = "SELECT * FROM products WHERE id = '$sel_id'";
		$results = mysql_query($query) or die (mysql_error());
		$rows3 = mysql_fetch_array($results);

		$sel_id = $rows3['id'];
		$status = $rows3['status'];
		$type = $rows3['type'];
		$category = $rows3['category'];
		$name = $rows3['name'];
		$model = $rows3['model'];
		$serial = $rows3['serial'];
		$description = $rows3['description'];
		$image = $rows3['image'];
		$product_info = $rows3['product_info'];
		$manual = $rows3['manual'];
		$price = $rows3['price'];
		$lease = $rows3['lease'];
		}
	echo'
	<form method="post" action="'.$_SERVER['PHP_SELF'].'" target="_self">
	<table width="60%" cellpadding="5" cellspacing="0" border="0">';	

	$query1 = "SELECT * FROM tpl_status";
	$results1 = mysql_query($query1) or die (mysql_error());		

	echo'
	<tr>
		<td align="right">
		<strong>Status: </strong>
		</td>
		<td align="left">
		<select name="status">';
		while($rows1 = mysql_fetch_array($results1))
			{
			$status_id = $rows1['status_id'];
			$status_name = $rows1['status_name'];
			echo'
			<option value="'.$status_id.'"'; if(strlen($status)>0 & $status == $status_id) { echo 'selected="selected"';} echo'>'.$status_name.'</option>';
			}
		echo'
		</select>
		</td>
	</tr>
	<tr>
		<td align="right">
		<strong>Product Category: </strong>
		</td>
		<td align="left">
		<select name="category">';
		$query2 = "SELECT * FROM tpl_category";
		$results2 = mysql_query($query2) or die (mysql_error());		

		while($rows2 = mysql_fetch_array($results2))
			{
			$cat_id = $rows2['cat_id'];
			$category_name = $rows2['category'];
			echo'
			<option value="'.$cat_id.'"'; if(strlen($category)>0 & $category == $cat_id) { echo 'selected="selected"';} echo'>'.$category_name.'</option>';
			}
		echo'
		</select>
		</td>
	</tr>
	<tr>
		<td align="right">
		<strong>New / Used / Refurbished: </strong>
		</td>
		<td align="left">
		<select name="type">';
		$query2 = "SELECT * FROM tpl_type";
		$results2 = mysql_query($query2) or die (mysql_error());		

		while($rows2 = mysql_fetch_array($results2))
			{
			$type_id = $rows2['type_id'];
			$type_name = $rows2['type_name'];
			echo'
			<option value="'.$type_id.'"'; if(strlen($type)>0 & $type == $type_id) { echo 'selected="selected"';} echo'>'.$type_name.'</option>';
			}
		echo'
		</select>
		</td>
	</tr>
	<tr>
		<td align="right">
		<strong>Product Name: </strong>
		</td>
		<td align="left">
		<input type="text" name="name"'; if(strlen($name)>0) { echo 'value="'.$name.'"';} echo' />
		</td>
	</tr>
	<tr>
		<td align="right">
		<strong>Model: </strong>
		</td>
		<td align="left">
		<input type="text" name="model"'; if(strlen($model)>0) { echo 'value="'.$model.'"';} echo' />
		</td>
	</tr>
	<tr>
		<td align="right">
		<strong>Serial(If Applicable): </strong>
		</td>
		<td align="left">
		<input type="text" name="serial"'; if(strlen($serial)>0) { echo 'value="'.$serial.'"';} echo' />
		</td>
	</tr>
	<tr>
		<td align="center" colspan="2">
		<strong>Description: </strong>
		<br />
		<br />
		<textarea cols="80" rows="30" name="description">'; if(strlen($description)>0) { echo $description;} echo'</textarea>
		</td>
	</tr>';
	 for ($i = 1; $i <= $num_files; $i++)
		{
		if($i==1)
			{
			$name = "Product Image";
			$filename1 = $image;
			}
		elseif($i==2)
			{
			$name = "Product Info";
			$filename1 = $product_info;
			}
		echo '
		<tr>
			<td align="right">
			<strong>'.$name.': </strong>
			</td>
			<td align="left">
			<input type="file" name="file'. $i .'"><br>';
			if(strlen($filename1)>0)
				{
				echo '<strong>Current File:</strong> '.$filename1.'';
				}
			echo'
			</td>
		</tr>';
           }
	echo'
	<tr>
		<td align="right">
		<strong>Link Drivers & Manual: </strong>
		</td>
		<td align="left">
		<input type="text" name="manual"'; if(strlen($manual)>0) { echo 'value="'.$manual.'"';} echo' />
		</td>
	</tr>
	<tr>
		<td align="right">
		<strong>Price: </strong>
		</td>
		<td align="left">
		$<input type="text" name="price"'; if(strlen($price)>0) { echo 'value="'.$price.'"';} echo' />
		</td>
	</tr>
	<tr>
		<td align="right">
		<strong>Lease: </strong>
		</td>
		<td align="left">
		$<input type="text" name="lease"'; if(strlen($lease)>0) { echo 'value="'.$lease.'"';} echo' />
		</td>
	</tr>
	<tr>
		<td align="center" colspan="2">
		<input type="hidden" name="sel_id" value="'.$sel_id.'" />';
		if($a==1)
			{
			echo'<input type="submit" name="submit" value="Add" />';
			}
		elseif($a==2)
			{
			echo'<input type="submit" name="submit" value="Update" />';
			}
		echo'
		</td>
	</tr>';
	echo'
	</table>
	</form>';
	}
if($page==2)
	{
	$count=0;
	$query = "SELECT * FROM products ORDER BY date DESC";
	$results = mysql_query($query) or die (mysql_error());
	$num_rows = mysql_num_rows($results);
	echo '
	<table width="100%" cellpadding="0" cellspacing="0" border="1">
		<tr style="background-color:#DBD9FF;">
			<td width="1%" align="center"><strong>#</strong></td>
			<td width="1%" align="center"><strong>Id</strong></td>
			<td width="6%" align="center"><strong>Status</strong></td>
			<td width="4%" align="center"><strong>State</strong></td>
			<td width="6%" align="center"><strong>Category</strong></td>
			<td width="10%" align="center"><strong>Product Name</strong></td>
			<td width="10%" align="center"><strong>Model</strong></td>
			<td width="10%" align="center"><strong>Serial</strong></td>
			<td width="10%" align="center"><strong>Price</strong></td>
			<td width="10%" align="center"><strong>Lease</strong></td>
			<td width="10%" align="center"><strong>Last Updated</strong></td>
			<td width="10%" align="center" colspan="2"><strong>Action</strong></td>
		</tr>';
	while($rows = mysql_fetch_array($results))
		{
		$count++;
		$total2 = 0;
		$id = $rows['id'];
		$status = $rows['status'];
		$type = $rows['type'];
		$name = $rows['name'];
		$category = $rows['category'];
		$model = $rows['model'];
		$serial = $rows['serial'];
		$price = $rows['price'];
		$lease = $rows['lease'];
		$date = $rows['date'];
		$date = date('m-d-Y',$date);

		$query2 = "SELECT * FROM tpl_status WHERE status_id = '$status'";
		$results2 = mysql_query($query2) or die (mysql_error());		
		$rows2 = mysql_fetch_array($results2);
		$status_name = $rows2['status_name'];

		$query2 = "SELECT * FROM tpl_type WHERE type_id = '$type'";
		$results2 = mysql_query($query2) or die (mysql_error());		
		$rows2 = mysql_fetch_array($results2);
		$type_name = $rows2['type_name'];

		$query2 = "SELECT * FROM tpl_category WHERE cat_id = '$category'";
		$results2 = mysql_query($query2) or die (mysql_error());		
		$rows2 = mysql_fetch_array($results2);
		$category = $rows2['category'];

		echo'
		<tr>
			<td width="1%" align="center">'.$count.'</td>
			<td width="1%" align="center">'.$id.'</td>
			<td width="6%" align="center">'.$status_name.' </td>
			<td width="4%" align="center">'.$type_name.' </td>
			<td width="6%" align="center">'.$category.' </td>
			<td width="10%" align="center">'.$name.' </td>
			<td width="10%" align="center">'.$model.' </td>
			<td width="15%" align="center">'.$serial.' </td>
			<td width="10%" align="center">'.$price.' </td>
			<td width="10%" align="center">'.$lease.' </td>
			<td width="10%" align="center">'.$date.' </td>
			<td width="5%" align="center"><a href="products.php?p=3&id='.$id.'" title="Add Products" target="_self" style="font-family:Arial, Helvetica, sans-serif; font-weight:bold; font-size:small; color:#000000; text-decoration:none;"><strong>Delete</strong></a></td>
			<td width="5%" align="center"><a href="products.php?p=1&a=2&id='.$id.'" title="Add Products" target="_self" style="font-family:Arial, Helvetica, sans-serif; font-weight:bold; font-size:small; color:#000000; text-decoration:none;"><strong>Update</strong></a></td>
		</tr>';
		}
	echo'
	</table>';
	}
if($page==3)
	{
	echo'<strong style="font-size:xx-large; color:#000000;">Page 3</strong>';
	}
?>

 

Any help would be appreciated.

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.