繁体   English   中英

指定用于上传文件的正确目录

[英]Specifying the correct directory for uploading files

我有一个名为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." "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded." 但是,我的FireFTP中已经存在的文件没有显示在我的uploads文件夹中。

我相信我已经允许了适当的权限,即我允许对我的上载文件夹进行读取,写入和执行。

所以我认为这是我的target directory的错误方向。 我尝试搜索上载的文件,我想我检查了所有目录,但找不到它们。 有人对此问题有解决方案吗?

您正在使用相对目录进行上传,

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

这会将文件移动到相对于此脚本运行位置的上载目录。 查看upload.php所在的目录,其中可能有一个包含文件的上载目录。

如果不是这种情况,建议您为$target_dir使用绝对路径,这样您就可以确定最终文件将放置在何处。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM