2009-08-06 27 views
3

我試圖讓用戶使用下面的PHPFacebook的 - 不能使用<INPUT TYPE =「文件」>標籤

<?php 
echo render_header('Your'); 

define("MAX_SIZE", "1536"); 

function getExtension($str){ 
    $i = strpos($str,"."); 
    if(!$i) {return "";} 
    $l = strlen($str) - $i; 
    $ext = substr($str, $i+1, $l); 
    return $ext; 
} 

$errors = 0; 

if(isset($_POST['Upload'])){ 
    $image = $_FILES["file1"]["name"]; 
    if($image){ 
    $filename = stripslashes($_FILES["file1"]["name"]); 
    $extension = getExtension($filename); 
    $extension = strtolower($extension); 
    if((strcasecmp($extension,"jpg") != 0) && (strcasecmp($extension,"jpeg") != 0) && (strcasecmp($extension,"png") != 0) && (strcasecmp($extension,"gif") != 0)) 
    { 
     $errors = 1; 
    } 
    else{ 
     $size = filesize($_FILES['file1']['tmp_name']); 
     if($size > MAX_SIZE*1024){ 
     $errors = 2; 
     } 
     else{ 
     $image_name = md5(uniqid()) . '.' . $extension; 
     $newname = "../images/" . $image_name; 
     $flName = "/images/" . $image_name; 
     $copied = move_uploaded_file($_FILES['file1']['tmp_name'], $newname); 
     if(!$copied){ 
      $errors = 3; 
     } 
     } 
    } 
    } 
} 

if(isset($_POST['Upload']) && $errors == 0){ 
    //add to database here 
    ... 
    if($errors == 0){ 
    include "uploadedFile.php"; 
    } 
} 
else{ 

$user_details = $fb->api_client->users_getInfo($user, 'first_name,last_name,pic_square'); 
$image_url = $user_details[0]['pic_square']; 
if($image_url == ""){ 
    $image_url = "http://static.ak.fbcdn.net/pics/q_silhouette.gif"; 
} 
$user_name = $user_details[0]['first_name'] . " " . $user_details[0]['last_name']; 
if(isset($_POST['Upload']) && $errors == 0){ 
?> 
<div id="error" class="error"> 
<h2 id="standard_error" name="standard_error">Failed to upload tattoo.</h2> 
<p id="standard_explanation" name="standard_explanation"> 
Error uploading file. This error occurred because either the photo was a size we don't support or there was a problem with the image file. 
<br/> 
</p> 
</div> 
<?php 
} 
?> 
<div id="newalbum" align="center"> 
<form id="upload" enctype="multipart/form-data" name="upload" action="" method="post"> 
<table class="formtable" cellspacing="0" border="0"> 
<tbody> 
<tr class="tallrow"> 
<td class="label"> 
Upload Image: 
<br/> 
<small> 
You can upload 
<br/> 
JPG, GIF or PNG 
<br/> 
files. 
</small> 
</td> 
<td> 
<div id="files"> 
<div id="1"> 
<input id="file1" class="inputfile" type="file" name="file1" size="22"/> 
</div> 
</div> 
</td> 
</tr> 
</tbody> 
</table> 
<div class="formbuttons"> 
<input id="" class="inputbutton" type="submit" value="Upload Tattoo" name="Upload" /> 
<br/> 
<small>The file size limit 1.5 MB. If your upload does not work, try uploading a smaller picture.</small> 
<br/> 
</div> 
<?php 
} 
?> 

上傳從我的Facebook應用程序上傳圖片文件,但是當我執行這個代碼並且用戶按下「上傳」按鈕,$ _FILES ['file1'] ['name']的值變爲空白。

這段代碼在Facebook應用程序允許的?如果不是什麼是上傳文件的正確方法?

感謝

編輯

好了,所以這個問題是與Facebook。他們從任何請求中刪除所有文件標籤!建議我們使用iframe代替!

感謝大家的幫助!

回答

3

確定。發現問題。 Facebook在發送請求之前剝去所有文件標籤。解決方案是使用iframe代替。

+0

嘿,我正要告訴你,我有與我自己的應用程序相同的問題我認爲問題是常規的應用程序頁面通過FB過濾,他們不想讓用戶上傳文件到他們的服務器然後必須將文件傳遞到您的服務器。 – 2009-08-11 21:39:54

+0

有人可以精心製作這個嗎?這是否意味着在應用程序設置空間中將應用程序設置爲「iframe」與「fbml」,或者這是否意味着爲整個頁面調用?或只是爲了表格?我很樂意爲我解決這個問題! – 2009-10-07 21:44:02

+0

您可以將包含表單的HTML放入iframe中。多數民衆贊成我是如何工作的。 – lostInTransit 2009-10-08 04:45:40

1

在嘗試訪問該文件名,試試這個...

switch ($_FILES['file1']['error']) { 
    case UPLOAD_ERR_INI_SIZE: 
    echo '<p class="warning">File Upload Failed! File too large.</p>'; 
    break; 
    case UPLOAD_ERR_FORM_SIZE: 
    echo '<p class="warning">File Upload Failed! File exceeds limit.</p>'; 
    break; 
    case UPLOAD_ERR_PARTIAL: 
    echo '<p class="warning">File Upload Failed! Please try again.</p>'; 
    break; 
    case UPLOAD_ERR_NO_TMP_DIR: 
    echo '<p class="warning">File Upload Failed! No temp directory.</p>'; 
    break; 
    case UPLOAD_ERR_CANT_WRITE: 
    echo '<p class="warning">File Upload Failed! Failed to write to disk.</p>'; 
    break; 
    case UPLOAD_ERR_EXTENSION: 
    echo '<p class="warning">File Upload Failed!</p>'; 
    break; 
} 

這應該告訴你問題所在。

+1

而且,這不能在頂部傷害: 函數ini_set( '的display_errors', '1'); error_reporting(E_ALL); 當它正常生活時拿出它。 – MSpreij 2009-08-06 12:09:54

+0

嘗試過。它不會進入任何開關情況。所以沒有錯誤:(在facebook中使用文件輸入有一些限制嗎? – lostInTransit 2009-08-06 12:22:33

+0

MSpreji,我試着添加這個語句,它給出錯誤「Undefined index:file1」。爲什麼會這樣??我有一個文件輸入名稱爲「文件1」 – lostInTransit 2009-08-06 12:25:05

0

你的HTML標記不會有一個表格結束標記。

<form> 

</form> 

此外,表單的動作要麼指向自己或處理該圖像處理該頁面(在你的情況下,同一個文件)

<?php 
echo("<form id=\"formid\" name=\"formid\" method=\"post\" action=\"".$PHP_SELF."\" 

</form>"); 
?> 
+0

添加這兩種類型,但使用INI聲明MSpreji曾提到過,當我還得到一個錯誤 - 未定義指數:文件1 – lostInTransit 2009-08-06 14:03:10

+0

嘗試一個簡單的測試,然後用1文件中的字段, 和發佈後創建表單。 ,使用的print_r($ _ FILES)看看信息通過與否。 如果連這個也不行,有一個服務器設置多數民衆贊成furking的事情了。 – Coreus 2009-08-07 11:57:29