I'm working uploading picture into the folder, applications work, but first time when I open the page to upload a image I have few error message, when I upload picture then there no error message end I can uploading as many I want without error. When I again click or reload page first time a have error and after first uploading errors gone.
I have folder images
File: pictureupload.php
File: classimage.php
Code for pictureupload.php is
<?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>
Code for classimage.php is
<?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();
}
}
}
}
?>
FIrst time when I upload picture I have this error
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 error is from file 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
Well $_FILES['picture']
will not be set the first time you load your page because you have not uploaded any image, hence when you execute:
$img= new images();
in the constructor you are tying to get date from $_FILES['picture'] which is not set yet.
What you should do is check the $_FILES array first before doing anything else.
public function __construct()
if (count($_FILES) > 0) {
// constructor code.
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.