2012-02-10 10 views
-2

upload.phpというコードを使用して、自分が作成したディレクトリ(profile_images)に保存された画像をアップロードしてファイル名を自分のデータベースに転送させようとしています:画像のアップロード時にタイプを取得できません

<?php 

    session_start(); 
    require_once "database.php"; 
    db_connect(); 
    require_once "auth.php"; 
    $current_user = current_user(); 

//Check to see if the type of file uploaded is a valid image type 
function is_valid_type($file) 
{ 
    //This is an array that holds all the valid image MIME types 
    $valid_types = array("upload_picture_fileinput/jpg", "upload_picture_fileinput/jpeg", "upload_picture_fileinput/bmp", "upload_picture_fileinput/gif", "upload_picture_fileinput/png"); 

    if (in_array($file['type'], $valid_types)) 
     return 1; 
    return 0; 

} 

function showContents($array) 
    { 

     echo "<pre>"; 
     print_r($array); 
     echo "</pre>"; 
    } 

//Set some constants 

//This variable is the path to the image folder where all the images are going to be stored 

//Note that there is a trailing forward slash 
$TARGET_PATH = "profile_images/"; 

//Get our POSTed variables 
$upload_picture_fileinput = $_FILES['upload_picture_fileinput']; 


//Sanitize input 
$upload_picture_fileinput['name'] = mysql_real_escape_string($upload_picture_fileinput['name']); 

//Build our target path full string. This is where the file will be moved to 
//i.e. profile_images/picture.jpg 
$TARGET_PATH .= $upload_picture_fileinput['name']; 

if(!is_valid_type($upload_picture_fileinput)) { 

    $_SESSION['error'] = "You must upload a jpeg, gif, bmp, or png"; 
    header("Location: account.php"); 
    exit; 


    } 

//attempt to move the file from its temporary directory to its new home 
if (move_uploaded_file($upload_picture_fileinput['tmp_name'], $TARGET_PATH)) { 

    $sql = "insert into users (profile_image_filename) values ('" . $upload_picture_fileinput['name'] . "')"; 


    $result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error()); 

    header("Location: account.php"); 
    exit; 
} 
else 
{ 


    $_SESSION['error'] = "Could not upload file. Check read/write permissions on the directory"; 
    header("Location: account.php") ; 
    exit; 

    } 

これは、ユーザーが画像をアップロードするように指示されたページに関連するコードです:私は、データベースの罰金に接続してい

<div class="pictures add_pictures"> 
      <div class="add_picture"> 
       <div class="upload_picture"> 
        <form action="upload.php" method="POST" enctype="multipart/form-data" name="upload_picture_form" class="upload_picture_form"> 
         <span class="add_picture_label">+ Add a Profile Picture</span> 
         <input type="file" name="upload_picture_fileinput" class="upload_picture_file_input"/> 
         <input type="hidden" name="MAX_FILE_SIZE" value="100000"/> 
         <br><br><br><br><br><br><br> 

         <input type="submit" id="submit" value="Upload" /> 
        </form> 
       </div> 
      </div> 
     </div> 

     <?php 

      $sql = "select * from users"; 
      $result = mysql_query($sql) or die ("Could not access DB: " . mysql_error()); 

      while ($row = mysql_fetch_assoc($result)) { 

       echo "<p>"; 

       echo "<img src=\"profile_images/" . $row['profile_image_filename'] . "\" alt=\"\" /><br />"; 
       echo "</p>"; 


      } 
     ?> 

。画像アップロードの形式は、upload_picture_fileinputという名前になります。

ただし、作成したエラーが引き続き表示されます。「jpeg、gif、bmp、またはpngをアップロードする必要があります。

画像がmyディレクトリに保存されていないか、ファイル名がデータベースにアップロードされていません。

これらのタイプのいずれかを使用していますが、問題がある場合は教えてください。

+0

あなたは何を得ているのかを見るために$ file ['type']をダンプしようとしましたか?私の推測では、あなたはimage/jpgなどと比較するべきです –

答えて

0

機能is_valid_typeを次のように変更する必要があります。

//Check to see if the type of file uploaded is a valid image type 
function is_valid_type($file) 
{ 
    //This is an array that holds all the valid image MIME types 
    $valid_types = array("image/pjpeg", "image/jpeg", "image/jpg", "image/png", "image/x-png", "image/gif", "image/bmp"); 

    if (in_array($file['type'], $valid_types)) 
     return 1; 
    return 0; 

} 
2
> //This is an array that holds all the valid image MIME types 
> $valid_types = array("upload_picture_fileinput/jpg", 
> "upload_picture_fileinput/jpeg", "upload_picture_fileinput/bmp", 
> "upload_picture_fileinput/gif", "upload_picture_fileinput/png"); 

有効なMIMEタイプ?なぜ彼らは "upload_picture_file"で始まるのですか?

+0

'upload_picture_file'を' image'に置き換えてください –

関連する問題