2013-03-19 57 views
0

我有一個文件上傳腳本:PHP文件上傳move_uploaded_file()以未能打開流

$allowedExts = array("gif", "jpeg", "jpg", "png"); 
$extension = end(explode(".", $_FILES["picture"]["name"])); 
if ((($_FILES["picture"]["type"] == "image/gif") || ($_FILES["picture"]["type"] == "image/jpeg") || ($_FILES["picture"]["type"] == "image/jpg") || ($_FILES["picture"]["type"] == "image/png")) && in_array($extension, $allowedExts)): 
    if($_FILES["picture"]["error"] > 0): 
     echo "Error: " . $_Files["picture"]["error"]; 
    else: 
     move_uploaded_file($_FILES["picture"]["tmp_name"], "/TnA/ProfilePics/" . $_SESSION['ID'] . "." . $extension); 
    endif; 
endif; 

但我得到的錯誤:

Warning: move_uploaded_file(TnA/ProfilePics/1.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/content/91/9848191/html/TnA/webservice.php on line 1067 

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php5nOdhI' to 'TnA/ProfilePics/1.jpg' in /home/content/91/9848191/html/TnA/webservice.php on line 1067 

這裏是我的文件結構:

Web Host Root (I have another site here) 
    -TnA (Root of this site) 
     -index.html 
     -webservice.php 
     -ProfilePics 
      -(My target location) 

我應該使用什麼相對的目錄url?我試過ProfilePics/1.jpg/ProfilePics/1.jpg都導致相同的錯誤。

編輯:

使用:

move_uploaded_file($_FILES["picture"]["tmp_name"], dirname(__FILE__) . "ProfilePics/" . $_SESSION['ID'] . "." . $extension); 

我得到:

Warning: move_uploaded_file(/home/content/91/9848191/html/TnA/ProfilePics/1.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/content/91/9848191/html/TnA/webservice.php on line 1067 

回答

3

最有可能你使用相關的路徑有問題。如果您的上傳腳本是webservice.php,那麼您實際上是試圖將文件放入 /%webHostRoot%/ TnA/TnA/ProfilePics/ 目錄。在move_uploaded_file()的目標中使用完整路徑。 嘗試使用類似:

move_uploaded_file($_FILES["picture"]["tmp_name"], dirname(__FILE__)."ProfilePics/" . $_SESSION['ID'] . "." . $extension); 

UPD: 這裏的FUNC遞歸創建迪爾斯:

function MkDirTree($path,$permissions = 0755, $compat_mode=true) { 
    if (!$compat_mode) { 
     $dirs = split("/",$path); 
     $path = ""; 
     foreach ($dirs as $key=>$dir) { 
      $path .= $dir."/"; 
      if ($dir!="" && !is_dir($path)) exec("mkdir -m ".$permissions." ".$path); 
     } 
    } else { 
     $dirs = split("/",$path); 
     $path = ""; 
     foreach ($dirs as $key=>$dir) { 
      $path .= $dir."/"; 
      if ($dir!="" && !file_exists($path)) mkdir($path, $permissions); 
     } 
    } 
    return file_exists($path); 
} 
+0

如果我使用了完整的HTTP://地址,我得到'警告:move_uploaded_file(HTTP:/ /html/TnA/ProfilePics/1.jpg)[function.move-uploaded-file]:無法打開流:HTTP包裝器不支持/ home/content/91/9848191/html/TnA/webservice.php on line 1067' – SnareChops 2013-03-19 19:43:40

+0

您應該使用**服務器的**內部絕對路徑,而不是您網站的網址。 我用代碼示例編輯了我的答案。 – 2013-03-19 19:47:04

+0

更緊密,但現在我得到這個錯誤(請參閱編輯的問題) – SnareChops 2013-03-19 19:53:34

相關問題