move_uploaded_file()不移动上传的文件

时间:2016-04-23 11:22:17

标签: php

我无法将上传的文件(本例中的图像)移动到指定的文件夹。当用户提交数据时,具有图像文件名的路径将填入数据库,但所选图像不会移动到指定的文件夹。当我在网上关注教程时,我对此感到陌生。但现在只是文件不会移动到指定的文件夹。

这是HTML表单:

<form action="" enctype="multipart/form-data" method="post">
    <input id="imageSelect" name="profile" type=
    "file">
    <input class="submitButton" id="uploadimg" name="uploadimg" type=
    "submit" value="Upload">
</form>

PHP表单:

  if (isset($_FILES['profile']) === true) {
          if (empty($_FILES['profile']['name']) === true) {
              ?><script type='text/javascript'>alert('Please select an image before submitting!');</script><?
          } else {
              $allowed = array('jpg', 'jpeg', 'gif', 'png');

              $file_name = $_FILES['profile']['name'];
              $file_extn = strtolower(end(explode('.', $file_name)));
              $file_temp = $_FILES['profile']['tmp_name'];

              if (in_array($file_extn, $allowed) === true) {
                  change_profile_image($session_id, $file_temp, $file_extn);


                    header('Location: profileimgchange.php');
                    exit();
              } else {
                  ?><script type='text/javascript'>alert('Incorrect file type! Only jpeg, jpg, gif and png are allowed');</script><?
              }
          }
      }

change_profile_image功能:

function change_profile_image($user_id, $file_temp, $file_extn) {
$file_path = 'Image/ServerData/Images/ProfilePicture/' . substr(md5(time()), 0, 10) . '.' . $file_extn;

move_uploaded_file($file_temp, $file_path);
mysql_query("UPDATE `users` SET `profileimg` = '" . mysql_real_escape_string($file_path) . "' WHERE `user_id` = " . (int)$user_id);
}

函数脚本(如果有帮助)位于:

Core > System > Users.php

图像文件夹位于Core文件夹

之外
Image > ServerData > Images > ProfilePicture

我希望这些信息足够了。谢谢

2 个答案:

答案 0 :(得分:0)

$file_path正在相对于包含脚本的目录进行解释,所以它试图将文件放在Core/System/Image/ServerData/Images/ProfilePicture/中。你需要上升2级才能到达root。

$file_path = '../../Image/ServerData/Images/ProfilePicture/' . substr(md5(time()), 0, 10) . '.' . $file_extn;

如果这不对,请使用绝对路径。

答案 1 :(得分:0)

这个正在运作:)

if(isset($_POST['uploadimg'])){
$target_dir = "assets/uploads/";
$target_file = $target_dir . basename($_FILES["profile"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

$check = getimagesize($_FILES["profile"]["tmp_name"]);
if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
} else {
    echo "File is not an image.";
    $uploadOk = 0;
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["profile"]["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["profile"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["profile"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
}

<强> HTML

<form action="" enctype="multipart/form-data" method="post">
    <input id="imageSelect" name="profile" type="file">
    <input class="submitButton" id="uploadimg" name="uploadimg" type="submit" value="Upload">
</form>