我不想限制用戶上傳大文件。用戶可以上傳任何文件大小。當我嘗試了22 MB的文件大小,它工作。但是當我嘗試200MB文件時,它似乎並沒有進入if塊,並且在世界上的所有時間都上傳文件後,將else塊的輸出返回給我。 我的工作如下。我錯過了什麼嗎?超大文件上傳無法使用PHP/HTML
HTML:
<form method="post" enctype="multipart/form-data" class="form-horizontal" role="form">
<label for="document">Upload your Document</label>
<input type="file" id="document" name="document">
<label for="doc_cat">Document Category:</label>
<select name="doc_cat" id="doc_cat" class="form-control" required>
<option value="">--Select a Category--</option>
<option value="1">Sales</option>
<option value="2">Technical</option>
<option value="3">Pricing</option>
<option value="4">Policies</option>
<option value="5">Other</option>
</select>
<button type="button" class="btn btn-primary" id="upload" name="upload">Upload</button>
</form>
JQUERY/AJAX:
<script type="text/javascript">
$(document).ready(function() {
$('#upload').click(function() {
var formData = new FormData($('form')[0]);
var that = this;
$.ajax({
url: 'upload-document-proccess.php', //Server script to process data
type: 'POST',
data: formData,
async: false,
beforeSend: function() {
$("#loading-image").show();
},
success: function (msg) {
$("#loading-image").hide();
$("#display-return-msg").html(msg);
$("#display-return-msg").fadeIn("slow").delay(5000).fadeOut("slow");
},
cache: false,
contentType: false,
processData: false
});
$('form')[0].reset();
return false;
});
});
</script>
上傳文檔-proccess.php:
if(isset($_POST) && $_FILES['document']['size'] > 0)
{
$category_id = $_POST['doc_cat'];
$doc_desc = htmlspecialchars($_POST['doc_desc'],ENT_QUOTES);
switch ($category_id) {
case 1:
$category="Sales";
break;
case 2:
$category="Technical";
break;
case 3:
$category="Pricing";
break;
case 4:
$category="Policies";
break;
case 5:
$category="Other";
break;
default:
# code...
break;
}
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["document"]["name"]);
$fileName = $_FILES['document']['name'];
$tmpName = $_FILES['document']['tmp_name'];
$fileSize = $_FILES['document']['size'];
$fileType = $_FILES['document']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
if (move_uploaded_file($_FILES["document"]["tmp_name"], $target_file)) {
$msg = "The file ". basename($_FILES["document"]["name"]). " has been uploaded to ".$category." Category";
} else {
echo "Sorry, there was an error uploading your file.";
}
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
try {
$sql = "INSERT INTO document (doc_name, doc_type, doc_size, user_id, doc_category_id, doc_date, doc_desc)
VALUES ('$fileName', '$fileType', '$fileSize', '$user_id', '$category_id', now(), '$doc_desc')";
// use exec() because no results are returned
$conn->exec($sql);
?><script type="text/javascript">
var msg = '<?php echo $msg; ?>';
$(document).ready(function(){
$("#display-return-msg").html(msg);
$("#display-return-msg").fadeIn("slow").delay(5000).fadeOut("slow");
});
</script>
<?php
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
//echo "<br>File $fileName uploaded<br>";
}else{
echo "Please select File to upload.";
}
?>
在 '#顯示返回味精' 身份證,當我上傳200MB文件時,它給了我「請選擇要上傳的文件」。當我上傳22 MB文件時,我得到了'msg'變量的值,這是正確的。請給我建議我該怎麼辦?
在php.ini中檢查'upload_max_filesize'和'post_max_size'的值是多少,然後增加限制,然後檢查? – AnkiiG
我嘗試上傳66MB文件,它給我「允許的內存大小134217728字節耗盡」。這意味着post_max_size是128 MB。但是,如果仔細閱讀它,當我上傳200MB文件時,它會將我引向其他循環。 –