P.S: I got the feeling you are a beginner and I think I made a nice list at PHP Newbies: How to write good code
2) Save the images to a folder specified
move-uploaded-file should work. What comes to mind is that you have not set the right write permissions to move files. What kind of server are you using(Linux/Windows/MacOSX)? What does the function return as boolean(result)?
One other thing: I need to have the image renamed to a random string
(to prevent images being overwritten). I know I can do this with the
rand() function, but I forgot to mention it in my original post.
rand()
is not really random and could give duplicate filenames. uniqid will be unique
Edit: I figured out saving the descriptions to the database using
$_SESSION['file_n_desc']. I just need to figure out how to upload the
darn things and then save the locations into the database.
When using session when you have not overridden session you are not using your MySQL database but the file-system. You could read this tutorial to for store session in a database. Standard this session gets purged when closing the browser(just for that session). Just try this code:
<?php
session_start();
echo "<p>";
if (isset($_SESSION['count'])) {
echo $_SESSION['count']++;
} else {
$_SESSION['count'] = 0;
echo $_SESSION['count']++;
}
echo "</p>";
echo "<p>" . session_id() . "</p>";
Load that page couple of times, without closing your browser. You will get something that looks like:
0
rlu10shi6l390il130qinlt913
1
rlu10shi6l390il130qinlt913
But when you close browser that session_id has changed and your counter has reset and you get output that looks like.
0
fiakdijmmk38i40f39fm8u5mi4
1
fiakdijmmk38i40f39fm8u5mi4
Be able to call the images and descriptions in their classified
listing in kind of a gallery setting
I used PDO(Good Read) to do (My)SQL. I did unit-testing(TDD) using phpunit. To access the database I used SQLite in memory mode(very nice to do SQL-testing) => new PDO('sqlite::memory:');
. I tried to follow Uncle Bob's three rules(Good Read).
Normally you split this in multiple files. Every class in a separate file. Every class should be tested in Isolation(loose coupling) in a separate file.
I used foreign key to map "Listing to Image". I think this example is tested pretty thoroughly, but right now it is really time for me to get some sleep, so I am not sure ;).
<?php
function createDatabase() {
$db = new PDO('sqlite::memory:');
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $db;
}
function createTables($db) {
// Listing table containing all listings.
$db->exec(
<<<EOT
CREATE TABLE IF NOT EXISTS listing(
id INTEGER PRIMARY KEY,
description TEXT NOT NULL UNIQUE)
EOT
);
// Image table containg all images.
$db->exec(
<<<EOT
CREATE TABLE IF NOT EXISTS image(
id INTEGER PRIMARY KEY,
listing_id INTEGER,
URL TEXT NOT NULL UNIQUE,
description TEXT NOT NULL UNIQUE,
FOREIGN KEY (listing_id) REFERENCES listing(id))
EOT
);
}
class Listing {
private $db;
private $id;
public $description;
/*private function __construct() {
}*/
private function __construct(PDO $db, $id, $description) {
$this->db = $db;
$this->id = $id;
$this->description = $description;
}
public static function create(PDO $db, $description) {
$stmt = $db->prepare(<<<EOT
INSERT OR IGNORE INTO listing(description)
VALUES (:description)
EOT
);
$stmt->execute(array(
":description" => $description
));
if ($stmt->rowCount() !== 1) {
return NULL;
}
return new Listing($db, $db->lastInsertId(), $description);
}
public static function get(PDO $db, $id) {
$stmt = $db->prepare("SELECT description FROM listing WHERE id = :id");
$stmt->execute(array(
":id" => $id
));
$row = $stmt->fetch();
if ($row == null) {
return null;
}
return new Listing($db, $id, $row['description']);
}
public function getImages() {
return Image::getImages($this->db, $this);
}
public function save() {
$stmt = $this->db->prepare(
<<<EOT
UPDATE listing SET description = :description WHERE id = :id
EOT
);
$stmt->execute(array(
":description" => $this->description,
":id" => $this->id
));
}
public function id() {
return $this->id;
}
}
class Image {
private $pdo;
public $URL;
private $id;
public $description;
private function __construct(PDO $pdo, $URL, $description, Listing $listing, $id) {
$this->pdo = $pdo;
$this->URL = $URL;
$this->description = $description;
$this->id = $id;
}
public static function create(PDO $pdo, $URL, $description, Listing $listing) {
$stmt = $pdo->prepare(
<<<EOT
INSERT OR IGNORE INTO image(URL, listing_id, description)
VALUES (:URL, :listing_id, :description)
EOT
);
$stmt->execute(array(
":URL" => $URL,
":listing_id" => $listing->id(),
":description" => $description
));
if ($stmt->rowCount() !== 1) {
return NULL;
}
return new Image($pdo, $URL, $description, $listing, $pdo->lastInsertId());
}
public function id() {
return $this->id;
}
public static function getImages(PDO $pdo, Listing $listing) {
$result = array();
$stmt = $pdo->prepare(
<<<EOT
SELECT * FROM image where listing_id = :listing_id
EOT
);
$stmt->execute(array(
":listing_id" => $listing->id(),
));
while($row = $stmt->fetch()) {
//$result[] = array($row['URL'], $row['description']);
$result[] = new Image($pdo, $row['URL'], $row['description'], $listing, $row['id']);
}
return $result;
}
}
class Test extends PHPUnit_Framework_TestCase {
protected $db;
protected function setUp() {
$this->db = createDatabase();
createTables($this->db);
}
public function testCreatingSingleListing() {
$listing1 = Listing::create($this->db, "Listing 1");
$this->assertEquals(1, $listing1->id());
}
public function testCreatingMultipleListings() {
$listing1 = Listing::create($this->db, "Listing 1");
$listing1 = Listing::create($this->db, "Listing 2");
$this->assertEquals(2, $listing1->id());
}
public function testReturningListingReturnsNullWhenNonexistence() {
$this->assertNull(Listing::get($this->db, 1));
}
public function testReturningCreatedListing() {
$Listing1 = Listing::create($this->db, "Listing 1");
$this->assertEquals("Listing 1", Listing::get($this->db, 1)->description);
}
public function testSavingListing() {
$listing1 = Listing::create($this->db, "Listing 1");
$listing1->description = "new";
$listing1->save();
$this->assertEquals("new", Listing::get($this->db, 1)->description);
}
public function testListingHasNoImagesWhenJustCreated() {
$listing1 = Listing::create($this->db, "Listing 1");
$this->assertEquals(array(), $listing1->getImages());
}
public function testAddingImageToListing() {
$listing1 = Listing::create($this->db, "Listing 1");
$image1 = Image::create($this->db, "http://localhost:12343/dfdfx/45.png", "first image", $listing1);
$this->assertEquals(array($image1), $listing1->getImages());
}
public function testAddingImagesToListing() {
$listing1 = Listing::create($this->db, "Listing 1");
$image1 = Image::create($this->db, "http://localhost:12343/dfdfx/45.png", "first image", $listing1);
$image2 = Image::create($this->db, "http://localhost:12343/df/46.png", "second image", $listing1);
$this->assertEquals(array($image1, $image2), $listing1->getImages());
}
}