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!

File Upload Data verification

Status
Not open for further replies.

overflo

Technical User
Feb 10, 2003
70
AU
I am trying to verify if the field is empty, (Have verifying if .jpg or .gif working)

I have tried these:
Code:
if (empty($_"FILES['image']['tmp_name'])){
$message = "Please choose and image";
}
if ($_"FILES['image']['size']===0){
$message = "Please choose and image";
}
if ($_"FILES['image']['size']==0){
$message = "Please choose and image";
}
if ($_"FILES['image']['tmp_name']=="")){
$message = "Please choose and image";
}
and all sorts of variations

It either comes up that there is no image selected whether there is or not - or there is an image selected whether there is or not.

Any help greatly appreciated
 
That's a typo (long day), and then I copied and pasted. The actual code doesn't have them in.
it should read
Code:
if (empty($_FILES['image']['tmp_name'])){
$message = "Please choose and image";
}
if ($_FILES['image']['size']===0){
$message = "Please choose and image";
}
if ($_FILES['image']['size']==0){
$message = "Please choose and image";
}
if ($_FILES['image']['tmp_name']=="")){
$message = "Please choose and image";
}
 
something like this reusable class might help you validate the uploads a bit better.

Code:
<?php
//assume fieldname is called myField

$upload = new Upload('myField');
if ($upload->isError){
	echo $upload->getErrorMessage();
	die;
} else {
	$upload->saveFile('new/path/to/file.jpg');
	if ($upload->isError()){
		echo $upload->getErrorMessage();
		die;
		
	} else {
		echo "File uploaded and saved correctly";
	}
}

class upload{
	private $fieldname = '';
	private $errorMessage = array();
	public $isError = false;
	
	public function upload($fieldname){
		if (isset($_FILES[$fieldname])){
			$this->fieldname = $fieldname;
			$this->checkError();
		}	else {
			$this->isError = true;
			$this->errorMessage = 'No file was uploaded';
		}
	}
	
	private function checkError(){
		$msg = '';
		if ($this->isError) return;
    	switch ($_FILES[$this->fieldname]['error']){
            case 0:
                break;
            case 1:
            case 2:
                $msg = "The uploaded file is too large"; 
                break;
            case 3:
                $msg = "The uploaded file was only partially uploaded";
                break;
            case 4:
                $msg = "No file was uploaded";
                break;
            case 6:
            case 7:
            case 8;
                $msg = "There was a problem saving ths uploaded file.";
                break;
        }
		$this->errorMessage[] = $msg;
		$this->isError = $msg === '' ? false : true;
    }
    
	public function getErrorMessage(){
		$output = '';
		foreach ($this->errorMessage as $m){
			$output .= <<<HTML
		<div style="color:red; padding:10px; width: 60%; border:dotted black thin;">
		$m
		</div>
HTML;
		}
		return $output;
	}
	
	public function saveFile($path){
		if (move_uploaded_file($_FILES[$this->fieldname]['tmp_name'])){
			return true;
		} else {
			$this->isError = true;
			$this->errorMessage[] = 'Problem copying the file to ' . $path . '. This is usually a permissions issue.';
		}
	}
}
?>
 
Thanks jpadie, I'll give it a go

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top