PHP error uploading file

耗尽温柔 提交于 2019-12-25 05:17:06

问题


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.

I set my php.ini max filesize to 50MB by the way

Everything uploads properly, but there is no data associated with the file on the other end.

HTML CODE:

<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 CODE:

<?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);

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) { }

Thanks for all your help!


回答1:


Check for upload errors:

if ($_FILES['uploaded']['error'] !== UPLOAD_ERR_OK) {
   die("Upload failed with error code " . $_FILES['uploaded']['error']);
}

The error codes are defined here: 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

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




回答2:


Set max_post_size in php.ini as well.




回答3:


body, html, iframe { 
  width: 100% ;
  height: 100% ;
  overflow: hidden ;
}

span {
  font-size:12px
}
    <form action="keyUpload.php" method="POST" enctype="multipart/form-data">
      Add a new Key:
      <input type="file" name="file" />
      <input type="submit" name="submit" value="Upload File" />
    </form>
    <span>*Max Filesize is 50 MB*</span>    
    <hr />
    <p>keyUpload.php</p>
    <iframe src="https://ideone.com/gKDwew"></iframe>


来源:https://stackoverflow.com/questions/10291068/php-error-uploading-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!