上传网页脚本

时间:2015-09-26 19:36:57

标签: php

所以我正在尝试为我的网页制作一个上传脚本,我似乎无法使其正常工作。尝试上传文件时,地址栏显示“webpage.com/upload.php”,但无法加载页面或上传文件。 php.ini上传启用了10240M(10G)上传最大尺寸。目标只是一个简单的上传脚本,不会接受2个同名文件。这是有缺陷的代码。如果你想尝试它,请注释掉css和include页眉/页脚。我离开了那些它确实影响了某些东西。

在网页上(位于/var/www/content/file.php):

<?php session_start(); ?>
<html>
<head>
    <title>Kaiocraft</title>
    <link rel="stylesheet" type="text/css" href="/style.css">
</head>
<body>
    <?php $logo="/logo.png"; include("/var/www/header.php"); ?>
        <form action="/upload.php" method="post" enctype="multipart/form-data">
            <p>file to upload:</p>
            <input type="file" name="filetoupload" id="filetoupload">
            <input type="submit" value="upload" name="submit">
        </form>
    <?php include("/var/www/footer.php"); ?>
</body>
</html>
PHP脚本中的

(位于/var/www/upload.php):

<?php
$target_dir = "/var/www/uploads/";
$target_file = $target_dir . basename($_FILES["filetoupload"]["name"]);
$uploadOk = 1;
if (file_exists($target_file)) {
    echo "<p>a file with that name already exists</p>";
    uploadOk = 0;
}
if ($uploadOk == 1) {
    if (move_uploaded_file($_FILES["filetoupload"]["tmp_name"],$target_file)) {
    echo "<p>uploaded successfully</p>";
} else {
    echo "<p>error. please try again</p>";
    }
}
?>

和/ var / www / uploads中的空白文件夹保存到。

并且我知道这是一种超级冒险且不安全的方法,但它必须接受任何高达10G的文件类型。

编辑: 当我发表评论时

if (file_exists($target_file)) {
    echo "<p>a file with that name already exists</p>";
    uploadOk = 0;
}

在PHP脚本中它可以工作,但是我可以使用同名文件覆盖它。

2 个答案:

答案 0 :(得分:1)

basename(S_FILES["filetoupload"]["name"]);

应该是

basename($_FILES["filetoupload"]["name"]);

你有S而不是$

另外,在php_ini中设置post_max_size,而不仅仅是upload_max_filesize

这是你重写的上传脚本(它可以工作,我已经测试过了)

<?php
// show errors, disable this on production server
error_reporting(E_ALL);
ini_set('display_errors', 1);


$target_dir = "/var/www/inovision.ro/web/ddns/up/";
$target_file = $target_dir.basename($_FILES["filetoupload"]["name"]);

if(!file_exists($target_file) && isset($_FILES["filetoupload"]["error"]) && $_FILES["filetoupload"]["error"] == 0) {
    move_uploaded_file($_FILES["filetoupload"]["tmp_name"], $target_file);
    echo "<p>uploaded successfully</p>";
}
else if(file_exists($target_file)) {
     echo "<p>a file with that name already exists</p>";
}
else {
    echo "<p>error uploading</p>";
}

?>

答案 1 :(得分:0)

确保您的上传文件夹存在,并且系统上的网络用户可以写入

例如在Ubuntu上,用户是www-data,权限应该是775。