2015-10-13 23 views
1

我有一個名爲uploads的文件夾,以及名爲upload.htmlupload.php的文件。我upload.html這個樣子的(好吧,至少相關部分做):指定上傳文件的正確目錄

$(document).ready(function(){ 
    $("#pictureUploadSubmit").submit(function(event) { 
     var file_data = $('#fileToUpload').prop('files')[0]; 
     var form_data = new FormData();     
     form_data.append('fileToUpload', file_data); 
     alert(form_data);        
     $.ajax({ 
      url: 'upload.php', // point to server-side PHP script 
      dataType: 'text', // what to expect back from the PHP script, if anything 
      cache: false, 
      contentType: false, 
      processData: false, 
      data: form_data,       
      type: 'post', 
      success: function(php_script_response){ 
       alert(php_script_response); // display response from the PHP script, if any 
      } 
     }); 
        event.preventDefault(); 
    }); 
}); 

而且我upload.php看起來是這樣的:

<?php 
$target_dir = "uploads/"; 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
$uploadOk = 1; 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
// Check if image file is a actual image or fake image 
if(isset($_POST["submit"])) { 
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); 
    if($check !== false) { 
     echo "File is an image - " . $check["mime"] . "."; 
     $uploadOk = 1; 
    } else { 
     echo "File is not an image."; 
     $uploadOk = 0; 
    } 
} 
// Check if file already exists 
if (file_exists($target_file)) { 
    echo "Sorry, file already exists."; 
    $uploadOk = 0; 
} 
// Check file size 
if ($_FILES["fileToUpload"]["size"] > 500000) { 
    echo "Sorry, your file is too large."; 
    $uploadOk = 0; 
} 
// Allow certain file formats 
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" 
&& $imageFileType != "gif") { 
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; 
    $uploadOk = 0; 
} 
// Check if $uploadOk is set to 0 by an error 
if ($uploadOk == 0) { 
    echo "Sorry, your file was not uploaded."; 
// if everything is ok, try to upload file 
} else { 
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { 
     echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded."; 
    } else { 
     echo "Sorry, there was an error uploading your file."; 
    } 
} 
?> 

現在,文件似乎被上載,但我認爲這是被上傳到不正確的目錄中,因爲當我嘗試上傳同一個文件兩次時,它會迴響,Sorry, file already exists,並且當圖像成功上傳時,它會迴響"The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded."。但是,我的FireFTP中已存在的文件未顯示在我的uploads文件夾中。

我相信我已經允許適當的權限,即允許讀取,寫入和執行我的上傳文件夾。

所以我認爲這是我的誤導。我試圖搜索上傳的文件,我想我檢查了所有目錄,但找不到它們。任何人都可以解決這個問題?

回答

1

您正在使用上傳的相對目錄,

$target_dir = "uploads/"; 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
# ... 
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file) 

這將文件移動到相對的上傳目錄到哪裏這個腳本運行。查看upload.php所在的目錄,那裏可能有一個上傳目錄和你的文件。

如果不是這種情況,我建議使用$target_dir的絕對路徑,這樣您可以確信自己知道將要放置最終文件的位置。

+0

謝謝您的迴應Dan,當您說「目錄與該腳本運行的任何地方相關時,您是什麼意思?」我認爲你的意思是,我的uploads.php在哪裏就像起始點那樣?而上傳/'只是相對於那個?但事情是,除了上傳目錄之外沒有其他目錄...並且我已經檢查了所有其他目錄,並且它們沒有這些文件。 –

+0

那麼,我將如何繼續爲'$ target_dir'設置絕對路徑? –

+0

您會將完整路徑放入該變量中。 '$ target_dir =「/ var/tmp/uploads /」;'(使用它應該在你的服務器上的任何東西)。 –