Dynadot Aftermarket: discover domain gems worth investing in. Visit our aftermarket.

Photoblog

Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
SpaceshipSpaceship
Watch

DJL2K

Pimp Master MEstablished Member
Impact
0
Right, well basically I'm trying to make a photoblog. I seem to have got myself a good code apart from one thing - the images don't upload.
addphoto.php
PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php
include('dbconnect.php');
$photo = $_POST['photo'];
$title = $_POST['title'];
$description = $_POST['description'];
$comments = $_POST['comments'];
$date = addslashes(strip_tags(date("j/n/y")));
$uploadto = "/photos/";
$maxsize = 1048576;
$filename = $_FILES['photo']['name'];
$filewidth = $_FILES['photo']['width'];
$fileheight = $_FILES['photo']['height'];
$allowed = array (
		"image/gif" => "gif",
		"image/pjpeg" => "jpg",
		"image/png" => "png",
		"image/bmp" => "bmp",
		"image/jpeg" => "jpg",
		"image/jpg" => "jpeg",
		);
if(isset($_POST['submit'])&& strlen($_POST['submit']) > 0 ) {
	if(!array_key_exists($_FILES['photo']['type'], $allowed)) {
		die("File is an invalid type (.gif, .jpeg, .png or .bmp).");
		}
	elseif(!array_key_exists($_FILES['photo']['size'] > $maxsize)) {
		die("File is too big (>1MB).");
		}
	elseif(file_exists($uploadto . $_FILES['photo']['title'])) {
		die("This file already exists.");
		}
	else {
		copy($filename, $uploadto.$filename) or die("Error, please try again later");
		$image = $uploadto.$filename;
		mysql_query("INSERT * INTO photos SET title='$title', description='$description', date='$date', location='$image', comments='$comments', height='$fileheight', width='$filewidth'") or die(mysql_error());
		echo "Image uploaded.<br /><a href='index.php'>Go back to the photoblog</a> or <a href='".$_SERVER['PHP_SELF']."'>Upload another photo";
		}
	}
else {
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
  Photo Title:<br />
  <input type="text" name="title" /><br />
  Photo description:<br />
  <textarea name="description"></textarea><br />
  Comments allowed?<br />
  <select name="comments">
	<option>Yes</option>
	<option>No</option>
  </select><br />
  Upload photo:<br />
  <input type="file" name="photo" /><br />
  <input type="submit" name="submit" value="Upload photo" />
</form>
<?php
}
?>
</body>
</html>
Basically what happens is when I try to upload the image and everything it comes up with the first error:
File is an invalid type (.gif, .jpeg, .png or .bmp).
I can't see anything wrong with the code. Anyone have any ideas?
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Are you uploading the correct file extension?
 
0
Close
hi

Your form needs an enctype for starters
example
HTML:
      <form name="myfrm" method="post" action="" enctype="multipart/form-data">
 
0
Close
Yup. Positive, I tryed a small .png and a small .jpeg and I've used a form like this before and never needed an enctype

Okay, updated the code:
addphoto.php
PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php
include('dbconnect.php');
$photo = $_POST['photo'];
$title = $_POST['title'];
$description = $_POST['description'];
$comments = $_POST['comments'];
$date = addslashes(strip_tags(date("j/n/y")));
$uploadto = "/photos/";
$maxsize = 1048576;
$filename = $_FILES['photo']['name'];
$filewidth = $_FILES['photo']['width'];
$fileheight = $_FILES['photo']['height'];
$allowed = array (
        "image/gif" => "gif",
        "image/pjpeg" => "jpg",
        "image/png" => "png",
        "image/bmp" => "bmp",
        "image/jpeg" => "jpg",
        "image/jpg" => "jpeg",
        );
if(isset($_POST['submit'])&& strlen($_POST['submit']) > 0 ) {
    if(!array_key_exists($_FILES['photo']['type'], $allowed)) {
        die("File is an invalid type (.gif, .jpeg, .png or .bmp).");
        }
    elseif($_FILES['photo']['size'] > $maxsize) {
        die("File is too big (>1MB).");
        }
    elseif(file_exists($uploadto . $_FILES['photo']['title'])) {
        die("This file already exists.");
        }
    else {
        copy($filename, $uploadto.$photo) or die("Error, please try again later");
        $image = $uploadto.$filename;
        mysql_query("INSERT * INTO photos SET title='$title', description='$description', date='$date', location='$image', comments='$comments', height='$fileheight', width='$filewidth'") or die(mysql_error());
        echo "Image uploaded.<br /><a href='index.php'>Go back to the photoblog</a> or <a href='".$_SERVER['PHP_SELF']."'>Upload another photo";
        }
    }
else {
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data">
  Photo Title:<br />
  <input type="text" name="title" /><br />
  Photo description:<br />
  <textarea name="description"></textarea><br />
  Comments allowed?<br />
  <select name="comments">
    <option>Yes</option>
    <option>No</option>
  </select><br />
  Upload photo:<br />
  <input type="file" name="photo" /><br />
  <input type="submit" name="submit" value="Upload photo" />
</form>
<?php
}
?>
</body>
</html>
And I get this:
Warning: copy(avatar2.png) [function.copy]: failed to open stream: No such file or directory in /home/license/public_html/photos/addphoto.php on line 40
 
Last edited:
0
Close
Pretty cool if you can make it work with no enctype, i've never been able to accomplish it.
 
0
Close
My main website uses a contact form with no enctype. But back on topic, does anyone have a clue what wrong with it?
 
0
Close
DJL2K said:
My main website uses a contact form with no enctype.

Does your contact form UPLOAD FILES?
 
0
Close
I thought you were talking about forms in general. But anyway, I've tryed different fuctions now. copy() just gives my an error saying it can't upload; chmod() does the same and move_uploaded_file() just uses the die() with no PHP error.
 
0
Close
Appraise.net

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back