簡體   English   中英

OOP在文件夾中上傳圖像

[英]OOP upload images in the folder

我正在將圖片上傳到文件夾中,應用程序正常工作,但是第一次打開頁面上傳圖片時,我收到的錯誤消息很少,當我上傳圖片時,沒有錯誤消息結束,我可以上傳盡可能多的我想要的而沒有錯誤。 當我第一次單擊或重新加載頁面時,出現錯誤,並且在第一次上傳后錯誤消失了。

我有文件夾圖像

File: pictureupload.php
File: classimage.php

pictureupload.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>

classimage.php的代碼是

<?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

該錯誤來自文件classimage.php

$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

那么$_FILES['picture']不會在您第一次加載頁面時設置,因為您還沒有上傳任何圖像,因此在執行時:

$img= new images();

在構造函數中,您想從尚未設置的$ _FILES ['picture']獲取日期。

您應該做的是先檢查$ _FILES數組,然后再執行其他操作。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM