2013-04-12 33 views
-2
<td class="reg-txt-left">Profile Picture:</td> 
<td class="reg-input-right"> 
    <form action="profile_save_upload.php" method="post" enctype="multipart/form-data"> 
     <?php 
     echo "<img src=\"../user/".$_SESSION['profileEmail']."/images/".$_SESSION['profilePic']."\" alt='Profile Picture' width='200px' height='200px' />" 
     ?> 
     <input type="file" name="file" /> 
     <input type="submit" name="submitProfilePic" id="button" value="Change Profile Picture" /> 
    </form> 
</td> 

這裏是profile_save_upload.php腓 - move_uploaded_file不能正常工作

<?php session_start(); 
if(isset($_POST['submitProfilePic'])){ 
/*file saving*/ 
    if ($_FILES["file"]["error"] > 0){ 
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; 
    } 
    else{ 
    if (file_exists("../user/".$_SESSION['profileEmail']."/" . $_FILES["file"]["name"])) 
     { 
     include('../includes/dbcon.php'); 
    /*update database*/ 
    $sql = mysql_query("UPDATE users 
         SET picture = '".$_FILES["file"]["name"]."' 
         WHERE email = '".$_SESSION['profileEmail']."'"); 
    header("Location: profileEdit.php"); 
    } 
    else{ 
    move_uploaded_file($_FILES["file"]["tmp_name"], "../user/".$_SESSION['profileEmail']."/" . $_FILES["file"]["name"]); 
    include('../includes/dbcon.php'); 

    $sql = mysql_query("UPDATE users 
         SET picture = '".$_FILES["file"]["name"]."' 
         WHERE email = '".$_SESSION['profileEmail']."'"); 
    header("Location: profileEdit.php"); 

    } 
    } 
} 
?> 

這是圖像文件名被保存到數據庫,但不能將文件夾

includes 
    dbcon.php 
site 
    profileEdit.php 
    profile_save_upload.php 
user 
    user1 
     [the image supposed to be moved here] 

的層次在文件夾中。

有沒有人有一個想法,哪裏是錯誤的部分?在此先感謝

+0

父文件夾的路徑是否有該文件夾結構的寫權限? –

+0

是的,有... – Katherine

+0

@EdwinAlex這個代碼工作時,我在本地主機。但當它已經在線時沒有工作 – Katherine

回答

1

這裏是你將如何檢查的擴展名(如果需要的話):

if(is_uploaded_file($_FILES['imgfile']['tmp_name'])) 
{ 
    $fileExtension = array('jpg','jpeg','gif','png'); 
    $file_name = pathinfo($_FILES["imgfile"]["name"]); 
    $extension = strtolower($file_name['extension']); 
    if(!in_array($extension, $fileExtension)) 
    { 
     $error .= "<span class='error'>invalid file format !</span><br>"; 
    } 
} 

這裏是如果沒有發現任何錯誤,你將如何上傳:

$tmp_file = '';  
if(is_uploaded_file($_FILES['imgfile']['tmp_name'])) 
{ 
    $file_name = pathinfo($_FILES["imgfile"]["name"]); 
    $extension = strtolower($file_name['extension']); 
    $tmp_file = substr($file_name['filename'], 0, 50).'_'.time().'.'.$extension; 

    $uploaddir = 'user_image/'; 
    $uploadfile = $uploaddir.$tmp_file; 

    move_uploaded_file($_FILES['imgfile']['tmp_name'], $uploadfile); 
    //list($width, $height) = getimagesize($uploadfile); 

    $fileType = $_FILES['imgfile']['type']; 
    $fileSize = $_FILES['imgfile']['size']; 
} 
+0

你嘗試過嗎?它工作? –

1
move_uploaded_file($_FILES["file"]["tmp_name"], "../user/".$_SESSION['profileEmail']."/" . $_FILES["file"]["name"]); 
include('../includes/dbcon.php'); 

$sql = mysql_query("UPDATE users 
        SET picture = '".$_FILES["file"]["name"]."' 
        WHERE email = '".$_SESSION['profileEmail']."'"); 

$_FILES["file"]["name"]是文件名。看看你的SQL查詢,你只保存這個名字。你可能想使你的查詢是這樣的:

UPDATE users SET picture = '".$basepath."/user/".$_SESSION['profileEmail']."/" . $_FILES["file"]["name"]."'... 

其中$基本路徑是的user/