我做了一個PHP上傳文件腳本。現在,我不希望人們上傳巨大的文件,也不希望他們上傳PHP文件。這裏是我當前的PHP腳本:如何設置最大文件上傳大小,並只允許PHP上傳的某些文件類型?
<?php
// Where the file is going to be placed
$target_path = "files/";
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$length = 10;
$characters = '123456789abcdefghijklmnopqrstuvwxyz';
$string="";
for($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters))];
}
$pos = strrpos(basename($_FILES['uploadedfile']['name']), ".");
$ext = str_split(basename($_FILES['uploadedfile']['name']), $pos);
$target_path = $target_path.$string.$ext[1];
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "<h2>Your file has been uploaded!</h2><br /><p>Your file has been uploaded successfully. <a href='".$target_path."'>Click here</a> to see the file you uploaded.
<br /><br /><br />
<h2>A link to your file</h2><br />The full link to your file is:
<br /><br />
<code>http://www.americaspoeticsoul.com/extras/upload/".$target_path."</code></p>";
} else{
echo "<span class='error'>There was an error uploading the file, please try again.</span>";
}
?>
我將如何設置一個最大上傳文件的大小,並僅允許特定的文件類型,如只有JPEG,GIF文件,PNG圖像和HTML文件?
由於提前,
彌敦道
我從來沒有嘗試過這個,但如果我在將其擴展名重命名爲'.jpg'後上傳任意文件? – Kumar
謝謝,馬里奧。我會看看這是否有效。我是PHP的新手,所以我可能會遇到一些麻煩,只是將上面提供的內容提供給我的代碼。 :/ – Nathan
@Kumar我不知道一個任意文件可以使用'.jpg'擴展名嗎? – Nathan