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.
PHPImages and Photos

Avatar of undefined
Last Comment
SuprSpy79

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Rob

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
SuprSpy79

Thanks I will take a look at that later today.
ASKER
SuprSpy79

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

ASKER
SuprSpy79

Great, that worked.

Only question is, what happens if the file name exists? can we auto append a number to it until its unique?
Your help has saved me hundreds of hours of internet surfing.
fblack61
Rob

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 :)
ASKER
SuprSpy79

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.
Rob

are you using file_exists?

if (file_exists($filename)) {
   echo "The file $filename exists";
} else {
   echo "The file $filename does not exist";
}
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
SuprSpy79

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

Rob

// add this before the if statement

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

$uploadfile = $filename;
ASKER
SuprSpy79

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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Rob

what is your file size upload limit in your php.ini?
ASKER
SuprSpy79

not sure, but im using the same file i was testing successfully before - its only 55k
Rob

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();

?>
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
SuprSpy79

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.
Rob

Check your disk space on the server is it possible you would've used that up?
ASKER
SuprSpy79

not a chance lol
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Rob

please repost your php code i don't think i declared the $i variable to 0
ASKER
SuprSpy79


	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

Rob

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

$i = 0;

while (....


Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
SuprSpy79

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

ASKER
SuprSpy79

any ideas? im stumped.
Rob

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 started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER
SuprSpy79

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"];
      }