2013-07-25 89 views
0

我想通過網絡表單上傳文件。該腳本輸出以下內容,並且沒有錯誤。但我仍然無法在服務器上找到目錄。我指定的位置錯了嗎?我已經嘗試過/上傳,我在我的FTP客戶端看到的完整路徑:/ cygdrive /我的/ home/ANTOR /舞蹈/上傳move_uploaded_file在哪裏存儲文件?

Upload: 1.jpg 
    Type: image/jpeg 
    Size: 107.9072265625 kB 
    Stored in: C:\WINDOWS\Temp\php767.tmpUpload: 1.jpg 
    Type: image/jpeg 
    Size: 107.9072265625 kB 
    Temp file: C:\WINDOWS\Temp\php767.tmpStored in: upload/1.jpgUpload: 1.jpg 
    Type: image/jpeg 
    Size: 107.9072265625 kB 
    Temp file: C:\WINDOWS\Temp\php767.tmp 
    Stored in: /cygdrive/i/home/antor/dance/upload/1.jpg 

這是我使用的代碼:

<form action="upload_file.php" method="post" 
    enctype="multipart/form-data"> 
    <label for="file">Filename:</label> 
    <input type="file" name="file" id="file"><br> 
    <input type="submit" name="submit" value="Submit"> 
    </form> 

和:

<?php 
    if ($_FILES["file"]["error"] > 0) 
     { 
     echo "Error: " . $_FILES["file"]["error"] . "<br>"; 
     } 
    else 
     { 
     echo "Upload: " . $_FILES["file"]["name"] . "<br>"; 
     echo "Type: " . $_FILES["file"]["type"] . "<br>"; 
     echo "Size: " . ($_FILES["file"]["size"]/1024) . " kB<br>"; 
     echo "Stored in: " . $_FILES["file"]["tmp_name"]; 
     } 
     $allowedExts = array("gif", "jpeg", "jpg", "png"); 
    $extension = end(explode(".", $_FILES["file"]["name"])); 
    if ((($_FILES["file"]["type"] == "image/gif") 
    || ($_FILES["file"]["type"] == "image/jpeg") 
    || ($_FILES["file"]["type"] == "image/jpg") 
    || ($_FILES["file"]["type"] == "image/pjpeg") 
    || ($_FILES["file"]["type"] == "image/x-png") 
    || ($_FILES["file"]["type"] == "image/png")) 
    && ($_FILES["file"]["size"] < 2000000) 
    && in_array($extension, $allowedExts)) 
     { 
     if ($_FILES["file"]["error"] > 0) 
     { 
     echo "Error: " . $_FILES["file"]["error"] . "<br>"; 
     } 
     else 
     { 
     echo "Upload: " . $_FILES["file"]["name"] . "<br>"; 
     echo "Type: " . $_FILES["file"]["type"] . "<br>"; 
     echo "Size: " . ($_FILES["file"]["size"]/1024) . " kB<br>"; 
     echo "Stored in: " . $_FILES["file"]["tmp_name"]; 
     } 
     } 
    else 
     { 
     echo "Invalid file"; 
     } 


     $allowedExts = array("gif", "jpeg", "jpg", "png"); 
    $extension = end(explode(".", $_FILES["file"]["name"])); 
    if ((($_FILES["file"]["type"] == "image/gif") 
    || ($_FILES["file"]["type"] == "image/jpeg") 
    || ($_FILES["file"]["type"] == "image/jpg") 
    || ($_FILES["file"]["type"] == "image/pjpeg") 
    || ($_FILES["file"]["type"] == "image/x-png") 
    || ($_FILES["file"]["type"] == "image/png")) 
    && ($_FILES["file"]["size"] < 2000000) 
    && in_array($extension, $allowedExts)) 
     { 
     if ($_FILES["file"]["error"] > 0) 
     { 
     echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; 
     } 
     else 
     { 
     echo "Upload: " . $_FILES["file"]["name"] . "<br>"; 
     echo "Type: " . $_FILES["file"]["type"] . "<br>"; 
     echo "Size: " . ($_FILES["file"]["size"]/1024) . " kB<br>"; 
     echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>"; 

     if (file_exists("upload/" . $_FILES["file"]["name"])) 
      { 
      echo $_FILES["file"]["name"] . " already exists. "; 
      } 
     else 
      { 
      move_uploaded_file($_FILES["file"]["tmp_name"], 
      "upload/" . $_FILES["file"]["name"]); 
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; 
      } 
     } 
     } 
    else 
     { 
     echo "Invalid file"; 
     } 

    ?> 

文件夾上傳位於主uploader.php文件的下方,並且擁有777個權限。 任何想法存儲文件的位置?或者更好的是,我怎樣才能把它放在正確的目錄中。

--UPDATE 更新/文件夾似乎是正確的位置。仍然沒有出現該文件,我也嘗試過在不同的服務器上。我在劇本中做錯了什麼?是否有一些額外的錯誤檢查我可以做?

.htaccess可以與它有任何關係嗎?有一個Drupal站點上面的舞蹈級別,其中有.htaccess:

# 
# Apache/PHP/Drupal settings: 
# 

# Protect files and directories from prying eyes. 
<FilesMatch "\.(engine|inc|info|install|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl|svn-base)$|^(code-style\.pl|Entries.*|Repository|Root|Tag|Template|all-wcprops|entries|format)$"> 
    Order allow,deny 
</FilesMatch> 

# Don't show directory listings for URLs which map to a directory. 
Options -Indexes 

# Follow symbolic links in this directory. 
Options +FollowSymLinks 

# Make Drupal handle any 404 errors. 
ErrorDocument 404 /index.php 

# Force simple error message for requests for non-existent favicon.ico. 
<Files favicon.ico> 
    # There is no end quote below, for compatibility with Apache 1.3. 
    ErrorDocument 404 "The requested file favicon.ico was not found. 
</Files> 

# Set the default handler. 
DirectoryIndex index.php 

# Override PHP settings. More in sites/default/settings.php 
# but the following cannot be changed at runtime. 

# PHP 4, Apache 1. 
<IfModule mod_php4.c> 
    php_value magic_quotes_gpc    0 
    php_value register_globals    0 
    php_value session.auto_start    0 
    php_value mbstring.http_input    pass 
    php_value mbstring.http_output   pass 
    php_value mbstring.encoding_translation 0 
</IfModule> 

# PHP 4, Apache 2. 
<IfModule sapi_apache2.c> 
    php_value magic_quotes_gpc    0 
    php_value register_globals    0 
    php_value session.auto_start    0 
    php_value mbstring.http_input    pass 
    php_value mbstring.http_output   pass 
    php_value mbstring.encoding_translation 0 
</IfModule> 

# PHP 5, Apache 1 and 2. 
<IfModule mod_php5.c> 
    php_value magic_quotes_gpc    0 
    php_value register_globals    0 
    php_value session.auto_start    0 
    php_value mbstring.http_input    pass 
    php_value mbstring.http_output   pass 
    php_value mbstring.encoding_translation 0 
</IfModule> 

# Requires mod_expires to be enabled. 
<IfModule mod_expires.c> 
    # Enable expirations. 
    ExpiresActive On 

    # Cache all files for 2 weeks after access (A). 
    ExpiresDefault A1209600 

    # Do not cache dynamically generated pages. 
    ExpiresByType text/html A1 
</IfModule> 

# Various rewrite rules. 
<IfModule mod_rewrite.c> 
    RewriteEngine on 

    # If your site can be accessed both with and without the 'www.' prefix, you 
    # can use one of the following settings to redirect users to your preferred 
    # URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option: 
    # 
    # To redirect all users to access the site WITH the 'www.' prefix, 
    # (http://example.com/... will be redirected to http://www.example.com/...) 
    # adapt and uncomment the following: 
    # RewriteCond %{HTTP_HOST} ^example\.com$ [NC] 
    # RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301] 
    # 
    # To redirect all users to access the site WITHOUT the 'www.' prefix, 
    # (http://www.example.com/... will be redirected to http://example.com/...) 
    # uncomment and adapt the following: 
    # RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC] 
    # RewriteRule ^(.*)$ http://example.com/$1 [L,R=301] 

    # Modify the RewriteBase if you are using Drupal in a subdirectory or in a 
    # VirtualDocumentRoot and the rewrite rules are not working properly. 
    # For example if your site is at http://example.com/drupal uncomment and 
    # modify the following line: 
    # RewriteBase /drupal 
    # 
    # If your site is running in a VirtualDocumentRoot at http://example.com/, 
    # uncomment the following line: 
    # RewriteBase/

    # Rewrite URLs of the form 'x' to the form 'index.php?q=x'. 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_URI} !=/favicon.ico 
    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA] 
</IfModule> 

# $Id: .htaccess,v 1.90.2.3 2008/12/10 20:04:08 goba Exp $ 
+0

您的代碼正在將數據存儲在Temp中,並將其移至上傳文件夾。我會推薦使用755 –

+0

他們現在有755個權限,沒有區別 – dorien

回答

1

如果該文件夾是PHP文件下面,然後

move_uploaded_file($_FILES["file"]["tmp_name"], 
    "upload/" . $_FILES["file"]["name"]); 

正試圖把它上傳到一個「上傳」目錄上的同一級別的文件從此運行。

嘗試

move_uploaded_file($_FILES["file"]["tmp_name"], 
    "../upload/" . $_FILES["file"]["name"]); 
+0

這似乎是有道理的。然而,我嘗試了它,仍然沒有文件。我確實注意到權限跳回到755,但是因爲我被列爲主人,我認爲沒關係。任何方式我可以檢查移動過程中的錯誤? – dorien

+1

我會在那裏放一個名爲testfile.php的文件,然後從這個腳本if(file_exists(「upload/testfile.php」)){echo'correct path';} else {echo'Incorrect Path';},那會告訴你路徑是否正確。如果是,那麼你已經縮小了搜索範圍 - 嘗試777權限以及該文件是由apache創建的,它可能不屬於你,如果這樣做,那麼你知道它是權限,你可以調整到一個更安全,也允許你寫 – JohnnyFaldo

+0

它說的是正確的路徑。我無法將權限更改爲777,它們始終默認爲755,不能完全控制Web服務器。至少我知道這只是「上傳/」,但爲什麼不是該文件出現? – dorien

1

您錯過了表單的enctype屬性。添加到您的表單標籤

enctype='multipart/form-data' 
+0

已經添加到表格 – ABorty

+0

抱歉,看不清楚:) –

+0

已經在那裏。 – dorien

1

創建一個文件info.php的,並把下面的內容吧。

<?php 
phpinfo(); 

上傳到您的服務器上您的上傳腳本,在瀏覽器中打開,找到:

_SERVER["SCRIPT_FILENAME"] 

這將顯示您完整的文件路徑,有助於找出路徑上傳文件夾。

正確的路徑將是:

move_uploaded_file($_FILES["file"]["tmp_name"], "I:\home\antor\dance\upload\\" . $_FILES["file"]["name"]); 
+0

好吧,輸出「我:\ home \ antor \ dance \ upload \」,所以它會顯示爲一個Windows的服務器。將此作爲路徑混淆視圖,而不是/ move_uploaded_file($ _ FILES [「file」] [「tmp_name」], 「I:\ home \ antor \ dance \ upload \\」。$ _FILES [文件名」]); – dorien

+0

也試過antor/dance/upload /,但是沒有做到。也許在別的地方有一個錯誤,而不是移動? – dorien

+0

只需添加一個斜槓即可。正確的是:move_uploaded_file($ _FILES [「file」] [「tmp_name」],「I:\ home \ antor \ dance \ upload \\」。 –

0

確保在源或主目錄

創建上傳目錄
+0

上傳目錄被創建並且在我的home/antor/dance下 – dorien