Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Upload File?

Status
Not open for further replies.

zishan876

Programmer
Mar 19, 2007
61
US
Hi all:
I am trying to:
1. Let the user place in there info
2. upload there picture
3. Take them to the page to see list of schools..
Now here is my code so far but what I do not know how to do is:
1. upload the picture storing it in my sphotos folder?
2. letting the use view a list with there school info and viewing there school picture with it in a list..
Code:
<?php
$hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost
$db_user = "lala"; // change to your database password
$db_password = "password"; // change to your database password
$database = "lala"; // provide your database name
$db_table = "school"; // leave this as is

$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database,$db);

// Set global variables to easier names
$name = $_POST['school'];
$site = $_POST['site'];
$street = $_POST['street'];
$street2 = $_POST['street2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$speciality=$_POST['comments'];



if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    //echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("sphotos/" . $name . "_" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "sphotos/" . $name . "_" . $_FILES["file"]["name"]);
      echo "Stored in: " . "sphotos/" . $name . "_" . $_FILES["file"]["name"];
	  $photo = $_POST['/sphotos/' . $name . '_' . $_FILES['file']['name']'];
      }
    }
  }
else
  {
  echo "Invalid file";
  }

$sql = "INSERT INTO $db_table (name,site,street,street2,city,state,zip,email,phone,photo) values ('{$name}','{$site}','{$street}','{$street2}','{$city}','{$state}','{$zip}','{$email}','{$phone}','{$photo}');";
if($result = mysql_query($sql ,$db)) {
echo '<h1>Thank you , Your School has been entered into our database - <a href="/schools.php"><b>See your School</b></a></h1><br><br>';
} else {
echo "ERROR: ".mysql_error();
}
?>
Please help???
 
1. You are already doing this, using the move_uploaded_file() function to place the file where you want it.

2. Query your table to show the school info, in your table you are storing the name to the photo, so its just a matter of using that path and surround it by the html img tag to show the picture.

Of Note however is that you are not storing the full path to the image. But rather just the image name.

You will need to supply the rest of the path information when displaying the image.

At its most basic:
Code:
... <img src=$pathtophotofromDB> ....





----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Ok, Thank you for the quick response, But I keep getting this error:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ']' in /home5/bharatki/public_html/mmaxtra.com/school2.php on line 107

I can't not figure it out???

 
Which one is line 107?

What are you attempting to do in that line and the few before it?

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Basically what I am trying to do is the following:
1. Let the user upload a picture during registering there school.
2. The picture gets stored into sphotos directory.
3. Also the info gets stored into the database, but the photo is stored as a path to where the picture is in the directory.
4. Then let the user see there school in the list with the pic of there school.
I do not know where am I going wrong??
Line 107 is
Code:
$sql = "INSERT INTO $db_table (name,site,street,street2,city,state,zip,email,phone,photo) values ('{$name}','{$site}','{$street}','{$street2}','{$city}','{$state}','{$zip}','{$email}','{$phone}','{$photo}');";
if($result = mysql_query($sql ,$db)) {
Thanks
 
what is the line before this. in debugging php you should always look before the reported line too.
 
This looks to be the problem:

Code:
 $photo = $_POST['/sphotos/' . $name . '_' . $_FILES['file']['name'][red]'[/red]];

You have a stray quote there.

With that said I don't think the $POST variable would contain that index. I mean why would your form have an element with a name like "/sphotos/someschool_photoname.jpg";

I'm not sure what exactly you want to do there, but I'm pretty sure it's not that.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top