我有一個名爲uploads
的文件夾,以及名爲upload.html
和upload.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
文件夾中。
我相信我已經允許適當的權限,即允許讀取,寫入和執行我的上傳文件夾。
所以我認爲這是我的誤導。我試圖搜索上傳的文件,我想我檢查了所有目錄,但找不到它們。任何人都可以解決這個問題?
謝謝您的迴應Dan,當您說「目錄與該腳本運行的任何地方相關時,您是什麼意思?」我認爲你的意思是,我的uploads.php在哪裏就像起始點那樣?而上傳/'只是相對於那個?但事情是,除了上傳目錄之外沒有其他目錄...並且我已經檢查了所有其他目錄,並且它們沒有這些文件。 –
那麼,我將如何繼續爲'$ target_dir'設置絕對路徑? –
您會將完整路徑放入該變量中。 '$ target_dir =「/ var/tmp/uploads /」;'(使用它應該在你的服務器上的任何東西)。 –