1

我正在创建简单的文件上传(用于图片)。我在 Opera 和 FireFox 中尝试过,上传工作正常。但是当我通过谷歌浏览器上传时,图片没有上传。你能告诉我问题出在哪里吗:

这是用于在数据库中存储图片的 php 脚本

<?php
$id=$_SESSION['user_id'];
$user_id=$_SESSION['user_id'];
$album_id=$_POST['album'];

$max_size = 500;            // Sets maxim size allowed for the uploaded files, in kilobytes

// sets an array with the file types allowed
$allowtype = array('bmp', 'gif', 'htm', 'html', 'jpg', 'jpeg', 'mp3', 'pdf', 'png', 'rar', 'zip');

// if the folder for upload (defined in $updir) doesn't exist, tries to create it (with CHMOD 0777)
/*if (!is_dir($updir)) mkdir($updir, 0777);*/

/** Loading the files on server **/

$result = array();          // Array to store the results and errors

// if receive a valid file from server
if (isset ($_FILES['files'])) {

  // checks the files received for upload

$file_name=$_FILES['files']['name'];
                $file_type=$_FILES['files']['type'];
                $file_size=$_FILES['files']['size'];    
                $file_tmp=$_FILES['files']['tmp_name'];

  for($f=0; $f<count($_FILES['files']['name']); $f++) {
    $file_name = $_FILES['files']['name'][$f]; 
    $random_name=rand(); 

    // checks to not be an empty field (the name of the file to have more then 1 character)
    if(strlen($file_name)>1) {
      // checks if the file has the extension type allowed
     $type=explode('.', $file_name);
            $type=end($type);
      if (in_array($type, $allowtype)) {
        // checks if the file has the size allowed
        if ($_FILES['files']['size'][$f]<=$max_size*1000) {
          // If there are no errors in the copying process
          if ($_FILES['files']['error'][$f]==0) {

                        $query = mysql_query("SELECT username from users WHERE id = '$id' ");
                            while($run=mysql_fetch_array($query)){
                                    $username=$run['username'];
                                    }
                                    $query = mysql_query("SELECT album.name an from album WHERE album.id = '$album_id' ");
                            while($run=mysql_fetch_array($query)){

                                    $album_name=$run['an'];
                            }   
                            mysql_query("INSERT INTO photos VALUE ('', '$album_id', '$random_name.jpg', '$user_id')");

            // Sets the path and the name for the file to be uploaded
            // If the file cannot be uploaded, it returns error message
            if (move_uploaded_file ($_FILES['files']['tmp_name'][$f],"./users/".$username."/".$album_name."/".$random_name.".jpg")) {
              /*$result[$f] = ' The file could not be copied, try again';*/
              $result[$f] = '<b>'.$file_name.'</b> - OK';
            }
            else {
               $result[$f] = ' The file could not be copied, try again';
            }
          }
        }
        else { $result[$f] = 'The file <b>'. $file_name. '</b> exceeds the maximum allowed size of <i>'. $max_size. 'KB</i>'; }
      }
      else { $result[$f] = 'File type extension <b>.'. $type. '</b> is not allowed'; }
    }
  }
   // Return the result
  $result2 = implode('<br /> ', $result);
  echo '<h4>Files uploaded:</h4> '.$result2;
}
?>

这是用于图片上传的表格:

    <form id="uploadform" action="uploaderimg.php" method="post" enctype="multipart/form-data" target="uploadframe" onSubmit="uploading(this); return false">
         <br>
          Select album: 

       <select name="album">
       <?php
       $query=mysql_query("SELECT id, name, user_id FROM album WHERE user_id = '$id'");

       while($run=mysql_fetch_array($query)){ 
       $album_id=$run['id'];
       $album_name=$run['name'];
       $album_user = $run['user_id'];   
       echo "<option value='$album_id'>$album_name</option>";
       }

       ?>

       </select>

           <br /><br />
  <h1>Chose your photo/s</h1>
  <br>
  <input type="file"  name="files[]" />
  <input type="submit" value="UPLOAD" id="sub" />
</form>

编辑:

这是根据 PHP 的错误(它在存储上传文件的第一个脚本中):

Notice: Undefined index: album in..
4

1 回答 1

2

与OP交谈后,问题出在这一行:

<form id="uploadform" action="uploaderimg.php" method="post" enctype="multipart/form-data" target="uploadframe" onSubmit="uploading(this); return false">

哪里onSubmit="uploading(this); return false"错了。

于 2013-10-02T20:49:29.120 回答