move_uploaded_file返回false,但$_FILES ['uploadedfile'] ['error'] == 0。

4
我正在使用POST方法在upload_photo.php文件中上传一张照片,该文件使用uploader.php处理图像。Uploader.php将调整图像大小并覆盖旧图像。在本地工作正常,但在服务器上无法正常工作。

move_uploaded_file返回false,但 $_FILES['uploadedfile']['error']==0 这对我来说没有意义。

我发布了整个uploader.php和upload_photo.php中显示表单标记的片段。

<?php
//This is uploader.php
session_start();
include ('dbconfig.php');
include('SimpleImage.php');

mysql_connect(HOST, USERNAME, PASSWORD);
$conn = mysql_select_db(DB_NAME);

$target_path = "uploads/";

$target_path = $target_path . renameImage();

$_SESSION['client_photo'] = $target_path;

$query = "UPDATE client ";
$query .= "SET personal_photo = ('$target_path') ";
$query .= "WHERE client_id = ".$_SESSION['clientID'];
$results = mysql_query($query) or die('Error msg: '.mysql_error().'<br/>
        sql: '.query.'<br/>');

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
  $msg = "The file ".  $_SESSION['client_photo']. 
  " has been uploaded";
  chmod($target_path, "0666");
  $work = new ImgResizer($target_path); 
  $work -> resize(600, $target_path); 
} else{
  $msg = "There was an error uploading the file, please try again!";
  $msg .= $_FILES['uploadedfile']['error'];
}
header("Location: upload_photo.php?msg=$msg");

function renameImage(){ 
  mysql_connect(HOST, USERNAME, PASSWORD);
  $conn = mysql_select_db(DB_NAME);

  $sql = "SELECT first_name, last_name, client_id
          FROM client
          WHERE client_id = ".$_SESSION['clientID'];
  $res = mysql_query($sql) or die('Error msg: '.mysql_error().'<br/>
                sql: '.$sql.'<br/>');
  if($row = mysql_fetch_array($res, MYSQLI_ASSOC)){
      $_SESSION['successphoto'] = 1;
      return $row{first_name}.'_'.$row{last_name}.'_'.$row{client_id};
  }
  else{
      echo "There was an error while fetching the row.<br/>";
  }
}

class ImgResizer {
  private $originalFile = '';
  public function __construct($originalFile = '') {
      $this -> originalFile = $originalFile;
  }
  public function resize($newWidth, $targetFile) {
      if (empty($newWidth) || empty($targetFile)) {
          return false;
      }
      $src = imagecreatefromjpeg($this -> originalFile);
      list($width, $height) = getimagesize($this -> originalFile);
      $newHeight = ($height / $width) * $newWidth;
      $tmp = imagecreatetruecolor($newWidth, $newHeight);
      imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
      if (file_exists($targetFile)) {
          unlink($targetFile);
      }
      imagejpeg($tmp, $targetFile, 85); // 85 is my choice, make it between 0 – 100 for output image quality with 100 being the most luxurious
  }
}

?>

这是来自upload_photo.php的代码片段。
echo '<form enctype="multipart/form-data" action="uploader.php" method="POST">';
echo '<tr>';
//echo "<td colspan='2'>".$_GET['text']."</td>";
echo '</tr>';
echo '<tr align="center">';
echo '<td colspan="2">';
echo '<input type="hidden" name="MAX_FILE_SIZE" value="5000000" />
        Choose a file to upload: <input name="uploadedfile" type="file" accept="image/*"/><br />
        <input type="submit" value="Upload File" />
      </form>';

你能检查一下你尝试移动上传文件的位置是否具有写入权限吗? - Stelian Matei
根据您的php文件所在位置,$target_path可能是错误的。我不知道您的站点结构,但请尝试这样做:$target_path = '/uploads/'; - wakooka
@mazzucci 这是上传文件的权限:drwxrwxrwx 2 root root 4096 Nov 16 15:40 uploads - rharrison33
@jeromesmadja 这是一个特定文件的$targetpath:uploads/__90748。应该是正确的。此外,我认为chmod函数并没有正常工作,因为如果我注释掉权限,在本地它可以工作。我确实认为这是一个权限问题,但我不确定如何克服它。 - rharrison33
2个回答

3

这可能意味着上传已经成功 - 文件已上传到临时目录 - 但是文件无法移动到您设置的目标位置。

您应该仔细检查要移动文件的路径,并确保Web用户(apachewww或其他)具有写入该目录的权限。


这是上传文件的权限:drwxrwxrwx 2 root root 4096 Nov 16 15:40 uploads - rharrison33
我认为这是一个权限问题,但我不确定正确的权限是什么。 - rharrison33
@rharrison33 没有必要将其设置为全局可写,只需更改所有权为 Web 用户并确保所有者可以写入即可。您确定路径正确吗?您正在使用相对路径,因此可能不是您想象的那样。 - jeroen
2
$jeroen 我的 uploads/ 目录和 uploader.php 文件在同一个目录下,所以我很确定它是正确的。 - rharrison33

-1

你走对了路。只需像下面这样更改目录权限即可。希望它能起作用。

chmod 777 uploaded_folder_name

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接