Hy, I am a beginner in PHP programming and I fallowed some tutorials in order to learn a piece of it. I started an OOP project with the help of some tutorials and I need some help from some specialists. I managed to upload one file at a time throw a form and insert it into database, the problem occurs when I am trying to upload multiple files throw same form. An example of the database:
I have a movie id, name, poster and trailer, poster and trailer are files, how can I do multiple uploads and associate them to the same movie in my case?
This is the a piece of my code, which is written with the help of tutorials and modified by me:
add_movie.php
PHP Code:
<?php
$max_file_size = 1048576
if(isset($_POST['submit'])) {
$movie = new Movie();
$movie->name = $_POST['name'];
$ok = 1; // Mandatory file
if($ok==1 || $_FILES['poster_upload']['size'] > 0 ) {
$movie->attach_file($_FILES['poster_upload']);
$ok = 1;
}
$movie->trailer = $_POST['trailer'];
$movie->trailer_caption = $_POST['trailer_caption'];
if($movie->save($id=NULL, $ok)) {
// Success
$session->message("Movie uploaded successfully.");
redirect_to('movies.php');
} else {
// Failure
$message = join("<br />", $movie->errors);
}
}
?>
<br/>
<h2 align="center">Add Movie</h2>
<?php echo output_message($message); ?>
<table class="bordered" cellpadding="0px" cellspacing="0px" align="center" >
<form action="add_movie.php" enctype="multipart/form-data" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" />
<tr>
<td>Name:</td>
<td><input type="text" name="name" value="" size="70" /></td>
</tr>
<tr>
<td>Poster:</td>
<td><input type="file" name="poster_upload" size="70" /></td>
</tr>
<tr>
<td>Poster Caption:</td>
<td><input type="text" name="poster_caption" value="" size="70" /></td>
</tr>
<tr>
<td>Trailer:</td>
<td><input type="text" name="trailer" value="" size="70" /></td>
</tr>
<tr>
<td>Trailer Caption:</td>
<td><input type="text" name="trailer_caption" value="" size="70" /></td>
</tr>
<tr>
<td><a href="movies.php">Back</a></td>
<td><input type="submit" name="submit" value="Add Movie" size="70" /></td>
</tr>
</form>
</table>
movie.php // Class
PHP Code:
<?php
protected static $table_name="movies";
protected static $db_fields=array('id', 'name', 'poster', 'poster_type', 'poster_size', 'poster_caption', 'trailer', 'trailer_type', 'trailer_size', 'trailer_caption');
public $id;
public $name;
public $poster;
public $poster_type;
public $poster_size;
public $poster_caption;
public $trailer;
public $trailer_type;
public $trailer_size;
public $trailer_caption;
private $temp_path;
protected $upload_dir="movie_posters";
public $errors=array();
protected $upload_errors = array(
UPLOAD_ERR_OK => "No errors.",
UPLOAD_ERR_INI_SIZE => "Larger than upload_max_filesize.",
UPLOAD_ERR_FORM_SIZE => "Larger than form MAX_FILE_SIZE.",
UPLOAD_ERR_PARTIAL => "Partial upload.",
UPLOAD_ERR_NO_FILE => "No file.",
UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.",
UPLOAD_ERR_CANT_WRITE => "Can't write to disk.",
UPLOAD_ERR_EXTENSION => "File upload stopped by extension."
);
// Pass in $_FILE(['uploaded_file']) as an argument
public function attach_file($file) {
// Perform error checking on the form parameters
if(!$file || empty($file) || !is_array($file)) {
// error: nothing uploaded or wrong argument usage
$this->errors[] = "No file was uploaded.";
return false;
} elseif($file['error'] != 0) {
// error: report what PHP says went wrong
$this->errors[] = $this->upload_errors[$file['error']];
return false;
} else {
// Set object attributes to the form parameters.
$this->temp_path = $file['tmp_name'];
$this->poster = basename($file['name']);
$this->poster_type= $file['type'];
$this->poster_size= $file['size'];
// Don't worry about saving anything to the database yet.
return true;
}
}
public function save($id=NULL, $ok=NULL) {
if(isset($id)) {
$this->update($id);
}
// Can't save if there are pre-existing errors
if(!empty($this->errors)) { return false; }
// Make sure the caption is not too long for the DB
if(strlen($this->poster_caption) > 255) {
$this->errors[] = "The Poster caption can only be 255 characters long.";
return false;
} elseif(strlen($this->trailer_caption) > 255) {
$this->errors[] = "The Trailer caption can only be 255 characters long.";
return false;
}
if(isset($ok)) {
// Can't save without filename and temp location
if(empty($this->poster) || empty($this->temp_path)) {
$this->errors[] = "The file location was not available.";
return false;
}
// Determine the target_path
$target_path = SITE_ROOT .DS. 'public' .DS. $this->upload_dir .DS. $this->poster;
// Make sure a file doesn't already exist in the target location
if(file_exists($target_path)) {
$this->errors[] = "The file {$this->poster} already exists.";
return false;
}
// Attempt to move the file
if(move_uploaded_file($this->temp_path, $target_path)) {
// Success
// Save a corresponding entry to the database
if(!isset($id)) {
if($this->create()) {
// We are done with temp_path, the file isn't there anymore
unset($this->temp_path);
return true;
}
} else {
// We are done with temp_path, the file isn't there anymore
unset($this->temp_path);
return true;
}
} else {
// File was not moved.
$this->errors[] = "The file upload failed, possibly due to incorrect permissions on the upload folder.";
return false;
}
} else {
// Don't worry about saving anything to the database yet.
return true;
}
}
public function update($id=0) {
global $database;
$class_name = get_called_class();
$object = new $class_name;
// Don't forget your SQL syntax and good habits:
// - UPDATE table SET key='value', key='value' WHERE condition
// - single-quotes around all values
// - escape all values to prevent SQL injection
$attributes = $this->sanitized_attributes();
$attribute_pairs = array();
foreach($attributes as $key => $value) {
if($value == '')
continue;
$attribute_pairs[] = "{$key}='{$value}'";
}
$sql = "UPDATE ".$object::$table_name." SET ";
$sql .= join(", ", $attribute_pairs);
$sql .= " WHERE id=". $database->escape_value($id);
$database->query($sql);
return ($database->affected_rows() != 0) ? true : false;
}
public function image_path() {
return $this->upload_dir."/".$this->poster;
}
}
?>
I posted the code that works, for uploading one file, in this case, the poster and inserted in the database. I tried to modify the attach_file function along with the form, with arrays but I didn't succeeded, can anyone who have a little time show me how to modify it to upload multiple files at the same time and insert them in one row, associated with one movie?
Thank you for your time and sorry for my grammar, English it's not my native language. Have a good day!