file or image upload php problem
<p>I  having problems with the download code of this script, I have modified it and added other features to it that work. Just the download part of the script does not work, I will provide the full code for all the files.</p>

<p><strong>upload.php</strong></p>

<pre><code>&lt;?php
require_once 'dbc.php';
page_protect();

$client_ID = mysql_query("SELECT id
    FROM clients WHERE user_name='".$_SESSION['user_name']."'")or die(mysql_error());
$client_ID = mysql_fetch_array($client_ID);
$client_ID = $client_ID['id'];


$uploadDir = 'uploads/';

if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}

$hostname_conndb = "localhost";
$database_conndb = "uploads";
$username_conndb = "root";
$password_conndb = "";
$conndb = mysql_connect($hostname_conndb, $username_conndb, $password_conndb) or trigger_error(mysql_error(),E_USER_ERROR);

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
} 

$date = date('Y-m-d H:i:s');

$sql = "INSERT INTO upload2 (name, client, size, type, path, date ) ".
"VALUES ('$fileName', '$client_ID', '$fileSize', '$fileType', '$filePath', '$date')";
mysql_select_db($database_conndb, $conndb);
$result = mysql_query($sql, $conndb) or die(mysql_error());

echo "&lt;br&gt;File $fileName uploaded&lt;br&gt;";

}
?&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Download File From MySQL&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;link href="styles.css" rel="stylesheet" type="text/css"&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;?php
$hostname_conndb = "localhost";
$database_conndb = "uploads";
$username_conndb = "root";
$password_conndb = "";
$conndb = mysql_connect($hostname_conndb, $username_conndb, $password_conndb) or trigger_error(mysql_error(),E_USER_ERROR);

$sql = "SELECT * FROM upload2 WHERE client='".$client_ID."' ORDER BY date DESC";
mysql_select_db($database_conndb, $conndb);
$result = mysql_query($sql, $conndb) or die(mysql_error());
$rows = mysql_fetch_assoc($result);
$total_rows = mysql_num_rows($result);
?&gt;
Welcome &lt;?php echo $_SESSION['user_name'];?&gt;
&lt;form method="post" enctype="multipart/form-data"&gt;
&lt;table width="350" border="0" cellpadding="1" cellspacing="1" class="box"&gt;
&lt;tr&gt; 
&lt;td width="246"&gt;
&lt;input type="hidden" name="MAX_FILE_SIZE" value="2000000"&gt;
&lt;input name="userfile" type="file" id="userfile"&gt; 
&lt;/td&gt;
&lt;td width="80"&gt;&lt;input name="upload" type="submit" class="box" id="upload" value=" Upload "&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;

&lt;?php if($total_rows &gt; 0) { ?&gt;
          &lt;table border="0" cellpadding="0" cellspacing="0" id="tbl_repeat"&gt;
        &lt;tr&gt;
          &lt;th scope="col"&gt;FIle/Image Name&lt;/th&gt;
          &lt;th scope="col" style="width:15%"&gt;Date&lt;/th&gt;
          &lt;th scope="col" style="width:10%"&gt;Size&lt;/th&gt;
          &lt;th scope="col" style="width:10%"&gt;Download&lt;/th&gt;
        &lt;/tr&gt;
        &lt;?php do { ?&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;?php echo $rows['name']; ?&gt;&lt;/td&gt;
          &lt;td&gt;&lt;?php echo $rows['date']; ?&gt;&lt;/td&gt;
          &lt;td&gt;&lt;?php echo $rows['size']; ?&gt;&lt;/td&gt;
          &lt;td&gt;&lt;a href="downloads.php?id=&lt;?php echo $rows['id']; ?&gt;"&gt;Download&lt;/a&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;?php } while($rows = mysql_fetch_assoc($result)); ?&gt;
      &lt;/table&gt; 
      &lt;?php } else { echo "&lt;p class="warn"&gt;Sorry there are no records available.&lt;/p&gt;"; } ?&gt;
&lt;p&gt;&lt;br /&gt;
  &lt;a href="logout.php"&gt;Logout &lt;/a&gt;&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>This code works fine. The download code is: downloads.php </p>

<pre><code>&lt;?php
require_once 'dbc.php';
page_protect();

$client_ID = mysql_query("SELECT id
    FROM clients WHERE user_name='".$_SESSION['user_name']."'")or die(mysql_error());
$client_ID = mysql_fetch_array($client_ID);
$client_ID = $client_ID['id'];


$uploadDir = 'uploads/';

if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}

$hostname_conndb = "localhost";
$database_conndb = "uploads";
$username_conndb = "root";
$password_conndb = "qaasim11";
$conndb = mysql_connect($hostname_conndb, $username_conndb, $password_conndb) or trigger_error(mysql_error(),E_USER_ERROR);

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
} 

$date = date('Y-m-d H:i:s');

$sql = "INSERT INTO upload2 (name, client, size, type, path, date ) ".
"VALUES ('$fileName', '$client_ID', '$fileSize', '$fileType', '$filePath', '$date')";
mysql_select_db($database_conndb, $conndb);
$result = mysql_query($sql, $conndb) or die(mysql_error());

echo "&lt;br&gt;File $fileName uploaded&lt;br&gt;";

}
?&gt;

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Download File From MySQL&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;link href="styles.css" rel="stylesheet" type="text/css"&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;?php
$hostname_conndb = "localhost";
$database_conndb = "uploads";
$username_conndb = "root";
$password_conndb = "";
$conndb = mysql_connect($hostname_conndb, $username_conndb, $password_conndb) or trigger_error(mysql_error(),E_USER_ERROR);

$sql = "SELECT * FROM upload2 WHERE client='".$client_ID."' ORDER BY date DESC";
mysql_select_db($database_conndb, $conndb);
$result = mysql_query($sql, $conndb) or die(mysql_error());
$rows = mysql_fetch_assoc($result);
$total_rows = mysql_num_rows($result);
?&gt;
Welcome &lt;?php echo $_SESSION['user_name'];?&gt;
&lt;form method="post" enctype="multipart/form-data"&gt;
&lt;table width="350" border="0" cellpadding="1" cellspacing="1" class="box"&gt;
&lt;tr&gt; 
&lt;td width="246"&gt;
&lt;input type="hidden" name="MAX_FILE_SIZE" value="2000000"&gt;
&lt;input name="userfile" type="file" id="userfile"&gt; 
&lt;/td&gt;
&lt;td width="80"&gt;&lt;input name="upload" type="submit" class="box" id="upload" value=" Upload "&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;

&lt;?php if($total_rows &gt; 0) { ?&gt;
          &lt;table border="0" cellpadding="0" cellspacing="0" id="tbl_repeat"&gt;
        &lt;tr&gt;
          &lt;th scope="col"&gt;FIle/Image Name&lt;/th&gt;
          &lt;th scope="col" style="width:15%"&gt;Date&lt;/th&gt;
          &lt;th scope="col" style="width:10%"&gt;Size&lt;/th&gt;
          &lt;th scope="col" style="width:10%"&gt;Download&lt;/th&gt;
        &lt;/tr&gt;
        &lt;?php do { ?&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;?php echo $rows['name']; ?&gt;&lt;/td&gt;
          &lt;td&gt;&lt;?php echo $rows['date']; ?&gt;&lt;/td&gt;
          &lt;td&gt;&lt;?php echo $rows['size']; ?&gt;&lt;/td&gt;
          &lt;td&gt;&lt;a href="downloads.php?id=&lt;?php echo $rows['id']; ?&gt;"&gt;Download&lt;/a&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;?php } while($rows = mysql_fetch_assoc($result)); ?&gt;
      &lt;/table&gt; 
      &lt;?php } else { echo "&lt;p class="warn"&gt;Sorry there are no records available.&lt;/p&gt;"; } ?&gt;
&lt;p&gt;&lt;br /&gt;
  &lt;a href="logout.php"&gt;Logout &lt;/a&gt;&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>Also this the code for my database: </p>

<pre><code>CREATE TABLE IF NOT EXISTS `upload2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`client` int(11) NOT NULL,
`name` varchar(30) NOT NULL,
`type` varchar(30) NOT NULL,
`size` int(11) NOT NULL,
`path` varchar(60) NOT NULL,
`date` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

--
-- Dumping data for table `upload2`
--

INSERT INTO `upload2` (`id`, `client`, `name`, `type`, `size`, `path`, `date`) VALUES
(1, 1, 'back.gif', 'image/gif', 1997, 'uploads/back.gif', '2010-09-19 12:17:05'); 
</code></pre>

<p>When i click the download link in upload.php i get the following error 
*Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in downloads.php on line 17*
the fileis non exist in</p>

<p>I am not sure if the code works to download files/images if this error was not their as I cannot figure out how to fix this pronlem. </p>
