Hi,
I have a problrm that when I’m running my upload page script the file gets downloaded. The page is for uploading an image but whenever I try to upload an image the source code page gets uploaded. I don’t know what is the problem. Here’s my code:
[code]<?php
//connect to the database
$link = mysql_connect(“localhost”, “root”, “”) or die("Could not connect: " . mysql_error());
mysql_select_db(“test”, $link) or die (mysql_error());
//make variables available
$image_caption = $_POST[‘image_caption’];
$image_username = $_POST[‘image_username’];
$image_tempname = $_FILES[‘image_filename’][‘name’];
$today = date(“d-m-Y”);
//upload image and check for image type
//make sure to change your path to match your images directory
$ImageDir =“File System/opt/lampp/htdocs/examples/image upload/images/”;
$ImageName = $ImageDir . $image_tempname;
if (move_uploaded_file($_FILES[‘image_filename’][‘tmp_name’], $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.
”;
echo “Please hit your browser’s ‘back’ button and try again.”;
}
//insert info into image table
$insert = “INSERT INTO images (image_caption, image_username, image_date) VALUES (’$image_caption’, ‘$image_username’, ‘$today’)”;
$insertresults = mysql_query($insert) or die(mysql_error());
$lastpicid = mysql_insert_id();
$newfilename = $ImageDir . $lastpicid . $ext;
rename($ImageName, $newfilename);
}
?>
So how does it feel to be famous?
Here is the picture you just uploaded to our servers:
This image is a <?php echo $ext; ?> image.
It is <?php echo $width; ?> pixels wide and <?php echo $height; ?> pixels high.
It was uploaded on <?php echo $today; ?>. [/code]
File System/opt/lampp/htdocs/examples/image upload/images/ is the name of directory path where I’ll store my images.
Code of HTML form:
[code]
Upload your pic to our site!Image Title or Caption Example: You talkin' to me? |
|
Your Username | Upload Image: |
Acceptable image formats include: GIF, JPG/JPEG, and PNG.
[/code]
Code of database creation:
<?php
//connect to the database
$link = mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error());
mysql_select_db("test", $link) or die (mysql_error());
//create images table
$sql = "CREATE TABLE IF NOT EXISTS images (
image_id INT(11) NOT NULL AUTO_INCREMENT,
image_caption VARCHAR(255) NOT NULL,
image_username VARCHAR(255) NOT NULL,
image_date DATE NOT NULL,
PRIMARY KEY (image_id)
)";
$results = mysql_query($sql) or die(mysql_error());
echo "Image table successfully created.";
?>
I’m practicing from wrox book and this code is from there. I am trying the exact code and only thing changed is hostname and password. Please check this and help me. Thanks.