如何在php mysql中上传文件?

时间:2016-03-16 06:52:11

标签: php mysql

基本上我已经编写了一小段用于上传文件的代码。路径最初是 var / www / html / uploads ,其中文件应该上传,但到目前为止,文件不会上传到此特定文件夹。我已经尝试了从删除创建一个单独的目录到检查它是否是一个目录的所有内容,但仍然没有给我任何关于我哪里出错的线索。任何建议都会有很大的帮助。

以下是代码:

 <?php
     session_start();

      $user_id = $_SESSION['uid'];
      $file_path ='/uploads';
      include('db.php');
?>

<!DOCTYPE html>
 <html>

 <head>

     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <meta name="viewport" content="width=device-width,initial-scale=1" />
      <link rel="stylesheet" type="text/css" href="style.css"/>

   </head>

   <body>   
   <br/><br/><br />
      <div id="mainContent">
          <form action = "<?php echo $_SERVER['PHP_SELF'];?>" method="POST" enctype="multipart/form-data">
            Select File : 
            <input type = "file" name = "files"><br />
            <input type = "submit" value = "Upload" name = "upload">
         </form>


        <?php   

            if(isset($_POST['upload']) && $_POST['upload'] == 'Upload') 
            { 
                $tmp_file = $_FILES['files']['tmp_name'];
                $file_name = $_FILES['files']['name'];
                $file_size = $_FILES['files']['size'];
                $file_type = $_FILES['files']['type'];
                if(move_uploaded_file($tmp_file, $file_path.'/'.$file_name))        
               { 

                 $sqlInsert = "INSERT INTO download_content(uid, file_name, file_type, file_size, file_path) VALUES('$user_id','$file_name','$file_type','$file_size','$file_path')";


                $result = mysql_query($sqlInsert) or die(mysql_error());
                if($result)  
                {

                    echo '<br/><hr>';
                    echo 'File Name  : '.$file_name.'<br/><hr>';
                    echo 'File size : '.$file_size. 'bytes'.'<br/><hr>';    
                    echo 'File type : '.$file_type.'<br/><br/>';


                    /*$id = mysql_insert_id();*/
                    $sqlSelect = "SELECT * from download_content WHERE uid = '$user_id' order by file_id desc limit 1";
                    $query = mysql_query($sqlSelect) or die(mysql_error());

                    if(mysql_num_rows($query) >= 1) {

                        $row = mysql_fetch_array($query);

                    }
                }
                else { 
                    echo 'error uploading';
                }
            }
        }
        ?>


    </div>
 </body>
 </html>

0 个答案:

没有答案