2014-03-12 3 views
0

에 이미지를 업로드하고 있습니다. 응용 프로그램이 작동하지만, 이미지를 업로드 할 때 처음 페이지를 열면 이미지를 업로드 할 때 몇 가지 오류 메시지가 나타납니다. 오류 메시지 끝 오류없이 원하는만큼 업로드 할 수 있습니다. 다시 클릭하거나 처음 페이지를 새로 고침하면 오류가 발생하고 처음으로 오류를 업로드 한 후 사라집니다. 나는이가 내가 사진을 업로드 할 때OOP 폴더

내가 폴더 이미지가 pictureupload.php에 대한

File: pictureupload.php 
File: classimage.php 

코드가 classimage.php에 대한

<?php 
include_once 'classimage.php'; 
$img= new images(); 

$img->uploadImages(); 

?> 
<!DOCTYPE HTML> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>Untitled Document</title> 
</head> 

<body> 
<form action="index.php" method="post" enctype="multipart/form-data"> 
<input type="file" name="picture" > 
<br><br> 

<input type="submit" name="upload" value="Upload Picture"> 
</form> 
</body> 

</html> 

코드

<?php 
class images 
{ 
protected $fileName; 
protected $fileTmp; 
protected $fileType; 
protected $fileSize; 
protected $fileError; 

public function __construct() 
{ 
$this->fileName = $_FILES['picture']['name']; 
$this->fileTmp = $_FILES['picture']['tmp_name']; 
$this->fileSize = $_FILES['picture']['size']; 
$this->fileError = $_FILES['picture']['error']; 
$this->fileType =$_FILES['picture']['type']; 
} 

public function uploadImages() 
{ 
//if Button push 
if(isset($_POST['upload']) && $_POST['upload']=='Upload Picture') 
{ 
//get type after . 
$kabom = explode(".", $this->fileName); 
//get image type 
$fileExtnsion = end($kabom); 
$fileName = time().rand().".".$fileExtnsion; 

if(!$this->fileTmp) 
{ 
echo "Error: Please browse for a file before clicking the upload button"; 
exit(); 
} 
elseif ($this->fileSize > 5242880) 
{ 
echo "Error: Your file was large than 5MB"; 
unlink($this->fileTmp); 
exit(); 
} 
elseif (!preg_match("/.(gif|jpg|png)$/i", $this->fileName)) 
{ 
echo "ERROR: Your image was not .gif, .jpg, or .png."; 
unlink($this->fileTmp); 
exit(); 
} 


elseif ($this->fileError==1) 
{ 
echo "Error.Please try again"; 
exit(); 
} 
$movePicture = move_uploaded_file($this->fileTmp, "images/$fileName"); 
if($movePicture == false) 
{ 
echo "Error File not uploaded."; 
exit(); 
} 
} 
} 
} 
?> 

처음이다 오류

Notice: Undefined index: picture in C:\xampp\htdocs\www\imageupload\test\classimage.ph p on line 12 

Notice: Undefined index: picture in C:\xampp\htdocs\www\imageupload\test\classimage.ph p on line 13 

Notice: Undefined index: picture in C:\xampp\htdocs\www\imageupload\test\classimage.ph p on line 14 

Notice: Undefined index: picture in C:\xampp\htdocs\www\imageupload\test\classimage.ph p on line 15 

Notice: Undefined index: picture in C:\xampp\htdocs\www\imageupload\test\classimage.ph p on line 16 

이 오류는 실행할 때 따라서,

$this->fileName = $_FILES['picture']['name']; - line12 
$this->fileTmp = $_FILES['picture']['tmp_name']; - line 13 
$this->fileSize = $_FILES['picture']['size']; - line 14 
$this->fileError = $_FILES['picture']['error']; - line 15 
$this->fileType =$_FILES['picture']['type']; - line 16 
+2

만')()'(A'새로운 이미지를 요구한다 . 즉, 사용자가 페이지를 제출할 때까지 참조 할 '$ _FILES'가 없기 때문에 오류가 표시됩니다. – sbeliv01

답변

0

파일 classimage.php에서 어떤 이미지를 업로드하지 않았기 때문에 당신이 당신의 페이지를로드 처음 설정되지 않습니다 음 $_FILES['picture']입니다 :

$img= new images(); 

생성자에서 아직 설정되지 않은 $ _FILES [ 'picture']에서 날짜를 가져 오려고합니다.

다른 작업을 수행하기 전에 먼저 $ _FILES 배열을 검사해야합니다. 당신은 이미지 업로드 페이지에서 클래스를 포함하는 경우 실제로`$ _FILES`가 제대로`__construct 거기에 할당 할 때

public function __construct() 
    if (count($_FILES) > 0) { 
    // constructor code. 
    } 
}