0

我一直在努力让这个单按钮上传工作一段时间,我尝试了几种方法来提交上传,我的这个似乎可以工作到 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');

                        }



                        ?>      
4

1 回答 1

0

在您的 HTML 表单[input type=submit]中,您的 PHP 代码中没有您这样做的原因:

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

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

if(isset($_FILES['Photo'])) {
 //the your uploading procedure
}
于 2013-03-17T17:40:56.560 回答