Code for File Upload

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I have been seeing a lot of talk about file uploads.
I wrote the following code and it works well when uploading images. The
actual image is not stored in the database just the name of the image.
When you want to view the image just query the database and include the
database variable in your <IMG SRC> tag.
<IMG SRC = http://www.path to image $image_name">.

<?
// includes
include("config.php");
?>

<?
// Validate entries
if ($_REQUEST[pic_category] == "")
        {
        echo "Must enter a Category for the pictures to go in.";
        exit;
        }
if ($_FILES['userfile']['size'] == 0)
        {
        echo "No size";
        exit;
        }
if ($_FILES['userfile']['type'] == "text/plain")
        {
        echo "cannot upload text it must be an image";
        exit;
        }

// Set Name of Image for Database entry. This comes from the input
fields in the HTML form.
$pic_1 = $_FILES['userfile']['name'];
$pic_2 = $_FILES['userfile_2']['name'];
$pic_3 = $_FILES['userfile_3']['name'];
$pic_4 = $_FILES['userfile_4']['name'];
$pic_5 = $_FILES['userfile_5']['name'];
$pic_6 = $_FILES['userfile_6']['name'];

// Path to where images are to be uploaded
$upfile = "/home/SOMEWHERE/php/photos/".$_FILES['userfile']['name'];
$upfile_2 = "/home/SOMEWHERE/php/photos/".$_FILES['userfile_2']['name'];

$upfile_3 = "/home/SOMEWHERE/php/photos/".$_FILES['userfile_3']['name'];

$upfile_4 = "/home/SOMEWHERE/php/photos/".$_FILES['userfile_4']['name'];

$upfile_5 = "/home/SOMEWHERE/php/photos/".$_FILES['userfile_5']['name'];

$upfile_6 = "/home/SOMEWHERE/php/photos/".$_FILES['userfile_6']['name'];

// Upload files to the $upfile Directory
if (is_uploaded_file($_FILES['userfile']['tmp_name']))
        {
        move_uploaded_file($_FILES['userfile']['tmp_name'], "$upfile");
        move_uploaded_file($_FILES['userfile_2']['tmp_name'],
"$upfile_2");
        move_uploaded_file($_FILES['userfile_3']['tmp_name'],
"$upfile_3");
        move_uploaded_file($_FILES['userfile_4']['tmp_name'],
"$upfile_4");
        move_uploaded_file($_FILES['userfile_5']['tmp_name'],
"$upfile_5");
        move_uploaded_file($_FILES['userfile_6']['tmp_name'],
"$upfile_6");
        }
        else
        {
        echo "Possible file upload attack. Filename: " .
$_FILES['userfile']['name'];
        }

// Print out the files that were uploaded
echo "file was uploaded <BR>"  . $_FILES['userfile']['name'];
echo "<BR>"  . $_FILES['userfile_2']['name'];
echo "<BR>"  . $_FILES['userfile_3']['name'];
echo "<BR>"  . $_FILES['userfile_4']['name'];
echo "<BR>"  . $_FILES['userfile_5']['name'];
echo "<BR>"  . $_FILES['userfile_6']['name'];
?>

<?
// open connection to database
$connection = mysql_connect($hostname, $user, $pass) or die ("Unable to
connect!");

// Place the picture name and captions in the database
$query = "INSERT INTO table name and so on and so
on................................

Hope this will helps!

Wade

--
Should you have any questions, comments or concerns, feel free to call
me at 318-338-2033.

Thank you for your time,

Wade Kelley, Design Engineer
================================================
Bayou Internet...(888) 30-BAYOU...http://www.bayou.com
Mississippi Internet...(800) MISSISSIPPI...http://www.mississippi.net
Vicksburg Online...(800) MISSISSIPPI...http://www.vicksburg.com
================================================




-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux