Shawn
Shawn

Reputation: 941

PHP, MySQL, storing filename in a field

i was wondering what the best way to store a file name in a field. i dont mind if the extension is chopped off or not.. so far i have

$fname = $_FILES["name"];

mysql_connect('myssdfebhost.com',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO uploadedfiles (filename) VALUES ('$fname')";
mysql_query($query);

but it doesnt put it in

Upvotes: 0

Views: 481

Answers (1)

Pascal MARTIN
Pascal MARTIN

Reputation: 400972

A couple of things to note :

  • I don't think you can use $_FILES["name"]:
    • instead, you should use $_FILES['NAME OF YOUR INPUT FIELD']["name"]
  • You should escape $fname with mysql_real_escape_string(), to avoid SQL injections.
  • The uploaded file is uploaded to a temporary directory ; if you do not move it to a non-temporary location, it will automatically be deleted.

It could be interesting for you to use the following portion of code :
var_dump($_FILES);

This will show you what's in the $_FILES array, so you know what you can use -- typically, you'll see the structure of that array, including the key that correspond to the name attribute of the <input type="file" ... /> of your form.


About that `$_FILES` variable, btw, you should take a look at the following section of the manual : [**POST method uploads**][4].

Upvotes: 3

Related Questions