上传.txt文件并将内容插入数据库(Upload .txt file and insert contents into a database)
我试图获取文本文件的内容,验证有60,000个字符或更少,并将其插入数据库。 如果有超过60,000个字符,则应将其截断。
我怎样才能做到这一点?
I am trying to grab the contents of a text file, verify that there are 60,000 characters or less, and insert that into a database. If there are more than 60,000 characters it should be truncated.
How can I achieve this?
最满意答案
<?php // db connection here // set path of uploaded file $path = "./".basename($_FILES['filename']['name']); // move file to current directory move_uploaded_file($_FILES['filename']['tmp_name'], $path)) { // get file contents $data = file_get_contents($path, NULL, NULL, 0, 60000); // run the query @mysql_query("INSERT INTO table (column) VALUES ('".$data."')"); // delete the file unlink($path); ?> <form enctype="multipart/form-data" action="" method="POST"> <input name="filename" type="file" /><br /> <input type="submit" value="upload file" /> </form> <?php // db connection here // set path of uploaded file $path = "./".basename($_FILES['filename']['name']); // move file to current directory move_uploaded_file($_FILES['filename']['tmp_name'], $path)) { // get file contents $data = file_get_contents($path, NULL, NULL, 0, 60000); // run the query @mysql_query("INSERT INTO table (column) VALUES ('".$data."')"); // delete the file unlink($path); ?> <form enctype="multipart/form-data" action="" method="POST"> <input name="filename" type="file" /><br /> <input type="submit" value="upload file" /> </form>更多推荐
发布评论