2013-03-17 74 views
0

我一直在努力争取一段时间,让这个单一按钮上传工作,我已经尝试了几种方法来提交上传和这一个我似乎工作到了点,但后来的东西该PHP似乎不喜欢提交,没有这个JS作为一个常规的选择文件和上传,但我很快添加JS(似乎工作)PHP然后不起作用。按钮上传问题

我完全困惑,为什么发生这种情况!

任何意见赞赏谢谢!

上传表单

<form name="form" method="POST" enctype="multipart/form-data" action="updateProfilePic_script.php">  
            <div class="update_header_pic"> 

<input type = 'button' value = 'Choose image' 
            onclick ="javascript:document.getElementById('Photo').click();"> 
            <input id='Photo' type='file' style='visibility: hidden;' name='Photo' onchange='submit();'/> 


            </div> 
           </form>          

的PHP

<? 
    session_start(); 

    $user = $_SESSION['username']; 
         $hostname = "localhost"; 
         $db_user = "xxxusername"; 
         $db_password = "xxxpassword"; 
         $database = "xxxdatabase"; 
         $db_table = "user_profile_pic"; // image table 

         # THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE 
         $db = mysql_connect($hostname, $db_user, $db_password); 
         mysql_select_db($database,$db); 
         $uploadDir = 'usermedia/users/images/profileimages/'. $user .'/';//Image Upload Folder 
         if (!is_dir($uploadDir) && !mkdir($uploadDir)){ 
          die("Error creating folder $uploadDir"); 
         } 

         if(isset($_POST['submit'])) { 


         $date = date("Y-m-d H:i:s"); 

         $unique = substr(number_format(time() * rand(),0,'',''),0,10); 
         $fileName = $unique .'-'.$_FILES['Photo']['name']; 
         $tmpName = $_FILES['Photo']['tmp_name']; 
         $fileSize = $_FILES['Photo']['size']; 
         $fileType = $_FILES['Photo']['type']; 
         $filePath = $uploadDir . $fileName; 
         $result = move_uploaded_file($tmpName, $filePath); 
         if (!$result) { 
         echo "Error uploading file"; 
         exit; 
         } 
         if(!get_magic_quotes_gpc()) 
         { 
         $fileName = addslashes($fileName); 
         $filePath = addslashes($filePath); 
         } 
         $query = "INSERT INTO $db_table (Image , username , datetime , filesize , filetype) VALUES ('$filePath' , '$user' , '$date' , '$fileSize' , '$fileType')"; 
         mysql_query($query) or die('Error, query failed'); 

         header('Location: edit_myProfile.php'); 

         } 



         ?>  
+0

您是否认为像print_r($ _ FILES),print_r($ _ POST)n东西?任何其他错误? – 2013-03-17 17:35:59

+1

您的表单中没有名为'submit'的元素,但您正在测试它if'isset($ _ POST ['submit'])){' – CBroe 2013-03-17 17:40:06

回答

0

在HTML表单没有[input type=submit]那么,为什么在你的PHP代码,你做的事:

if(isset($_POST['submit'])) { 
//... 
//.. 
} 

你可以简单地将其更改为:

if(isset($_FILES['Photo'])) { 
//the your uploading procedure 
}