Upload a Photo - Class help
Posted: Thu Mar 04, 2010 10:30 am
Hi Guys!
Thing have been going well for me, learning procedural PHP coding, but I don't want to go any further without putting it into Object Orientated Programming format.
I'm working on a new Class that lets the user upload an image with a description onto the server. The function gives the image a unique name and puts it in the folder /images/photos. The image name is also written into a mysql database along with the user's description, posted from a form.
Any advice or thoughts would be really helpful, and my biggest problems are to do with using OOP.
The main thing I want to achieve is, there is an echo="$message"; command at the bottom of the user's page. I want this to say either "Image uploaded successfully", "Please provide a description" or "No file was selected to be uploaded".
I have the whole thing fundamentally working, so any suggestions to help me finish this would be fantastic!
Here we go...
Thing have been going well for me, learning procedural PHP coding, but I don't want to go any further without putting it into Object Orientated Programming format.
I'm working on a new Class that lets the user upload an image with a description onto the server. The function gives the image a unique name and puts it in the folder /images/photos. The image name is also written into a mysql database along with the user's description, posted from a form.
Any advice or thoughts would be really helpful, and my biggest problems are to do with using OOP.
The main thing I want to achieve is, there is an echo="$message"; command at the bottom of the user's page. I want this to say either "Image uploaded successfully", "Please provide a description" or "No file was selected to be uploaded".
I have the whole thing fundamentally working, so any suggestions to help me finish this would be fantastic!
Here we go...
Code: Select all
<?php
//connect to my database using a "DB" class
require_once("DB.php");
// Here's the new class "Photos"
class Photos{
private $db;
private $success = false;
public function __construct()
{
//connect to the database
$this->db = new DB();
}
//the new function begins...
public function InsertPhoto($description, $image = array())
{
try{
// check there is a file to upload
if("" != $image['picture']['name'] && 0 != $image['picture']['size']){
// name the image after the time uploaded and its original name
$imgName = time() . basename($image['picture']['name']);
}else{
// Here I get lost, I want to exit the script and set the "$message" to "please select a photo to upload"
}
$sql = "INSERT INTO photos VALUES(NULL, '{$description}', '{$imgName}')";
$result = $this->db->connection->query($sql);
if(!$result){
throw new Exception($sql);
}else{
// finish the file upload
$target = "../images/photos/" . $imgName;
move_uploaded_file($image['picture']['tmp_name'], $target);
// set success to true
$this->success = true;
}
}catch(Exception $e){
echo("Insert query failed: " . $e->getMessage());
}
}
public function getSuccess()
{
return $this->success;
}
}
?>
<?php
//here is the Page using the class
if("Upload" == $_POST['submit']){
// check the user has filled in the description box
if("" != $_POST['description']){
$photoInsert = new Photos();
$photoInsert->InsertPhoto($_POST['description'], $_FILES);
if($photoInsert->getSuccess()){
//set the "$message" to "your photo has sucessfully been uploaded"
}else{
//sticky form to keep the values of the form
$description = $_POST['description'];
$message = "Required Field(s) are still empty";
}
}
}
?>
<form action="<?php echo($_SERVER['PHP_SELF']);?>" method="post" enctype="multipart/form-data">
<ol>
<li>
<label for="description">Description:</label>
<textarea name="description" id="description" rows="7" cols="30"><?php echo($description); ?></textarea>
</li>
<li>
<label for="picture">Image:</label>
<input type="file" name="picture" id="picture" />
</li>
<li class="last_item">
<input type="submit" value="Upload" name="submit" class="button" />
</li>
</ol>
</form>
<?php
// finally here is the message I'm having difficulty with.
// All it has to say is "Please fill out the description field"
//"Please select a photo from your computer to upload"
//and
//Photo successfully uploaded!
if($message){
echo("{$message}");
}
?>