-1

嗨,我是 PHP 新手(我是一名 asp.net 开发人员),刚刚切换到 PHP,因为我需要为我的朋友开发一个网站。我目前正在尝试将音乐文件上传到我网站上的文件夹中,但不断收到此错误:

注意:“未定义的索引:第 5 行 C:\xampp\htdocs\sitename\admin\uploader.php 中的上传文件”

我尝试了以下步骤:

  1. 将 max_upload_filesize 和 post_max_size 设置为“1028M”。
  2. 在我的表单标签中添加了“enctype="multipart/form-data"。
  3. 试过“print_r($_FILES);” 并且数组继续返回空。
  4. 在新网站中使用了上传脚本,它工作正常。
  5. 在线尝试了很多不同的脚本,但没有一个有效,最后我尝试了这个,但仍然面临同样的问题。

下面是我的代码:

HTML

<!DOCTYPE html>
<html >
<head>
    <title>ControlHome</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="../css/style.css">
    <link rel="stylesheet" href="css/admin.css">
    <script src="../js/jquery-1.7.1.min.js"></script>
    <script src="../js/superfish.js"></script>
    <script src="../js/jquery.easing.1.3.js"></script>
    <script src="../js/tms-0.4.1.js"></script>
    <script src="../js/slider.js"></script>
<!--[if lt IE 8]>
   <div style=' clear: both; text-align:center; position: relative;'>
     <a href="http://windows.microsoft.com/en-US/internet-explorer/products/ie/home?ocid=ie6_countdown_bannercode">
       <img src="http://storage.ie6countdown.com/assets/100/images/banners/warning_bar_0000_us.jpg" border="0" height="42" width="820" alt="You are using an outdated browser. For a faster, safer browsing experience, upgrade for free today." />
    </a>
  </div>
<![endif]-->
<!--[if lt IE 9]>
    <script src="../js/html5.js"></script>
    <link rel="stylesheet" href="css/ie.css"> 
<![endif]-->
</head>
<body>
<div class="main-bg">

                         <div class="page">
                         <form method="GET" action="uploader.php" enctype="multipart/form-data> 
                           <table width="100%" border="0" cellspacing="0" cellpadding="0">
                             <tr>
                               <td>
<form enctype="multipart/form-data" action="uploader.php" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</td>
                               <td>&nbsp;</td>
                               <td>&nbsp;</td>
                               <td>&nbsp;</td>
                             </tr>
                           </table>
</form>
                         </div>
                </div>
            </div>
        </div>
    </section>
    <!-- Footer -->
    <footer>
    </footer>
</div>
</body>
</html>

PHP

<?php
ini_set('upload_max_filesize', '1028M');
ini_set('post_max_size', '1028M');

echo $_FILES['uploadedfile']['error'];
print_r($_FILES);

//**********************************************************************************************


echo "Please wait while we attempt to upload your file...<br><br>";

//**********************************************************************************************


$target_path = "uploads/";

$flag = 0; // Safety net, if this gets to 1 at any point in the process, we don't upload.

$filename = $_FILES['uploadedfile']['name'];
$filesize = $_FILES['uploadedfile']['size'];
$mimetype = $_FILES['uploadedfile']['type'];

$filename = htmlentities($filename);
$filesize = htmlentities($filesize);
$mimetype = htmlentities($mimetype);

$target_path = $target_path . basename( $filename );

if($filename != ""){

echo "Beginning upload process for file named: ".$filename."<br>";
echo "Filesize: ".$filesize."<br>";
echo "Type: ".$mimetype."<br><br>";

}

//First generate a MD5 hash of what the new file name will be
//Force a MP3 extention on the file we are uploading

$hashedfilename = md5($filename);
$hashedfilename = $hashedfilename.".mp3";

//Check for empty file
if($filename == ""){
$error = "No File Exists!";
$flag = $flag + 1;

}

//Now we check that the file doesn't already exist.
$existname = "uploads/".$hashedfilename;

if(file_exists($existname)){

if($flag == 0){
$error = "Your file already exists on the server!  
Please choose another file to upload or rename the file on your
computer and try uploading it again!";
}

$flag = $flag + 1;
}

//Whitelisted files - Only allow files with MP3 extention onto server...

$whitelist = array(".mp3");
foreach ($whitelist as $ending) {

if(substr($filename, -(strlen($ending))) != $ending) {
 $error = "The file type or extention you are trying to upload is not allowed!  
You can only upload MP3 files to the server!";
$flag++;
}
}


//Now we check the filesize.  If it is too big or too small then we reject it
//MP3 files should be at least 1MB and no more than 6.5 MB

if($filesize > 6920600){
//File is too large

if($flag == 0){
$error = "The file you are trying to upload is too large!  
Your file can be up to 6.5 MB in size only.  
Please upload a smaller MP3 file or encode your file with a lower bitrate.";
}

$flag = $flag + 1;
}

if($filesize < 1048600){
//File is too small

if($flag == 0){
$error = "The file you are trying to upload is too small!
Your file has been marked as suspicious because our system has
determined that it is too small to be a valid MP3 file.
Valid MP3 files must be bigger than 1 MB and smaller than 6.5 MB.";
}

$flag = $flag + 1;

}

//Check the mimetype of the file
if($mimetype != "audio/x-mp3" and $mimetype != "audio/mpeg"){

if($flag == 0){
$error = "The file you are trying to upload does not contain expected data.
Are you sure that the file is an MP3?";
}

$flag = $flag + 1;
}

//Check that the file really is an MP3 file by reading the first few characters of the file
$f = @fopen($_FILES['uploadedfile']['tmp_name'],'r');
$s = @fread($f,3);
@fclose($f);
if($s != "ID3"){

if($flag == 0){
$error = "The file you are attempting to upload does not appear to be a valid MP3 file.";
}

$flag++;
}



//All checks are done, actually move the file...

if($flag == 0){

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {


    //Change the filename to MD5 hash and FORCE a MP3 extention.

    if(@file_exists("uploads/".$filename)){

    //Rename the file to an MD5 version
    rename("uploads/".$filename, "uploads/".$hashedfilename);

    echo "The file ".  basename( $filename ). "
     has been uploaded.  Your file is <a href='uploads/$hashedfilename'>here</a>.";

    }  
    else{
      echo "There was an error uploading the file, please try again!";
    }


} else{
    echo "There was an error uploading the file, please try again!";
}

}
else {
echo "File Upload Failed!<br>";
if($error != ""){
echo $error;
}
} 
?>
4

4 回答 4

0

将表单方法="GET" ....更改为方法="POST" ....

于 2013-05-02T17:42:06.150 回答
0

这里试试这个

<!DOCTYPE html>
<html >
 <head>
  <title>ControlHome</title>
  <meta charset="utf-8">
  <link rel="stylesheet" href="../css/style.css">
  <link rel="stylesheet" href="css/admin.css">
  <script src="../js/jquery-1.7.1.min.js"></script>
  <script src="../js/superfish.js"></script>
  <script src="../js/jquery.easing.1.3.js"></script>
  <script src="../js/tms-0.4.1.js"></script>
  <script src="../js/slider.js"></script>
  <!--[if lt IE 8]>
   <div style=' clear: both; text-align:center; position: relative;'>
     <a href="http://windows.microsoft.com/en-US/internet-explorer/products/ie/home?ocid=ie6_countdown_bannercode">
      <img src="http://storage.ie6countdown.com/assets/100/images/banners/warning_bar_0000_us.jpg" border="0" height="42" width="820" alt="You are using an outdated browser. For a faster, safer browsing experience, upgrade for free today." />
     </a>
  </div>
<![endif]-->
<!--[if lt IE 9]>
<script src="../js/html5.js"></script>
<link rel="stylesheet" href="css/ie.css"> 
 <![endif]-->
</head>
<body>
<div class="main-bg">
<div class="page">
 <form enctype="multipart/form-data" action="uploader.php" method="POST">
     <table width="100%" border="0" cellspacing="0" cellpadding="0">
         <tr>
           <td>
              <!-- MAX_FILE_SIZE must precede the file input field -->
            Choose a file to upload: <input name="uploadedfile" type="file" /><br />
            <input type="submit" value="Upload File" />
            </td>
         </tr>
     </table>
</form>
</div>
</div>
</body>
</html>

您只需要删除额外的表单标签并使用 POST 作为方法。

于 2013-05-02T18:02:08.670 回答
0

检查服务器是否可以将文件写入目录tmp。如果需要,显式设置 PHP 临时路径。

于 2013-05-02T17:50:38.953 回答
0

您缺少第一个表单 enctype 属性上的结束语标记(“),您打开另一个表单而不关闭第一个表单,不一定有两个表单,您应该删除第一个打开表单标记。

于 2013-05-02T17:41:20.337 回答