Php 移动上传的文件不起作用
我尝试了不同的方法来实现文件上传系统。到目前为止,它们都没有起作用,我知道有很多类似的问题,我检查并尝试了几乎所有的问题,但它们都没有起作用 如果我试图上传文件,它会给出最后一次回音。如果文件超过最大大小,则会发出错误的回显“抱歉,不允许使用此文件类型” 这是我的php代码Php 移动上传的文件不起作用,php,html,file-upload,Php,Html,File Upload,我尝试了不同的方法来实现文件上传系统。到目前为止,它们都没有起作用,我知道有很多类似的问题,我检查并尝试了几乎所有的问题,但它们都没有起作用 如果我试图上传文件,它会给出最后一次回音。如果文件超过最大大小,则会发出错误的回显“抱歉,不允许使用此文件类型” 这是我的php代码 <?php $target_dir = "uploads/"; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uplo
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$FileType = pathinfo($target_file,PATHINFO_EXTENSION);
// 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;
}
// Allow certain file formats
if($FileType == "exe" && $FileType == "dll" && $FileType == "zip" ) {
echo "Sorry, this filetype is not allowed";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo " <br>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.";
}
}
?>
<html>
<head> <link rel="stylesheet" href="table.css"></head>
<title>Dosya Yükleme</title>
<body>
<form action="fileupload.php" method="post" enctype="multipart/form-data">
Bir dosya seçiniz:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload" name="Yükle">
</form>
</body>
</html>
这是HTML代码
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$FileType = pathinfo($target_file,PATHINFO_EXTENSION);
// 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;
}
// Allow certain file formats
if($FileType == "exe" && $FileType == "dll" && $FileType == "zip" ) {
echo "Sorry, this filetype is not allowed";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo " <br>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.";
}
}
?>
<html>
<head> <link rel="stylesheet" href="table.css"></head>
<title>Dosya Yükleme</title>
<body>
<form action="fileupload.php" method="post" enctype="multipart/form-data">
Bir dosya seçiniz:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload" name="Yükle">
</form>
</body>
</html>
多西娅·尤克勒姆
Bir dosya seçinizing:
我简化了代码,将php和html放在同一个php文件中,并删除了检查过程
仍然无法工作的新PHP文件
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$FileType = pathinfo($target_file,PATHINFO_EXTENSION);
if (isset($_POST['upload'])) {
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.";
}
}
?>
<html>
<head> <link rel="stylesheet" href="table.css"></head>
<title>Dosya Yükleme</title>
<body>
<form action="fileupload.php" method="post" enctype="multipart/form-data">
Bir dosya seçiniz:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload" name="upload">
</form>
</body>
</html>
多西娅·尤克勒姆
Bir dosya seçinizing:
解决方案chmod-R 777将类型条件分支中的
&
更改为|
:
if($FileType == "exe" || $FileType == "dll" || $FileType == "zip" ) {
我认为一个问题是提交按钮,并试图用
if(isset($\u POST['upload']){
检查其值。似乎按钮的名称需要是submit
看看。我在ubuntu论坛上找到了解决方案。问题是文件权限。曾经有一篇关于文件权限的帖子,但代码是错误的。在这个主题上,终端代码是chmod 0751-R folder,但工作代码是错误的
chmod-R 777文件夹谢谢,但它仍然不起作用。我想至少我们排除了第二个错误。但更重要的是,它仍然没有上传文件do调试:添加
var\u dump($variable)
在你的代码中为每一个正在使用的变量添加代码,并将变量的内容与你的假设进行比较。dublicate标签是不必要的,因为解决方案是不相关的。我在ubuntu论坛上找到了解决方案。问题是文件权限。曾经有一篇关于文件权限的帖子,但代码是错误的。在这个主题上终端代码是chmod 0751-R folder,但工作代码是chmod-R 777 folderI发现它们似乎没有什么不同,但解决方案不起作用。如果你找到一个真正可以解决它的aswer,请发送链接给我。dublicate标签是不必要的,因为解决方案是无关的。我在ubuntu论坛上找到了解决方案。问题出在哪里是文件权限。曾经有一篇关于文件权限的帖子,但代码是错误的。在这个主题上,终端代码是chmod 0751-R folder,但工作代码是chmod-R 777 folder。请将您的解决方案作为答案发布,而不是将其编辑到问题中。谢谢!我不知道如何$\u post['upload']如果您将name属性更改为'submit'
,则
将起作用。如果其他人没有相同的设置,则关闭为不可复制。也就是说,了解这一点很有用。