PHP上传不起作用,可能是权限问题

时间:2015-05-31 11:30:25

标签: php file-permissions

我需要在我的网站上建一个上传页面,我正在使用一个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.";
    }
}
?>

3 个答案:

答案 0 :(得分:1)

从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);
}
?>

答案 1 :(得分:0)

正如您在帖子中建议的那样,请尝试按照以下代码更改文件夹的权限:

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

答案 2 :(得分:0)

你正在使用绝对路径linux类型,/ tmp / uploads,如果你使用linux尝试使用&#34; ls -l / tmp / uploads&#34;显示权限文件夹,如果你使用的是Windows主机,也许你可以打印路径$ target_file,在w3schools示例中,tagtet_path没有&#34; /&#34; &#34; TMP /上传&#34; !=&#34; / tmp / uploads&#34;。