按钮上传问题

时间:2013-03-17 17:32:14

标签: php button upload

我一直在努力让这个单按钮上传工作,我已经尝试了几种方法来提交上传,这个我似乎已经工作到了php的点,但后来在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');

                        }



                        ?>      

1 个答案:

答案 0 :(得分:0)

在您的HTML表单中没有[input type=submit],那么为什么在您的PHP代码中执行:

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

您只需将其更改为:

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