【问题标题】:Can not Upload file inside folder using PHP无法使用PHP上传文件夹内的文件
【发布时间】:2016-01-20 11:57:38
【问题描述】:

在项目文件夹中上传文件时遇到问题。我在下面解释我的代码。

$imageName=generateRandomNumber().'_'.$_FILES["uploadme"]["name"];
$target_dir = "upload1/";
    $target_file = $target_dir . basename($imageName);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    $check = getimagesize($_FILES["uploadme"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
      if (file_exists($target_file)) {
      echo "Sorry, file already exists.";
      $uploadOk = 0;
    }
    // Check file size
   if ($_FILES["uploadme"]["size"] > 500000) {
      echo "Sorry, your file is too large.";
      $uploadOk = 0;
    }
   // Allow certain file formats
   if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
      && $imageFileType != "gif" ) {
      echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
      $uploadOk = 0;
   }
  // Check if $uploadOk is set to 0 by an error
  if ($uploadOk == 0) {
     echo "Sorry, your file was not uploaded.";
   // if everything is ok, try to upload file
  } else {
    if (move_uploaded_file($_FILES["uploadme"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["uploadme"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
  }

我在这里获取图像并添加一个随机数但是在上传图像时无法保存在upload1 文件夹中。这里我收到Sorry, there was an error uploading your file 消息。请帮我解决这个问题。

【问题讨论】:

  • 检查文件夹的权限。
  • 从脚本文件的位置确认你的文件夹路径是正确的
  • 您使用的是linux操作系统吗?如果是这样,那就是文件夹权限问题。在终端中执行这个“sudo chmod 777 -R folder-path”命令
  • 添加var_dump(error_get_last())到else部分,看看错误并修复它
  • 该文件夹的权限已经存在。

标签: javascript php file


【解决方案1】:

通过右键单击文件夹并选择权限选项卡来授予文件夹权限,然后通过允许从文件夹中创建和删除文件来授予文件夹权限。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    • 2015-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    相关资源
    最近更新 更多