开发者

PHP, MySQL, storing filename in a field

开发者 https://www.devze.com 2023-02-18 00:21 出处:网络
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

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 (fi开发者_如何学Clename) VALUES ('$fname')";
mysql_query($query);

but it doesnt put it in


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.
    • See the move_uploaded_file() function, about that, if you want to keep the file.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消