Jump to content

Testing for attatched file


smartalco

Recommended Posts

Is there a way to test to see if a file is attatched or not on a registration form using php(sorta like testing for a blank text field, just a file)?I have tried a multitude of things and can't seem to get it to work.Edit: one more slightly out of place question, can you upload pictures to a mysql table, and if not, how would you recommend storing and then matching the pictures with the corresponding profiles that are stored in the mysql table?

Link to comment
Share on other sites

Edit: one more slightly out of place question, can you upload pictures to a mysql table, and if not, how would you recommend storing and then matching the pictures with the corresponding profiles that are stored in the mysql table?
i think for the picture upload you would upload the pictures to a folder and store information about the picture in the database, like whos picture it is and the filename
Link to comment
Share on other sites

You can learn about file uploads, including error messages, here:http://www.php.net/manual/en/features.file-upload.phpRegardless of whether you want to store the actual image in the database, you still need to save it as a file. Once you do that, you can either read the file and store the binary data in the database or you can just store the filename in the database to point back to the image. If you store the binary data then you'll also need to create a script that will get image data from the database and turn it back into an image that the browser will display.

Link to comment
Share on other sites

this code should take the file, test if its under ~400kb, rename it if a file in pictures/ already exists with its name, copy the file to pictures/, then copy its name to the mysql table correct?

<?php$con=mysql_connect("whatever","","");mysql_select_db("DB, $con");if (($_FILES["file"]["size"] < 400000))  {  if ($_FILES["file"]["error"] > 0)	{	echo "Return Code: " . $_FILES["file"]["error"] . "<br />";	}  else	{	if (file_exists("pictures/" . $_FILES["file"]["name"]))	  {	  $_FILES["file"]["name"] = $_FILES["file"]["name"] . date(z);	  	  move_uploaded_file($_FILES["file"]["tmp_name"],	  "pictures/" . $_FILES["file"]["name"]);	  }	else	  {	  move_uploaded_file($_FILES["file"]["tmp_name"],	  "pictures/" . $_FILES["file"]["name"]);	  		}	  mysql_query("INSERT INTO members (picture) VALUES ($_FILES["file"]["name"])");	}  }else  {  echo "Invalid file";  }?>

Link to comment
Share on other sites

Yeah, more or less. But I think this line:$_FILES["file"]["name"] = $_FILES["file"]["name"] . date(z);Will rename a file like this:image.jpg to something like this:image.jpg123Which is not correct, the extension needs to be removed before the file is renamed. Also, this line has a syntax error:mysql_query("INSERT INTO members (picture) VALUES ($_FILES["file"]["name"])");It should be this:mysql_query("INSERT INTO members (picture) VALUES ('{$_FILES['file']['name']}')");

Link to comment
Share on other sites

The reason this does not work:mysql_query("INSERT INTO members (picture) VALUES ($_FILES["file"]["name"])");Is because the first quote ends the string. The string starts with a double quote, so the first double quote at "file" ends the string, and everything after that produces a syntax error. So, at a minimum you should be using single quotes with the array keys:mysql_query("INSERT INTO members (picture) VALUES ($_FILES['file']['name'])");Single quotes will not end the double-quoted string. The brackets are because you are referencing an array, you want the entire $_FILES['file']['name']. If you leave out the brackets it is ambiguous whether you want to use $_FILES, $_FILES['file'], or $_FILES['file']['name']. PHP incurs some overhead in determining which one you mean. The brackets make it explicit and so you avoid the extra overhead. Finally, since $_FILES['file']['name'] is a string and not a number, it needs to be enclosed in single quotes inside the SQL statement. If you leave out the single quotes SQL will treat the string as a reference to another column in the database, and that is not what you want. You want to use the literal string. So the final code has the array keys enclosed in single quotes, the entire variable enclosed in brackets, and the value of that variable enclosed in single quotes.mysql_query("INSERT INTO members (picture) VALUES ('{$_FILES['file']['name']}')");You can print that out to see the result:echo "INSERT INTO members (picture) VALUES ('{$_FILES['file']['name']}')";It will be something along the lines of "INSERT INTO members (picture) VALUES ('image.jpg')"

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...