4 Replies - 368 Views - Last Post: 19 November 2012 - 02:29 PM Rate Topic: -----

#1 Tenderfoot  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 160
  • Joined: 21-March 12

Handling photo upload

Posted 19 November 2012 - 01:08 PM

Hello good people of the PHP forum.

I'm following a book on how to use PHP to handle file uploads but it is a little vague on the details. I'm going to post my attempt and perhaps you could tell me where I went wrong. But first: What I'm trying to do is to upload a photo.

And to explain a bit further, I was supposed to use a class to handle the file upload. I'm finding it a bit hard to find content on exactly that online.

What I was hoping to do with the code was this, it all starts with: <form action="class.Upload.php>. What I thought this line would do would be to call the class.Upload.php (which is in the same folder) when I click submit.

Now, in the class.Upload.php file I create the class Upload. In there I have a function that I thought (going by the book I'm reading) would upload the file to the folder my project is in, under a folder called "myndir". I then have a __construct in the Upload class that runs the function that I attempted to use to upload my file. However, when I pick a file and click submit, a blank page appears, but at least it went to localhost/CorrectDirector/class.Upload.php

So what am I doing wrong?

Here's my form:
<!DOCTYPE html>
<html>
	<head>
		<title>SuperForm</title>
	</head>
	<body>
	<!-- kennitölu, aldur, nafn, heimilisfang, símanúmer, tölvupóstfang og mynd. -->
		<form action="class.Upload.php" enctype="multipart/form-data" method="post">
		<label for="kennitala">Kennitala: </label>
		<input type="text" name="kennitala" />
		<label for="aldur">Aldur: </label>
		<input type="number" name="aldur" />
		<label for="nafn">Nafn: </label>
		<input type="text" name="nafn" />
		<label for="heimilisfang">Heimilisfang: </label>
		<input type="text" name="heimilisfang" />
		<label for="simanumer">Símanúmer: </label>
		<input type="text" name="simanumer" />
		<label for="mynd">Mynd: </label>
		<input type="file" name="mynd" />
		<input type="submit" name="submit" value="Submit" />
		</form>
	</body>
</html>



And here's my code:
class Upload
{
	function __construct()
	{
		$this->handleUpload(); 
	}
	
	function handleUpload()
	{
		define ("FILEREPOSITORY", "/myndir");
		
		$name = $_POST($_POST['name']);
		$result = move_uploaded_file($_FILES['mynd']['tmp_name'], FILEREPOSITORY.$_FILES['mynd']['name']);
		
		if ($result == 1)
		{
			echo "<p>File successfully uploaded.</p>";
		}
		
		else
		{
			echo "<p>There was a problem uploading the file.</p>";
		}
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Handling photo upload

#2 Tenderfoot  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 160
  • Joined: 21-March 12

Re: Handling photo upload

Posted 19 November 2012 - 01:16 PM

The problem is, or at least, one of the problems is, it won't go into the handleUpload() function - despite my calling it in the constructor.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: Handling photo upload

Posted 19 November 2012 - 02:00 PM

$name = $_POST($_POST['name']);


POST POST? :unsure:

form action="class.Upload.php"


I think you need to post to a script that creates an Upload object, but I can't say for sure. Never tried going directly to a class like that in a form action.
Was This Post Helpful? 1
  • +
  • -

#4 Tenderfoot  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 160
  • Joined: 21-March 12

Re: Handling photo upload

Posted 19 November 2012 - 02:16 PM

View PostJackOfAllTrades, on 19 November 2012 - 02:00 PM, said:

$name = $_POST($_POST['name']);


POST POST? :unsure:/>/>


I haven't the slightest, I'm taking this directly out of a book, I swear. :unsure:

View PostJackOfAllTrades, on 19 November 2012 - 02:00 PM, said:

I think you need to post to a script that creates an Upload object, but I can't say for sure. Never tried going directly to a class like that in a form action.


Did that literally seconds before I saw your post. This time it goes inside the function and all, I just need to sort things like.. well.. the one you pointed out at first. Will see about the rest. Thanks for the reply.

This post has been edited by Tenderfoot: 19 November 2012 - 02:17 PM

Was This Post Helpful? 0
  • +
  • -

#5 Tenderfoot  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 160
  • Joined: 21-March 12

Re: Handling photo upload

Posted 19 November 2012 - 02:29 PM

Working code below:

I also changed the form action part to file_upload.php, which is where I require class.Upload.php, and instantiate an object. The object then uses the __construct which calls the handleUpload function. It does work but I'll probably refine it a bit. One thing I found interesting is that when I go to the folder I save it to I don't have access to view the picture, apparently.

class Upload
{
	function __construct()
	{
		$this->handleUpload(); 
	}
	
	function handleUpload()
	{
		$result = move_uploaded_file($_FILES["mynd"]["tmp_name"], "myndir/" . $_FILES["mynd"]["name"]);
		
		echo "Stored in: " . "upload/" . $_FILES["mynd"]["name"];
		
		if ($result == 1)
		{
			echo "<p>File successfully uploaded.</p>";
		}
		
		else
		{
			echo "<p>There was a problem uploading the file.</p>";
		}
	}
}


This post has been edited by Tenderfoot: 19 November 2012 - 02:30 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1