Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple file upload with limits

Status
Not open for further replies.

d0nny

IS-IT--Management
Dec 18, 2005
278
GB
OK, I'm now stuck. I had started asking questions in another thread on this forum, but I'm getting loads of script problems now so I thought I would ask a new question.

I have a form which allows users to upload the CV's (resume's) online. I was fine with this as I had checks in my script for filesize and also filetype, then I would upload the file into a directory on my webserver.
Now, they want a field in the form for uploading a photo(!), so now I need to have 2 upload fields in my form and I assume my script needs to be hugely amended to do these checks (filetype and filesize).

I am also storing these filenames (not the full path) in my DB too.

Any ideas/help?
 
you have not shared your script with us. it's unlikely to need much changing though.
 
Sorry...

My form currently has this:

Code:
<input name="url" type="file" size="30">

And my script has this:

Code:
$url = addslashes($_POST["url"]);
.
.
.
$upload_Name = $_FILES['url']['name'];
$upload_Size = $_FILES['url']['size'];
$upload_Temp = $_FILES['url']['tmp_name'];
$upload_Mime_Type = $_FILES['url']['type'];

I can then do a check on the filesize limit and also the file type.
I then set the path, move the temp file to the path location, chmod the file and also set a file_URL variable using the full server path (for later inclusion in an email).

But now I want to add another file upload field in my form and do the same tests.
Any ideas?
 
name your inputs like this
Code:
<input name="url[red][][/red]" type="file" size="30">

then your processing code should look like this

Code:
if (isset($_FILES['url'])){
	$file = new processFileUploads($_FILES['url']);
}

class processFileUploads{

	public function processFileUploads($files, $saveDirectory){
		foreach ($files as $file){
			$_file = new uploadedFile($file);
			if ($_file->isError){
				$file->displayError();
				exit;
			}
			$file->save($saveDirectory);
			if ($_file->isError){
				$file->displayError();
				exit;
			}
			$file->displaySuccess();
		}
	}

}

class uploadedFile {
	public $type;
	public $tmpName;
	public $isError = true;
	public $error;
	public $size;
	public $name;
	
	function uploadedFile($file){
		$this->type = $file['type'];
		$this->name = $file['name'];
		$this->tmpName = $file['tmp_name'];
		$this->size = $file['size'];
		$this->error = $this->handleErrors($file['error']);
	}	
	
	public function save($dir){
		
		if (!is_dir($dir)){
			$this->isError = true;
			$this->error = 'Directory does not exist';
			return;
		}
		
		if(!is_writable($dir)){
			$this->isError = true;
			$this->error = 'Directory is not writable for this user';
			return;	
		}
		
		$filename = $this->getFileName($dir);
		$result = move_uploaded_file($this->tmpName, $dir . DIRECTORY_SEPARATOR . $filename);
		
		if (!$result){
			$this->isError = true;
			$this->error = "There was a problem saving the uploaded file {$this->name}";
		}
	}
	public function displayError(){
		echo <<<HTML
<div style="color:red; width:50%; padding: 15px 0; margin:0 auto; border: 1 solid dotted;">
{$this->error}
</div>
HTML;
	}
	
	public function displaySuccess(){
		echo "The uploaded file {$this->name} has been successfully saved";
	}
	
	private function getFileName($dir){
		//might want to use this helper function to check for existing files with the same name
		return $this->name;
	}
	
	private function handleErrors($error){
		switch ($error) {
			case 0:
				$this->isError = false;
				return 'OK';
				break;
			case 1:
			case 2:
				return "File {$this->name} is too large ({$this->size})";
				break;
			case 3:
				return "File {$this->name} did not fully upload";
				break;
			case 4:
				return 'No file was uploaded';
				break;
			case 6:
				return 'File upload cannot be processed as there is no temp directory available';
				break;
			case 7:
				return "Problem writing {$this->name} to disk. Permissions issue?";
				break;
			case 8:
				return 'Another extension is preventing the upload from working';
				break;
		}
	}
}
 
Thanks for this.
I haven't tried it out yet but will do today.

Have you any idea how I might update my database using the filenames from this script?
I currently have 2 fields in my database (one called url and the other called photo, but I can change them), so I would want these fields updated with the relavent filename.
Do I simply setup a variable like this:
Code:
$url_Name = $_FILES['url']['name'][0];
$photo_Name = $_FILES['url']['name'][1];
and then use these variables in my INSERT script?
 
nope. the data is in an array one level higher. $_FILES['url'][0]['name']

to do this i would add a method to the second class

Code:
public function store(){
  $sql = "Insert into tablename (id, name) values (NULL,'".mysql_real_escape_string($this->name)."');
  $result = mysql_query($sql);
  if (!$result) {
    $this->isError  = true;
    $this->error = mysql_error();
    return ;
  } else {
    $this->lastInsertId = mysql_insert_id();
  }
}

if you're going to store pointers in the database i'd recommend doing the storage BEFORE you do the save. then use the value of $this->lastInsertId as the filename for saving the file. i'd also recommend you store the file type and the file size in the database.

obviously to call the store method you would add the line

Code:
$file->store();
before the save line. and then add some error checking in the same way that is currently in place.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top