文件没有上传我正在使用apache 2.4.33

时间:2018-04-12 09:50:44

标签: php apache server

我正在尝试使用简单的PHP脚本上传图片 以下代码:

<!DOCTYPE html>
<!DOCTYPE html>
<html>
<head>
  <title>Upload your files</title>
</head>
<body>
  <form enctype="multipart/form-data" action="uploadfile.php" method="POST">
    <p>Upload your file</p>
    <input type="file" name="uploaded_file"></input><br />
    <input type="submit" value="Upload"></input>
  </form>
</body>
</html>
<?php
//echo exec('whoami');
//die($_FILES['uploaded_file']);
  if(!empty($_FILES['uploaded_file']))
  {
    $path = "./img/backend/uploads/enquiry/";
    $path = $path . basename( $_FILES['uploaded_file']['name']);
    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
      echo "The file ".  basename( $_FILES['uploaded_file']['name']). 
      " has been uploaded";
    } else{
        echo "There was an error uploading the file, please try again!";
      }
    }
   ?>

我使用的是php 7.1我已经检查了文件夹的权限。但是当我尝试提交时,请带我到url中使用@custom类的东西。但如果我尝试在没有图像的情况下发布它就可以正常工作。

有什么问题?我认为没有正确处理的apache。在我的其他服务器上工作正常。

就在这台服务器上我使用auto ssl,这是问题吗?

看到它带我的图像,这个链接看起来像是没有提交或被服务器拒绝 enter image description here

2 个答案:

答案 0 :(得分:0)

请更改此

的路径
$path = __DIR__."/img/backend/uploads/enquiry/";

答案 1 :(得分:0)

您在文件路径中缺少一个点。请尝试以下方法:

$path = "./img/backend/uploads/enquiry/";

完整代码:

<!DOCTYPE html>
<html>
<head>
  <title>Upload your files</title>
</head>
<body>
  <form enctype="multipart/form-data" action="#" method="POST">
    <p>Upload your file</p>
    <input type="file" name="uploaded_file"></input><br />
    <input type="submit" value="Upload"></input>
  </form>
</body>
</html>
<?php
if(!empty($_FILES['uploaded_file'])){
    $path = "./img/backend/uploads/enquiry/";
    $path = $path . basename( $_FILES['uploaded_file']['name']);
    echo $path;
    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
        echo "The file ".  basename( $_FILES['uploaded_file']['name']). 
        " has been uploaded";
    } else{
        echo "There was an error uploading the file, please try again!";
    }
}
?>

如果没有dot,文件路径也将采用脚本名称,例如,如果您的PHP脚本/文件名是&#34; test.php&#34;,那么在文件路径中没有前导点将创建一个类似的路径(www是保存test.php文件的文件夹):

/www/test.php/img/backend/uploads/enquiry/

因此它将是一个无效的文件路径,带有一个前导点,它将跳过它(外行术语),你的路径将是:

/www/img/backend/uploads/enquiry/

此路径是有效路径,因此会上传。