我的HTML格式:
<form action='' method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Submit">
</form>
我的php文件:
if ($_POST['submit'] == "Submit") {
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("/downloads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"/downloads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "/downloads/" . $_FILES["file"]["name"];
}
}
我的public_html中有2个文件夹:tmp
和downloads
,均为777权限(仅供测试)
此文件位于下载的.htaccess受保护文件夹中(public_html / downloads / new / update.php) 我希望将zip文件上传到下载目录。
此代码不会给我任何错误,但不会上传文件。为什么呢?
答案 0 :(得分:2)
尝试添加
if(move_uploaded_file(...)){
echo "it works";
} else {
echo "NOPE";
}
和 替换:
move_uploaded_file($_FILES["file"]["tmp_name"], "/downloads/" . $_FILES["file"]["name"]);
使用:
move_uploaded_file($_SERVER['DOCUMENT_ROOT'].'/'.$_FILES["file"]["tmp_name"], $_SERVER['DOCUMENT_ROOT']."/downloads/" . $_FILES["file"]["name"]);
答案 1 :(得分:1)
PHP需要MAX_FILE_SIZE才能接收上传的文件
<form action='' method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="10240000">
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Submit">
</form>
使用此表单,您可以上传10 MB或更少的zip存档...
你还必须将php.ini中的max_post_size和upload_max_filesize设置为相同或更高的值来上传文件
使用此PHP代码
if ((isset($_POST['submit']) && $_POST['submit'] == "Submit") AND isset($_FILES)) {
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("/downloads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
if(move_uploaded_file($_FILES["file"]["tmp_name"],
"/downloads/" . $_FILES["file"]["name"]))
echo "Stored in: " . "/downloads/" . $_FILES["file"]["name"];
else echo "file could not be processed";
}
}
答案 2 :(得分:0)
这就是我解决它的方法:
HTML:
<form enctype="multipart/form-data" method="post" action="">
<input type="hidden" name="MAX_FILE_SIZE" value="300000" />
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Submit">
</form>
PHP:
if($_POST['submit'] == "Submit") {
$filename = $_FILES["file"]["name"];
$source = $_FILES["file"]["tmp_name"];
$type = $_FILES["file"]["type"];
$name = explode(".", $filename);
$target_path = "../".$filename;
if(move_uploaded_file($source, $target_path)) {
$message = "Your .zip file was uploaded";
} else {
$message = "ERROR";
}
if($message) echo $message;
}