所以我正在尝试为我的网页制作上传脚本,但我似乎无法使其工作。在尝试上传文件时,地址栏显示"webpage.com/upload.php",但无法加载页面或上传文件。php.ini已启用上传,最大上传大小为10240M(10G(。目标只是一个简单的上传脚本,它不会接受两个同名文件。这是有缺陷的代码。如果你想试试的话,请把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脚本中,它是有效的,但是我可以用相同名称的文件覆盖它。
basename(S_FILES["filetoupload"]["name"]);
应该是
basename($_FILES["filetoupload"]["name"]);
你有S而不是$
此外,在php_ini中设置post_max_size,而不仅仅是上传_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>";
}
?>
确保您的上传文件夹存在,并且web用户可以在您的系统上写入
例如,在Ubuntu上,用户是www数据,权限应该是775。