Jump to content

Need help with a couple of things (image upload script)


meow

Recommended Posts

Hey everyone, I'm pretty new to PHP and would appreciate some help with an image upload script that I'm working on. I lifted it from a tutorial but I've been trying to customize it to suit my needs.

 

I have a nice little form:

<form enctype="multipart/form-data" method="post">
<table cellpadding="8">
	<tr>
		<td><label for="title">title:</label></td>
		<td><input type="text" name="title" /></td>
	</tr>
	<tr>
		<td><label for="description">description:</label></td>
		<td><textarea name="description" rows="4" cols="35"></textarea></td>
	</tr>
	<tr>
		<td><label for="image">image file:</label></td>
		<td><input type="file" name="image" /></td>
	</tr>
	<tr>
		<td><label for="author" maxlength="30">your name:</label></td>
		<td><input type="text" name="author"></td>
	</tr>
	<tr>
		<td><label for="email">your email:</label></td>
		<td><input type="text" name="email"></td>
	</tr>
	<tr>
		<td colspan="2"><input type="submit" value="Submit" /></td>
	</tr>
</table>
</form>

 

and this script:

<?
if(isset($_FILES["image"])){

        $file = $_FILES['image'];
        $title = htmlspecialchars($_POST["title"]);
        $description = htmlspecialchars($_POST["description"]);
$author = htmlspecialchars($_POST["author"]);
$email = htmlspecialchars($_POST["author"]);
$date = date('F jS, Y');
        $basename = date("d-m-Y-G-j-s-").basename($file['name']);

        if($file['type'] == 'image/gif' || $file['type'] == "image/jpeg" || $file['type'] == "image/pjpeg" || $file['type'] == 'image/png'){
        	
            if(move_uploaded_file($file['tmp_name'], "/path/to/public_html/wallpapers/".$basename) && 
		mysql_query("INSERT INTO wallpapers (id, title, description, image, author, email, date, basename) VALUES('', '$title', '$description', 'http://www.url.com/wallpapers/$basename', '$author', '$email', '$date', '$basename')") or die(mysql_error())){

			echo "<p>thanks! see your submission <a href='view.php?id=$id'>here</a>.</p>";
            }
            else {
                "<p>sorry, there was an error!</p>";
            }
        }//is an image
        else {
            echo "<p>that wasn't an image!</p>";
        }//isn't image

}//end isset file

else {
	echo "<p>all fields are required!</p>";
}
?>

 

I would like to a) create and save a thumbnail of the image as they are uploaded whose path I would be able to call from the MySQL database, b) make sure the user has not left any fields empty, and c) redirect or link the user to their submitted image.

 

I realize that this is a lot to ask but I would really appreciate it if someone could help me. Thanks a lot!  :)

Ok, checking empty values is easy enough. Just use the empty() function.

ex:

if(empty($title)){ $errors[] = 'Please input a title'; }

Then you can check $errors using:

if(!empty($errors)){
  foreach($errors as $msg){
    echo $msg.'<br />';
  }
  die();
}else{
  run rest of script...
}

 

For the thumbnails, look into the GD Image Libary: http://www.php.net/gd

http://www.phpgd.com/

 

Also, here's a tutorial that instructs you through creating thumbnails and resizing images.

http://www.phpit.net/article/image-manipulation-php-gd-part1/

http://www.phpit.net/article/image-manipulation-php-gd-part2/

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.