텍스트 파일의 내용을 가져 와서 60,000 자 이하인지 확인하고 데이터베이스에 삽입하려고합니다. 60,000 자 이상인 경우 잘립니다..txt 파일을 업로드하고 내용을 데이터베이스에 삽입하십시오.
어떻게하면됩니까?
텍스트 파일의 내용을 가져 와서 60,000 자 이하인지 확인하고 데이터베이스에 삽입하려고합니다. 60,000 자 이상인 경우 잘립니다..txt 파일을 업로드하고 내용을 데이터베이스에 삽입하십시오.
어떻게하면됩니까?
<?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>
실제로 길이를 먼저 확인하지 않아도됩니다. 잘라내는 묵시적으로 수행 할 수 있습니다 : 그것은 정말 이상이면 substr
는 문자열을 차단하기 때문에
$content = file_get_contents("asciiart.txt");
$content = substr($content, 0, 60000);
이 작동합니다. 그렇지 않으면 그냥 보관할 것입니다. 데이터베이스 삽입은 아무런 문제가 포즈해야
$pdo->prepare("INSERT INTO textfiles (content) VALUES (?)")
->execute(array($content));
'file_get_contents()' – Prisoner
미안 내가 내 아이 패드에있어 구문 오류를 얻을 경우.
<?php
$txt = $_FILES['files'];
$dbTxt = mysql_real_escape_string($txt);
if((!strlen($txt) =< 60000) && ($txt['type'] == "text/plain")) {
die("Error here");
}
// no need for else as txt above 60000 have been locked away
move_uploaded_file($txt['temp_name'], 'tmp/'.$_server['remote_addr']);
$contents = file_get_contents('tmp/'.$_server['remote_addr']);
mysql_query("INSERT INTO txtUpload(text, ip) VALUES ($contents, $_server['remote_addr']");
$myFile = ''tmp/'.$_server['remote_addr'];
$fh = fopen($myFile, 'w') or die("can't open file");
fclose($fh);
Unlink($myFile);
아, 파일을 데이터베이스에 삽입 한 후 (또는 그렇게하지 못한 경우) 텍스트 파일을 삭제해야합니다. – Mario112
file_get_contnets, strlen, 보통의 db insert –