Jump to content


Photo

File uploads


  • Please log in to reply
2 replies to this topic

#1 sabadell

sabadell
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 23 June 2006 - 02:00 PM

Hi, I need a bit of help with a small project which is using PHP and MYSQL. Basically, I need to upload images to the server which I have done and also add an entry to a mysql database which I have also done. My problem is I don't know how to write the image name eg. MyPhoto.jpg to the dbase field photo1 that I have create in a table and also I need to be able to limit the types of uploaded files to jpg and gif extension. I hope it is quite straight forward and would appreciate any help from outthere! Thescripts I am using are as follows: Thanks in advance.


Upload form.............


<html>
<head>
<title>insert form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<p>Insert
form for Rock Stars</p>
<form action="formfileinsert.php" method="post" enctype="multipart/form-data" name="form1">
<table width="386" border="0" cellspacing="1" cellpadding="0">
<tr>
<td width="73">Name</td>
<td width="310"><input name="Firstname" type="text" id="Firstname"></td>
</tr>
<tr>
<td>Surname</td>
<td><input name="Surname" type="text" id="Surname"></td>
</tr>
<tr>
<td>Group</td>
<td><input name="Group" type="text" id="Group">
</td>
</tr>
<tr>
<td>Photo</td>
<td><input name="photo" type="file" id="photo">
<input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
<p>&nbsp;
</p>
</body>
</html>


::::::::::::::::::The upload script::::::::::::::::::::::::::::::::::::::


<html>
<?php require_once('../../Connections/Punks_conn.php'); ?>

<head>
<title>form variable insert</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
$name = $_POST[Firstname];
$surname =$_POST[Surname];
$group = $_POST[Group];
$photo = $_POST[photo];



//folder creation - change in permissions to 777 Chmod
$file_dir = "../test6/uploads/";
if( !file_exists( $file_dir ) )
{
mkdir( $file_dir );
chmod( $file_dir, 0777 );

}
?>



<?php
foreach($_FILES as $file_name => $file_array) {
echo "path: ".$file_array['tmp_name']."<br>\n";
echo "name: ".$file_array['name']."<br>\n";
echo "type: ".$file_array['type']."<br>\n";
echo "size: ".$file_array['size']."<br>\n";

if (is_uploaded_file($file_array['tmp_name'])) {
move_uploaded_file($file_array['tmp_name'], "$file_dir/$file_array[name]") or die ("Couldn't copy");
echo "file was uploaded!<br><br>";
}
}

?>





<?php // upload the form details

mysql_select_db($database_Punks_conn, $Punks_conn);
$sql = "INSERT INTO punks (FirstName, Surname, `Group`, Photo1) VALUES ('$name','$surname','$group', '$photo')";
//execute the SQL Statement
if (mysql_query($sql, $Punks_conn)) {
echo "new punk created";
}
else {
echo "This Punk never made it";
}
//error identifier
?>

</body>
</html>

Thanks for any help received

#2 Wildbug

Wildbug
  • Members
  • PipPipPip
  • Advanced Member
  • 1,149 posts

Posted 23 June 2006 - 02:39 PM

It's in the PHP manual: [a href=\"http://www.php.net/manual/en/features.file-upload.php\" target=\"_blank\"]File uploads[/a].

Basically the $_FILES superglobal will contain the name(s) of the files being uploaded as well as the mimetype (image/gif or image/jpeg, etc).

If you don't trust the reported mimetype or file extention, you can also check the file itself for the existence of the appropriate file header for that particular file format. I think jpegs start with "EXIF" and gifs start with "GIF8..." something. I can't recall off the top of my head; try Google if that interests you.
Twice a day my clock works PERFECTLY!  I can't figure out what's wrong with it.

#3 sabadell

sabadell
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 23 June 2006 - 04:55 PM

[!--quoteo(post=387176:date=Jun 23 2006, 04:39 PM:name=Wildbug)--][div class=\'quotetop\']QUOTE(Wildbug @ Jun 23 2006, 04:39 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
It's in the PHP manual: [a href=\"http://www.php.net/manual/en/features.file-upload.php\" target=\"_blank\"]File uploads[/a].

Basically the $_FILES superglobal will contain the name(s) of the files being uploaded as well as the mimetype (image/gif or image/jpeg, etc).

If you don't trust the reported mimetype or file extention, you can also check the file itself for the existence of the appropriate file header for that particular file format. I think jpegs start with "EXIF" and gifs start with "GIF8..." something. I can't recall off the top of my head; try Google if that interests you.
[/quote]



Thanks for your advice but as someone who is very new to php and mysql saying read the manual is a little bit throw away frankly as I assure you that I am slowly doing so albeit it is never an easy thing to read, digest and then use. Even when you began I am that you had the same experience after these forums wouldn't exist if this wasn't the case.

So I would really appreciate a little bit of advice that is far more basic and explicit if you can. Thanks once again and I know that my issue is not a major one to most on this forum.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users