sabadell Posted June 23, 2006 Share Posted June 23, 2006 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>Insertform 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> </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 detailsmysql_select_db($database_Punks_conn, $Punks_conn); $sql = "INSERT INTO punks (FirstName, Surname, `Group`, Photo1) VALUES ('$name','$surname','$group', '$photo')";//execute the SQL Statementif (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 Quote Link to comment https://forums.phpfreaks.com/topic/12729-file-uploads/ Share on other sites More sharing options...
Wildbug Posted June 23, 2006 Share Posted June 23, 2006 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 Link to comment https://forums.phpfreaks.com/topic/12729-file-uploads/#findComment-48799 Share on other sites More sharing options...
sabadell Posted June 23, 2006 Author Share Posted June 23, 2006 [!--quoteo(post=387176:date=Jun 23 2006, 04:39 PM:name=Wildbug)--][div class=\'quotetop\']QUOTE(Wildbug @ Jun 23 2006, 04:39 PM) [snapback]387176[/snapback][/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. Quote Link to comment https://forums.phpfreaks.com/topic/12729-file-uploads/#findComment-48847 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.