Wrox Programmer Forums
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 12th, 2014, 09:57 AM
Registered User
 
Join Date: Feb 2014
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Image upload

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Upload image-create & save thumbnail-display image angshujit ASP.NET 2.0 Professional 6 July 11th, 2013 10:34 PM
How to: Upload image to server and save image name in db with other form info Clint PHP How-To 1 October 26th, 2009 05:50 AM
Upload image phuc2583 Classic ASP Basics 1 January 17th, 2008 08:48 AM
Image Upload spbharti Pro PHP 3 February 3rd, 2006 08:20 AM
Image upload cutesneakers PHP How-To 2 January 5th, 2005 08:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.