Link to home
Start Free TrialLog in
Avatar of SuprSpy79
SuprSpy79

asked on

PHP Upload Images and Display Them

I need a VERY simple script that I can upload an image and display it on a page.

I have a field in my database with user name ,pass etc, and photo.

I know how i can take the user id and select the photo for that user.

my problem is i have no clue how to upload a picture to my server with a php script and how to enter the info in the db field and how to display it on the actually page. (3 columns)

any help is appreciated.
ASKER CERTIFIED SOLUTION
Avatar of Rob
Rob
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SuprSpy79
SuprSpy79

ASKER

Thanks I will take a look at that later today.
Ok here is my form now, the only thing im not sure of is the tmp_name and name vars you used in the php.

does this form look like it should work?
<?php
if (isset($_POST['submit'])) {
 
	$uploaddir = '/public_html/lymphaticmalformations/images/fampics/';
	$uploadfile = $uploaddir . basename($_FILES['Photo']['name']);
	$FirstName = $_POST['FirstName'];
	$LastName = $_POST['LastName'];
	$BBID = $_POST['BBID'];
	$Website = $_POST['Website'];
	$Email = $_POST['Email'];
	$Photo = $_POST['Photo'];
	$albumurl = $_POST['albumurl'];
	
	if (move_uploaded_file($_FILES['Photo']['tmp_name'], $uploadfile)) {
 	  mysql_query("INSERT INTO families (FirstName, LastName, Photo, albumurl, BBID, Website, Email) VALUES ('$FirstName', '$LastName', '$Photo', '$albumurl', '$BBID', '$Website', '$Email')") or die(mysql_error());  
	  echo "Your child was successfully registered. An administrator will look at your file and approve the account shorty. Once approved you will receive a login and password so you can update your listing at any time.\n";	
	mysql_close($db);
	} 
	else {
  	echo "File Size To Large. Please try again with a smaller file by using your browsers back button.\n";
	}
}
else {
?>
 
<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="registerfamily.php" method="POST">
<table>   
<tr>
	<td>Child's First Name: </td>
    <td><input type="text" name="FirstName" id="FirstName"/></td>
</tr>
<tr>
	<td>Child's Last Name: </td>
    <td><input type="text" name="LastName" id="LastName"/></td>
</tr>
<tr>
	<td>Message Board ID: </td>
    <td><input type="text" name="BBID" id="BBID"/></td>
</tr>
<tr>
	<td>Website: </td>
    <td><input type="text" name="Website" id="Website"/></td>
</tr>
<tr>
	<td>Email: </td>
    <td><input type="text" name="Email" id="Email"/></td>
</tr>
<tr>
	<td>Photo: (125 x 125 pixels) <input type="hidden" name="MAX_FILE_SIZE" value="30000" /></td>
	<td><input name="Photo" id="Photo" type="file" /></td>
</tr>
<tr>
	<td>Album Url (i.e. Photobucket): </td>
	<td><input name="albumurl" id="albumurl" type="text" /></td>
</tr>
<tr>
	<td><input type="submit" name="submit" id="submit" value="Submit" /></td>
    <td>&nbsp;</td>
</tr>
</table>
</form>
</p>
<?php
}
?>

Open in new window

Great, that worked.

Only question is, what happens if the file name exists? can we auto append a number to it until its unique?
yep exactly  i've just used a loop for this and while the file exists increment the number, when it doesn't exist that's the filename you use :)
u sure? because ive uploaded the same file a bunch of times resubmitting the same form (testing other things) and it just either overwrote it , or didnt upload it cuz it was ignored since it was there.
are you using file_exists?

if (file_exists($filename)) {
   echo "The file $filename exists";
} else {
   echo "The file $filename does not exist";
}
here is what im using
<?php
if (isset($_POST['submit'])) {
 
	$uploaddir = '/home/user/public_html/domain/images/';
	$uploadfile = $uploaddir . basename($_FILES['Photo']['name']);
	$FirstName = $_POST['FirstName'];
	$LastName = $_POST['LastName'];
	$BBID = $_POST['BBID'];
	$Website = $_POST['Website'];
	$Email = $_POST['Email'];
	$Photo = $_POST['Photo'];
	$albumurl = $_POST['albumurl'];
	
	if (move_uploaded_file($_FILES['Photo']['tmp_name'], $uploadfile)) {
	  $photolink = basename($_FILES['Photo']['name']);	
	  
	  $recipient = "info@email.com";
	  $subject = "Account Registration on website.com";
 
	  $message .= "<p>Thank you for registering on website.com. An administrator will review your information and you will recieve an email if you are approved. The process usually happens within 24 hrs. We do this to prevent fraudulant users from registering for this site</p>";
	  $message .= "<p>Below is a copy of the information you submitted. Once you are approved you will be able to login and edit this information</p>";
	  $message .= "<p>";
	  $message .= "First Name: $FirstName<br />";
	  $message .= "Last Name: $LastName<br />";
	  $message .= "Messageboard ID: $BBID<br />";
	  $message .= "Website: $Website<br />";
	  $message .= "Email: $Email<br />";
	  if ($photolink == "") {
	  $message .= "Photo Submitted: No<br />";
	  }
	  else {
	  $message .= "Photo Submitted: Yes<br />";
	  }
	  $message .= "Album Link: $albumurl<br />";
	  $message .= "</p>";
	
	  $headers .= "FROM: $Email\n";
	  $headers .= "Cc: $Email\n";
	  $headers .= "Reply-To: email.com\n";
	  $headers .= "MIME-Version: 1.0\n";
	  $headers .= "Content-type: text/html; charset=iso-8859-1\n";
	  
	  mail($recipient,$subject,$message,$headers,'-f info@lymphaticmalformations.com'); 
	  
	  mysql_query("INSERT INTO families (FirstName, LastName, Photo, albumurl, BBID, Website, Email) VALUES ('$FirstName', '$LastName', 'http://www.lymphaticmalformations.com/images/fampics/$photolink', '$albumurl', '$BBID', '$Website', '$Email')") or die(mysql_error());  
	  echo "You were successfully registered. An administrator will look at your file and approve the account shorty. Once approved you will receive a login and password so you can update your listing at any time.\n";	
	  mysql_close($db);
	} 
	else {
  	echo "File Size To Large. Please try again with a smaller file by using your browsers back button.\n";
	}
}
else {
 
DISPLAY FORM

Open in new window

// add this before the if statement

while (file_exists($filename)) {
    $filename = $uploadfile . "_" . $i++;
}

$uploadfile = $filename;
now its telling me the file is to large to move

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpSBY6qZ' to '' in address\egisterfamily.php on line 72
File Size To Large. Please try again with a smaller file by using your browsers back button.
what is your file size upload limit in your php.ini?
not sure, but im using the same file i was testing successfully before - its only 55k
did you remove the real file name? Unable to move '/tmp/phpSBY6qZ' to xxxxxxx'' in address\egisterfamily.php on line 72

write the following script and look for your file size upload setting

<?php

phpinfo();

?>
File Uploads       upload_max_filesize       Maximum allowed size for uploaded files.       10M

i havent touched anything, all i did was add the line you said and tried to upload the same file.
Check your disk space on the server is it possible you would've used that up?
not a chance lol
please repost your php code i don't think i declared the $i variable to 0

	while (file_exists($filename)) {
    $filename = $uploadfile . "_" . $i++;
    }
    $uploadfile = $filename;
 
	if (move_uploaded_file($_FILES['Photo']['tmp_name'], $uploadfile)) {
	  $photolink = basename($_FILES['Photo']['name']);	

Open in new window

but just before the while statement you have to specify that i =0 ie.

$i = 0;

while (....


same issue, for some reason that code is triggering a file size error.

Here is the whole form
<?php
if (isset($_POST['submit'])) {
 
	$uploaddir = '/home/suprspy7/public_html/lymphaticmalformations/images/fampics/';
	$uploadfile = $uploaddir . basename($_FILES['Photo']['name']);
	$FirstName = $_POST['FirstName'];
	$LastName = $_POST['LastName'];
	$BBID = $_POST['BBID'];
	$Website = $_POST['Website'];
	$Email = $_POST['Email'];
	$Photo = $_POST['Photo'];
	$albumurl = $_POST['albumurl'];
	
	$i = 0;
	while (file_exists($filename)) {
    $filename = $uploadfile . "_" . $i++;
    }
    $uploadfile = $filename;
 
	if (move_uploaded_file($_FILES['Photo']['tmp_name'], $uploadfile)) {
	  $photolink = basename($_FILES['Photo']['name']);	
	  
	  $recipient = "info@lymphaticmalformations.com";
	  $subject = "Account Registration on LymphaticMalformations.com";
 
	  $message .= "<p>Thank you for registering on LymphaticMalformations.com. An administrator will review your information and you will recieve an email if you are approved. The process usually happens within 24 hrs. We do this to prevent fraudulant users from registering for this site</p>";
	  $message .= "<p>Below is a copy of the information you submitted. Once you are approved you will be able to login and edit this information</p>";
	  $message .= "<p>";
	  $message .= "First Name: $FirstName<br />";
	  $message .= "Last Name: $LastName<br />";
	  $message .= "Messageboard ID: $BBID<br />";
	  $message .= "Website: $Website<br />";
	  $message .= "Email: $Email<br />";
	  if ($photolink == "") {
	  $message .= "Photo Submitted: No<br />";
	  }
	  else {
	  $message .= "Photo Submitted: Yes<br />";
	  }
	  $message .= "Album Link: $albumurl<br />";
	  $message .= "</p>";
	
	  $headers .= "FROM: info@lymphaticmalformations.com\n";
	  $headers .= "Cc: $Email\n";
	  $headers .= "Reply-To: info@lymphaticmalformations.com\n";
	  $headers .= "MIME-Version: 1.0\n";
	  $headers .= "Content-type: text/html; charset=iso-8859-1\n";
	  
	  mail($recipient,$subject,$message,$headers,'-f info@lymphaticmalformations.com'); 
	  
	  mysql_query("INSERT INTO families (FirstName, LastName, Photo, albumurl, BBID, Website, Email) VALUES ('$FirstName', '$LastName', 'http://www.lymphaticmalformations.com/images/fampics/$photolink', '$albumurl', '$BBID', '$Website', '$Email')") or die(mysql_error());  
	  echo "You were successfully registered. An administrator will look at your file and approve the account shorty. Once approved you will receive a login and password so you can update your listing at any time.\n";	
	  mysql_close($db);
	} 
	else {
  	echo "File Size To Large. Please try again with a smaller file by using your browsers back button.\n";
	}
}
else {
?>

Open in new window

any ideas? im stumped.
yes! i was using $filename as an example, really it should be $uploadfile as below

$i=0;
$file_parts = pathinfo($uploadfile);
while(file_exists($uploadfile)) {
    $uploadfile = $uploaddir . $file_parts['filename'] . "_" . $i++ . $file_parts["extension"];
}
I had to change it to add a period before the extention

      $i=0;
      $file_parts = pathinfo($uploadfile);
      while(file_exists($uploadfile)) {
    $uploadfile = $uploaddir . $file_parts['filename'] . "_" . $i++ . "." . $file_parts["extension"];
      }