PHP Classes

Multiple upload class

Recommend this page to a friend!

      Top level forums  >  PHP Specialists  >  General  >  Multiple upload class  
Subject:Multiple upload class
Summary:Problem with multiple uploads
Messages:2
Author:w33bs r3db0y
Date:2010-04-17 12:42:14
Update:2010-04-21 04:09:56
 

  1. Multiple upload class   Reply   Report abuse  
Picture of w33bs r3db0y w33bs r3db0y - 2010-04-17 12:42:14
I need to upload more than 1 file to the server and I look around web and found out it can be done with arrays, I use the fallowing code:

add_movie.php:
<?php
$max_file_size = 1048576;
if(isset($_POST['submit'])) {
$movie = new Movie();
$n = 2;//nr of files
for($i=1; $i<=$n; $i++) {
$movie->attach_file($_FILES['file']);
}
if($movie->save($id=NULL)) {

// Success
$session->message("Movie uploaded successfully.");
redirect_to('movies.php');
} else {
// Failure
$message = join("<br />", $movie->errors);
}
}
?>
<form action="add_movie.php" enctype="multipart/form-data" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" />
<input type="file" name="file[]" size="70" />
<input type="file" name="file[]" value="" size="70" />
<input type="submit" name="submit" value="Add Movie" size="70" />
</form>

movie.php //class

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'];

return true;
}
}

public function save($id=NULL, $ok=NULL) {

// Can't save if there are pre-existing errors
if(!empty($this->errors)) { return false; }

$n=2;
if(isset($ok)) {
for($i=1; $i<=$n; $i++) {
// Can't save without filename and temp location
if(empty($this->file[$i]) || empty($this->temp_path[$i])) {
$this->errors[$i][] = "The file location was not available.";
return false;
}

// Determine the target_path
$target_path = SITE_ROOT .DS. 'public' .DS. $this->upload_dir .DS. $this->file[$i];

// Make sure a file doesn't already exist in the target location
if(file_exists($target_path)) {
$this->errors[$i][] = "The file {$this->file[$i]} 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[$i][] = "The file upload failed, possibly due to incorrect permissions on the upload folder.";
return false;
}
}
} else {
return true;
}
}
public function image_path($i=1) {
return $this->upload_dir."/".$this->file[$i];
}

This is a part of the code, I didn't put it all because it is big and hard to fallow. I believe that the problem is at function attach_file, I need somehow to run the function for every file, I tried using arrays but I failed. Can anyone please help me ?

PS: Sorry for my grammar, it's not my native language, Thank you.

There is 1 reply in this thread, which is not being displayed.
Browsing this forum thread replies is available only to premium subscribers.


Go to the premium subscriptions page to learn how to become a premium subscriber and have full access to this forum.