PHP 错误上传文件 [英] PHP error uploading file
问题描述
好的,所以我为网站设置了一个上传引擎,以便经过身份验证的用户可以上传库中歌曲的音频文件(密钥),但是当我尝试上传任何文件时遇到了这个奇怪的问题超过 5MB.
Okay, so I set up an upload engine for a website so that an authenticated user can upload a audio file (a key) for a song in the library, but I come across this strange problem when I try to upload any file over 5MB.
顺便说一下,我将 php.ini 的最大文件大小设置为 50MB
一切正常上传,但在另一端没有与文件相关的数据.
Everything uploads properly, but there is no data associated with the file on the other end.
HTML 代码:
<form action="keyUpload.php?id=<?php echo $id;?>" method="post" enctype="multipart/form-data">
<p style="color:#fff;font-size:30px;font-family:Times">
Add a new Key:<br/><input name="uploaded" type="file" id="file"><br />
<input type="text" name="kname" id="kname" value placeholder="Key Name (Ex. Demo, A#, etc.)" style="width:300px;"><br/>
<button class="button">Upload File</button><br/>
<span style="font-size:12px;">*Max Filesize is 50 MB*</span>
</p>
</form>
PHP 代码:
<?php
$id=$_GET["id"];
$name=$_POST["kname"];
$name = str_replace(" ","%20",$name);
$allowed_filetypes = array('.mp3','.m4a','.wav','.wma');
$filename = $_FILES['uploaded']['name'];
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
当我上传大于 5 MB 的文件时,$filename 和 $ext 都是空变量.在所有其他情况下,该引擎都能完美运行.
Both $filename and $ext are empty variables when I upload a file larger than 5 MB. In all other cases, this engine works perfectly.
当回显时,根本没有任何反应,所以很明显,如果文件不存在,引擎将不会保存文件.怎么回事?
When echoed, simply nothing happens, so obviously the engine will not save the file if it doesn't exist. What's going on?
var_dump:
array(0) { }
感谢您的帮助!
推荐答案
检查上传错误:
if ($_FILES['uploaded']['error'] !== UPLOAD_ERR_OK) {
die("Upload failed with error code " . $_FILES['uploaded']['error']);
}
错误代码定义在这里:http://www.php.net/manual/en/features.file-upload.errors.php
同样,不要使用文件名来验证上传.恶意用户伪造文件名并上传恶意文件是非常重要的,例如.
As well, do NOT use filenames to validate the uploads. It is beyond trivial for a malicious user to fake a filename and upload malicious files, eg.
ren nastyvirus.exe good_tune.mp3
并且不要对文件名使用字符串操作.有一大堆用于文件名操作的 PHP 函数,例如http://php.net/basename
And don't use string operations on filenames. There's a whole whack of PHP functions for filename manipulation, e.g. http://php.net/basename
这篇关于PHP 错误上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!