5 Replies - 422 Views - Last Post: 07 September 2012 - 10:04 AM Rate Topic: -----

#1 c9-adams  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 12-December 11

Uploading a profile picture

Posted 07 September 2012 - 08:26 AM

Hi guys,

I am currently trying to create a function on my website that allows a user to upload a profile picture. It uploads ok but does not show the uploaded picture. This is what I have tried so far:

<?php
 define ("MAX_SIZE","1000"); 

//This function reads the extension of the file. It is used to determine if the
// file  is an image by checking the extension.
 function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }

//This variable is used as a flag. The value is initialized with 0 (meaning no 
// error  found)  
//and it will be changed to 1 if an errro occures.  
//If the error occures the file will not be uploaded.
 $errors=0;
//checks if the form has been submitted
 if(isset($_POST['Submit'])) 
 {
 	//reads the name of the file the user submitted for uploading
 	$image=$_FILES['image']['name'];
 	//if it is not empty
 	if ($image) 
 	{
 	//get the original name of the file from the clients machine
 		$filename = stripslashes($_FILES['image']['name']);
 	//get the extension of the file in a lower case format
  		$extension = getExtension($filename);
 		$extension = strtolower($extension);
 	//if it is not a known extension, we will suppose it is an error and 
        // will not  upload the file,  
	//otherwise we will do more tests
 if (($extension != "jpg") && ($extension != "jpeg") && ($extension !=
 "png") && ($extension != "gif")) 
 		{
		//print error message
 			echo '<h3>Unknown extension!</h3>';
 			$errors=1;
 		}
 		else
 		{
//get the size of the image in bytes
 //$_FILES['image']['tmp_name'] is the temporary filename of the file
 //in which the uploaded file was stored on the server
 $size=filesize($_FILES['image']['tmp_name']);

//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*1024)
{
	echo '<h1>You have exceeded the size limit!</h1>';
	$errors=1;
}

//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images 
//folder)
$newname="images/".$image_name;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied) 
{
	echo '<h3>Copy unsuccessfull!</h3>';
	$errors=1;
}}}}
 ?>
<!Doctype html>
<html>
<head>
<title>
Member Home Page
</title>
</head>
<body>
<form name="newad" method="post" enctype="multipart/form-data"  
action="">
 <table>
 	<tr><td><input type="file" name="image" id="file"></td></tr>
 	<tr><td><input name="Submit" type="submit" value="Upload image" id="image_upload">
       </td></tr>
 </table>	
 </form>



Is This A Good Question/Topic? 0
  • +

Replies To: Uploading a profile picture

#2 BenignDesign  Icon User is online

  • I cause cancer.
  • member icon




Reputation: 4660
  • View blog
  • Posts: 8,887
  • Joined: 28-September 07

Re: Uploading a profile picture

Posted 07 September 2012 - 08:30 AM

If it uploads okay, then the problem is in the display code. I don't see in this code where you're trying to display the uploaded image.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9038
  • View blog
  • Posts: 33,526
  • Joined: 27-December 08

Re: Uploading a profile picture

Posted 07 September 2012 - 08:31 AM

So where are you trying to show the image? And you should also work on your code indentation and formatting conventions. Having four end-braces on the same line isn't helpful. The end braces help show where a block of code like a conditional or function ends.
Was This Post Helpful? 0
  • +
  • -

#4 c9-adams  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 12-December 11

Re: Uploading a profile picture

Posted 07 September 2012 - 08:36 AM

Yes I think thats where I am struggling. How should I display the image? What code would I have to use for this?

I am trying to display the image just above the upload button.
Was This Post Helpful? 0
  • +
  • -

#5 BenignDesign  Icon User is online

  • I cause cancer.
  • member icon




Reputation: 4660
  • View blog
  • Posts: 8,887
  • Joined: 28-September 07

Re: Uploading a profile picture

Posted 07 September 2012 - 08:47 AM

I would start with standard HTML.

<img src="filename.jpg" alt="MyFile" />


Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9038
  • View blog
  • Posts: 33,526
  • Joined: 27-December 08

Re: Uploading a profile picture

Posted 07 September 2012 - 10:04 AM

Let's do a little thinking here. How do you know what image to display? Or perhaps more to the point, how are you storing the list of files (or their paths)? Big hint- use a database. When you need persistent data, you need a database generally. And databases go hand-in-hand with web applications.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1