无法使用move_uploaded_file将图像上传到目录?

时间:2015-04-17 13:09:26

标签: php apache .htaccess mod-rewrite

我目前在我的文件夹上传文件时遇到问题,使用move_uploaded_file函数这里是我的代码: -

<?php
//Check whether the user has uploaded a profile pic or not
  //Profile Image upload script
  if (isset($_FILES['profilepic'])) {
   if (((@$_FILES["profilepic"]["type"]=="image/jpeg") || (@$_FILES["profilepic"]["type"]=="image/png") || (@$_FILES["profilepic"]["type"]=="image/gif"))&&(@$_FILES["profilepic"]["size"] < 1048576)) //1 Megabyte
  {
   $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
   $rand_dir_name = substr(str_shuffle($chars), 0, 15);
   $dir = __DIR__."userdata/profile_pics/$rand_dir_name";
   mkdir($dir);
    move_uploaded_file(@$_FILES["profilepic"]["tmp_name"],"./userdata/profile_pics/$rand_dir_name/".$_FILES["profilepic"]["name"]);
    $profile_pic_name = @$_FILES["profilepic"]["name"];
    $profile_pic_query = mysqli_query($conn,"UPDATE users2 SET profile_pic='$rand_dir_name/$profile_pic_name' WHERE username='$user'");

  }
  else
  {
      $msg5 =  "Invailid File! Your image must be no larger than 1MB and it must be either a .jpg, .jpeg, .png or .gif";
  }
  }

?>

我正在从www.domain.com/editprofile文件夹上传文件,它似乎无法移动文件并将其上传到www.domain.com/userdata/randomfolder。我不知道但由于我的.htaccess代码,文件上传没有发生。这是我的.htaccess。

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9._-]+)/?$ profile.php?u=$1 [L,QSA]

2 个答案:

答案 0 :(得分:0)

在下面的行.....

 move_uploaded_file(@$_FILES["profilepic"]["tmp_name"],"./userdata/profile_pics/$rand_dir_name/".$_FILES["profilepic"]["name"]);
$profile_pic_name = @$_FILES["profilepic"]["name"];
你应该使用......

move_uploaded_file(@$_FILES["profilepic"]["tmp_name"],"../userdata/profile_pics/$rand_dir_name/".$_FILES["profilepic"]["name"]);

move_uploaded_file(@$_FILES["profilepic"]["tmp_name"],"/userdata/profile_pics/$rand_dir_name/".$_FILES["profilepic"]["name"]);

答案 1 :(得分:0)

我终于找到了解决方案,在这里:

<?php error_reporting(E_ALL); ini_set('display_errors', 1);
  if (isset($_FILES['profilepic'])) {
   if (((@$_FILES["profilepic"]["type"]=="image/jpeg") || (@$_FILES["profilepic"]["type"]=="image/png") || (@$_FILES["profilepic"]["type"]=="image/gif"))&&(@$_FILES["profilepic"]["size"] < 1048576)) //1 Megabyte
  {
   $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
   $rand_dir_name = substr(str_shuffle($chars), 0, 15);
   $dir = "/home/rahulkapoor90/public_html/userdata/profile_pics/$rand_dir_name";
   mkdir($dir);
    move_uploaded_file(@$_FILES["profilepic"]["tmp_name"],"/home/rahulkapoor90/public_html/userdata/profile_pics/$rand_dir_name/".$_FILES["profilepic"]["name"]);
    $profile_pic_name = @$_FILES["profilepic"]["name"];
    $profile_pic_query = mysqli_query($conn,"UPDATE users2 SET profile_pic='$rand_dir_name/$profile_pic_name' WHERE username='$user'");

  }
  else
  {
      echo  "Invailid File! Your image must be no larger than 1MB and it must be either a .jpg, .jpeg, .png or .gif";
  }
  }

?>