I can help you for the first part.
i have already written a function for uploading pictures that can be used for uploading any kind of format with a little changes in source.
it can give you the clue.
here is the code for the function:
Code:
function uploadimage($name, $file, $tablename, $dir, $username)
{
$debug = false;
$maxwidth = 300;
$maxheight = 300;
$image_caption = $name;
$image_username = $username;
$image_tempname = $file['name'];
$filename = $file['name'];
$tmpname = $file['tmp_name'];
if ($debug)
{
list($width, $height, $type, $attr) = getimagesize($tmpname);
echo "<span class = debug>";
echo "<p >Debug info: <p />";
echo $image_tempname;
echo "<br />";
echo $tmpname;
echo "<br />";
echo "image width and height are: " . $width . "*" . $height;
echo "</span>";
die();
}
$today = date("Y-m-d");
//upload image and check for image type
//make sure to change your path to match your images directory
//$ImageDir ="D:/Program Files/Apache Software Foundation/Apache2.2/htdocs/nmpfrance2/images2/";
$ImageDir = $dir;
$ImageName = $ImageDir . $image_tempname;
list($width, $height, $type, $attr) = getimagesize($tmpname);
if ($width > $maxwidth || $height > $maxheight)
{
unlink($tmpname);
echo "<p><span class = error> Image Width and Height should be equal or less than 300 pixels </span><p />";
echo "<a href = 'javascript:history.back()' class = activelink> Select another image </a>";
die();
}
if (move_uploaded_file($tmpname, $ImageName))
{
//get info about the image being uploaded
list($width, $height, $type, $attr) = getimagesize($ImageName);
switch ($type)
{
case 1:
$ext = ".gif";
break;
case 2:
$ext = ".jpg";
break;
case 3:
$ext = ".png";
break;
default:
echo "Sorry, but the file you uploaded was not a GIF, JPG, or " .
"PNG file.<br>";
echo "Please hit your browser's 'back' button and try again.";
echo "<br>or click<a href='javascript:history.back()'> here</a>!";
die();
}
//insert info into image table
$insert = "INSERT INTO $tablename
(image_caption, image_username, image_ext,image_date)
VALUES
('$image_caption', '$image_username', '$ext','$today')";
$insertresults = mysql_query($insert)
or die(mysql_error('Can not insert image record!'));
$lastpicid = mysql_insert_id();
$dotindex = strpos($filename, '.', 0 );
$filename = substr($filename, 0, $dotindex);
$filename = $filename . $lastpicid . $ext;
$update = "update $tablename set saved_name = '$filename' where image_id = $lastpicid";
$upres = mysql_query($update) or die('<span class = error> Can not update image record!!!</span>');
$newfilename = $ImageDir . $filename;
///// if the file exist then this code can not raname it
//// instead it will copy newfile with newname and then remove(unlink oldfile)
if (!rename($ImageName, $newfilename))
{
if (copy ($ImageName, $newfilename))
{
unlink($ImageName);
}
}
}
return $lastpicid;
}
Touraj Ebrahimi [toraj_e] [at] [yahoo] [dot] [com]