我做了一个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