PHP 上传失败,可能是权限问题

PHP upload not working, maybe permission issues

我需要在我的网站上制作一个上传页面,我使用的是 altervista 试用服务器。 我用了教程http://www.w3schools.com/php/php_file_upload.asp 但上传不起作用。也许,正如我所读,这是一个权限问题,但我对如何更改我的文件夹的权限一无所知。

是否可以在可上传文件中也添加 pdf 文件?

谢谢

uploadpage.html

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documento senza titolo</title>
<link href="valsilehome.css" rel="stylesheet" type="text/css">
</head>

<body>


<form action="/tmp/upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

upload.php

<?php
$target_dir = "/tmp/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;
}

// 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.";
    }
}
?>

按照您在 post 中的建议,尝试按照以下代码更改文件夹的权限:

chmod("/tmp/uploads", 0777);

您正在使用绝对路径 linux 类型,/tmp/uploads,如果您正在使用 linux,请尝试使用 "ls -l /tmp/uploads" 显示权限文件夹,如果您正在使用 windows 主机也许你可以打印路径 $target_file,在 w3schools 示例中 tagtet_path 没有“/” "tmp/uploads" != “/tmp/uploads”.

截至w3学校,他们已经为榜样创造了很多条件。但我们不需要严格遵守。我们可以只在需要时使用它。

这是您可以拥有的最小代码

<!DOCTYPE html>
<html>
<body>
<form action="" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
<?php
if (isset($_POST['submit'])) 
{
echo 'succesfully uploaded';
$structure = 'uploadedfiles/';
$target_file = $structure.basename($_FILES["fileToUpload"]["name"]);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
}
?>

如果您使用上述代码,您应该在保存此文件的文件夹中创建一个名为 uploadedfiles 的文件夹。

否则,如果您需要为每个文件上传创建每个文件夹,那么您应该编写代码。它每次都会将文件名创建为文件夹。

<!DOCTYPE html>
<html>
<body>
<form action="" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
<?php
if (isset($_POST['submit'])) 
{
echo 'succesfully uploaded';
$structure = $_FILES["fileToUpload"]["name"];
if (!mkdir($structure, 777, true)) 
{}
$target_file = $structure.basename($_FILES["fileToUpload"]["name"]);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
}
?>